11// cli usage for webgme-cli
2+ var Q = require ( 'q' ) ;
3+ var utils = require ( './lib/utils' ) ;
4+ var _ = require ( 'lodash' ) ;
5+
26var webgme = { } ;
37
48[
@@ -14,4 +18,66 @@ var webgme = {};
1418 webgme [ component . _name ] = component ;
1519} ) ;
1620
21+ // Add 'all' option
22+ webgme . all = { } ;
23+ webgme . all . import = function ( projectName , opts , callback ) {
24+ let rootPath ;
25+
26+ opts = opts || { } ;
27+ return Q ( )
28+ . then ( ( ) => { // ensure in a webgme app dir. Move to the root.
29+ rootPath = utils . getRootPath ( ) ;
30+ if ( ! rootPath ) {
31+ throw new Error ( 'Could not find a project in current or any parent directories' ) ;
32+ }
33+ process . chdir ( rootPath ) ;
34+ } )
35+ . then ( ( ) => { // install the project
36+ let deferred = Q . defer ( ) ;
37+ utils . installProject ( projectName , false , ( err , config ) => {
38+ if ( err ) return deferred . reject ( err ) ;
39+ deferred . resolve ( config ) ;
40+ } ) ;
41+ return deferred . promise ;
42+ } )
43+ . then ( ( ) => { // read in the webgme-cli config for the given project
44+ projectName = opts . packageName || utils . getPackageName ( projectName ) ;
45+ projectName = projectName . toLowerCase ( ) ;
46+ let configPath = utils . getConfigPath ( projectName ) ;
47+ let config = require ( configPath ) ;
48+ return config ;
49+ } )
50+ . then ( config => { // import each of the components
51+ const components = config . components ;
52+ const types = Object . keys ( components ) ;
53+ return Q . all ( types . map ( type => {
54+ const managerId = type . replace ( / s $ / , '' ) ; // make it singular...
55+ const names = Object . keys ( components [ type ] ) ;
56+ const imports = names . map ( name => {
57+ const args = {
58+ name : name ,
59+ project : projectName ,
60+ skipInstall : true
61+ } ;
62+ return webgme [ managerId ] . import ( args ) ;
63+ } ) ;
64+ return Q . all ( imports ) ;
65+ } ) )
66+ . then ( ( ) => config ) ;
67+ } )
68+ . then ( config => { // import-all on each of the dependencies
69+ const deps = config . dependencies ;
70+ const types = Object . keys ( deps ) ;
71+ const allProjects = types
72+ . map ( type => {
73+ const names = Object . keys ( deps [ type ] ) ;
74+ return names . map ( name => deps [ type ] [ name ] . project ) ;
75+ } )
76+ . reduce ( ( l1 , l2 ) => l1 . concat ( l2 ) , [ ] ) ;
77+ const projects = _ . uniq ( allProjects ) ;
78+ return Q . all ( projects . map ( project => webgme . all . import ( project ) ) ) ;
79+ } )
80+ . nodeify ( callback ) ;
81+ } ;
82+
1783module . exports = webgme ;
0 commit comments