@@ -9,9 +9,9 @@ var path = require('path');
99var prompt = require ( 'prompt' )
1010var Q = require ( 'q' ) ;
1111var semver = require ( 'semver' ) ;
12+ var Errors = require ( '../errors' ) ;
1213var Constants = require ( '../Constants' ) ;
1314var JsonLoader = require ( '../JsonLoader' ) ;
14- var JsonWriter = require ( '../JsonWriter' ) ;
1515var PackageMeta = require ( '../PackageMeta' ) ;
1616var Plugin = require ( '../Plugin' ) ;
1717var Project = require ( '../Project' ) ;
@@ -38,8 +38,33 @@ module.exports = function (dependencies) {
3838 var isCompatibleEnabled = false ;
3939 // whether adapt.json is being used to compile the list of plugins to install
4040 var isUsingManifest = false ;
41+ // whether this command is being performed on the command line
42+ var isInteractive = true ;
4143
4244 return {
45+ apiinstall : function ( pluginName , cwd ) {
46+ isInteractive = false ;
47+
48+ Constants . setCwd ( cwd ) ;
49+
50+ project = new Project ( Constants . DefaultProjectManifestPath , Constants . DefaultProjectFrameworkPath ) ;
51+
52+ if ( ! project . isProjectContainsManifestFile ( ) ) {
53+ return Q . reject ( { error :Errors . ERROR_COURSE_DIR } ) ;
54+ }
55+
56+ itinerary = { } ;
57+ plugins = [ ] ;
58+
59+ return init ( pluginName ? [ pluginName ] : [ ] )
60+ . then ( createPlugins )
61+ . then ( getInitialInfo )
62+ . then ( findCompatibleVersions )
63+ . then ( checkConstraints )
64+ . then ( createInstallationManifest )
65+ . then ( performInstallation )
66+ . then ( summariseInstallation ) ;
67+ } ,
4368 install : function ( renderer ) {
4469 var args = [ ] . slice . call ( arguments , 1 ) ;
4570 var done = args . pop ( ) || function ( ) { } ;
@@ -145,7 +170,11 @@ module.exports = function (dependencies) {
145170 promises . push ( plugins [ i ] . getInitialInfo ( ) ) ;
146171 }
147172
148- return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
173+ if ( isInteractive ) {
174+ return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
175+ }
176+
177+ return Q . all ( promises ) ;
149178
150179 function progressUpdate ( ) {
151180 var settled = plugins . filter ( function ( plugin ) { return plugin . _rawInfo || plugin . _isMissingAtRepo ; } ) . length ;
@@ -169,7 +198,11 @@ module.exports = function (dependencies) {
169198 promises . push ( present [ i ] . findCompatibleVersion ( project . getFrameworkVersion ( ) ) ) ;
170199 }
171200
172- return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
201+ if ( isInteractive ) {
202+ return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
203+ }
204+
205+ return Q . all ( promises ) ;
173206
174207 function progressUpdate ( ) {
175208 var settled = present . filter ( function ( plugin ) { return plugin . _latestCompatibleVersion != undefined ; } ) . length ;
@@ -193,7 +226,11 @@ module.exports = function (dependencies) {
193226 promises . push ( present [ i ] . checkConstraint ( project . getFrameworkVersion ( ) ) ) ;
194227 }
195228
196- return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
229+ if ( isInteractive ) {
230+ return Q . all ( promises ) . progress ( progressUpdate ) . then ( conclude ) ;
231+ }
232+
233+ return Q . all ( promises ) ;
197234
198235 function progressUpdate ( ) {
199236 var settled = present . filter ( function ( plugin ) { return plugin . _constraintChecked != undefined ; } ) . length ;
@@ -234,6 +271,37 @@ module.exports = function (dependencies) {
234271 // a compatible version exists but user has requested a valid version that is later than the latest compatible version (prompt for (r)equested, (l)atest compatible or (s)kip)
235272 var compatibleWithUnmetConstraint = present . filter ( isCompatibleWithUnmetConstraint ) ;
236273
274+ if ( ! isInteractive ) {
275+
276+ incompatibleWithOldConstraint . forEach ( function ( p ) {
277+ p . _error = Errors . ERROR_INCOMPATIBLE_VALID_REQUEST ;
278+ } ) ;
279+ incompatibleWithLatestConstraint . forEach ( function ( p ) {
280+ p . _error = Errors . ERROR_INCOMPATIBLE_VALID_REQUEST ;
281+ } ) ;
282+ incompatibleWithBadConstraint . forEach ( function ( p ) {
283+ p . _error = Errors . ERROR_INCOMPATIBLE_BAD_REQUEST ;
284+ } ) ;
285+ incompatibleWithNoConstraint . forEach ( function ( p ) {
286+ p . _error = Errors . ERROR_INCOMPATIBLE ;
287+ } ) ;
288+ compatibleWithOldIncompatibleConstraint . forEach ( function ( p ) {
289+ p . _error = Errors . ERROR_COMPATIBLE_INC_REQUEST ;
290+ } ) ;
291+ compatibleWithBadConstraint . forEach ( function ( p ) {
292+ p . _error = Errors . ERROR_COMPATIBLE_BAD_REQUEST ;
293+ } ) ;
294+ compatibleWithUnmetConstraint . forEach ( function ( p ) {
295+ p . _error = Errors . ERROR_COMPATIBLE_INC_REQUEST ;
296+ } ) ;
297+
298+ compatibleWithOldCompatibleConstraint . forEach ( function ( p ) {
299+ p . markRequestedForInstallation ( ) ;
300+ } ) ;
301+
302+ return Q . resolve ( ) ;
303+ }
304+
237305 var allPromises = [ ] ;
238306
239307 add ( incompatibleWithOldConstraint , 'There is no compatible version of the following plugins:' , getPrompt_incompatibleGeneric ) ;
@@ -270,6 +338,10 @@ module.exports = function (dependencies) {
270338 console . log ( obj . header ) ;
271339 return promise . serialise ( obj . list , obj . prompt ) ;
272340 }
341+
342+ function getPackageName ( p ) {
343+ return p . packageName ;
344+ }
273345 }
274346
275347 function updateManifest ( ) {
@@ -477,6 +549,69 @@ module.exports = function (dependencies) {
477549 var noSuccess = 'None of the requested plugins could be installed' ;
478550 var successMsg ;
479551
552+ if ( ! isInteractive ) {
553+ var report = [ ] ;
554+
555+ if ( plugins . length == 1 ) {
556+ var p = plugins [ 0 ] ;
557+
558+ if ( installSucceeded . length == 1 ) {
559+ var bowerPath = path . join ( Constants . cwd , 'src' , p . _belongsTo , p . packageName , 'bower.json' ) ;
560+ return Q . resolve ( JsonLoader . readJSONSync ( bowerPath ) ) ;
561+ }
562+ if ( installSkipped . length == 1 ) {
563+ return Q . reject ( p . _error ) ;
564+ }
565+ if ( installErrored . length == 1 ) {
566+ var error = _ . clone ( Errors . ERROR_INSTALL_ERROR ) ;
567+
568+ if ( p . _installError ) error . message = p . _installError ;
569+
570+ return Q . reject ( error ) ;
571+ }
572+ return Q . reject ( Errors . ERROR_NOT_FOUND ) ;
573+ }
574+
575+ installSucceeded . forEach ( function ( p ) {
576+ var bowerPath = path . join ( Constants . cwd , 'src' , p . _belongsTo , p . packageName , 'bower.json' ) ;
577+ report . push ( {
578+ name :p . packageName ,
579+ status :'fulfilled' ,
580+ pluginData :JsonLoader . readJSONSync ( bowerPath )
581+ } ) ;
582+ } ) ;
583+
584+ installSkipped . forEach ( function ( p ) {
585+ report . push ( {
586+ name :p . packageName ,
587+ status :'rejected' ,
588+ reason :p . _error
589+ } ) ;
590+ } ) ;
591+
592+ installErrored . forEach ( function ( p ) {
593+ var error = _ . clone ( Errors . ERROR_INSTALL_ERROR ) ;
594+
595+ if ( p . _installError ) error . message = p . _installError ;
596+
597+ report . push ( {
598+ name :p . packageName ,
599+ status :'rejected' ,
600+ reason :error
601+ } ) ;
602+ } ) ;
603+
604+ missing . forEach ( function ( p ) {
605+ report . push ( {
606+ name :p . packageName ,
607+ status :'rejected' ,
608+ reason :Errors . ERROR_NOT_FOUND
609+ } ) ;
610+ } ) ;
611+
612+ return Q . resolve ( report ) ;
613+ }
614+
480615 if ( installErrored . length == 0 && missing . length == 0 ) successMsg = allSuccess ;
481616 else if ( installSucceeded . length == 0 ) successMsg = noSuccess ;
482617 else successMsg = someSuccess ;
@@ -676,7 +811,11 @@ module.exports = function (dependencies) {
676811 }
677812
678813 function performInstallation ( ) {
679- return Q . all ( plugins . filter ( isToBeInstalled ) . map ( createInstallationTask ) ) . progress ( progressUpdate ) . then ( conclude ) ;
814+ if ( isInteractive ) {
815+ return Q . all ( plugins . filter ( isToBeInstalled ) . map ( createInstallationTask ) ) . progress ( progressUpdate ) . then ( conclude ) ;
816+ }
817+
818+ return Q . all ( plugins . filter ( isToBeInstalled ) . map ( createInstallationTask ) ) ;
680819
681820 function progressUpdate ( ) {
682821 var list = plugins . filter ( isPresent ) . filter ( isToBeInstalled ) ;
@@ -692,7 +831,7 @@ module.exports = function (dependencies) {
692831 }
693832
694833 function createInstallationTask ( plugin ) {
695- return PackageMeta . getKeywords ( plugin , { registry : Constants . getRegistry ( ) } ) . then ( doInstall ) . then ( conclude ) ;
834+ return PackageMeta . getKeywords ( plugin , { registry : Constants . getRegistry ( ) , cwd : Constants . cwd } ) . then ( doInstall ) . then ( conclude ) ;
696835
697836 function doInstall ( keywords ) {
698837 var resolver = new PluginTypeResolver ( ) ,
@@ -703,7 +842,8 @@ module.exports = function (dependencies) {
703842
704843 return install ( plugin , {
705844 directory : path . join ( 'src' , pluginType . belongsTo ) ,
706- registry : Constants . getRegistry ( )
845+ registry : Constants . getRegistry ( ) ,
846+ cwd : Constants . cwd
707847 } ) ;
708848 }
709849
0 commit comments