Skip to content

Commit 0d0bcc6

Browse files
authored
Added support for webgme import all. Closes #211 (#228)
* WIP #211 added support for `webgme import all` * WIP #211 fixed project name resolution * WIP #211 removed support for node v4 for cli
1 parent efe2d96 commit 0d0bcc6

6 files changed

Lines changed: 122 additions & 30 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ cache:
44
directories:
55
- node_modules
66
node_js:
7-
- "4"
87
- "6"
98
- "8"

bin/webgme-import

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#!/usr/bin/env node
22

33
'use strict';
4+
var descs = {
5+
all: 'Import all components from another app'
6+
};
7+
var args = {
8+
all: '<app>'
9+
};
410
require('../lib/BinUtils')
511
.createSubCommands(__dirname,
612
'<name> <app>',
713
'Import a new <%= name %> from another app',
814
{
9-
explicit: true
15+
explicit: true,
16+
descs: descs,
17+
args: args
1018
}
1119
);

bin/webgme-import-all

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
/*jshint node: true*/
3+
4+
'use strict';
5+
var Command = require('commander').Command,
6+
program = new Command(),
7+
webgme = require('..');
8+
9+
program.arguments('<project>')
10+
.option('-p,--package-name [pkg-name]', 'Set installed package name (in package.json)')
11+
.action(function(project) {
12+
program.project = project;
13+
webgme.all.import(project, program);
14+
})
15+
.parse(process.argv);

index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// cli usage for webgme-cli
2+
var Q = require('q');
3+
var utils = require('./lib/utils');
4+
var _ = require('lodash');
5+
26
var 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+
1783
module.exports = webgme;

src/ComponentManager.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ var utils = require('./utils'),
1717
Q = require('q'),
1818
path = require('path'),
1919
exists = require('exists-file'),
20-
childProcess = require('child_process'),
21-
spawn = childProcess.exec,
2220
Logger = require(__dirname + '/Logger');
2321

2422
var ComponentManager = function(name, logger) {
@@ -149,7 +147,7 @@ ComponentManager.prototype.import = function(args, callback) {
149147
if (args.skipInstall) {
150148
this._addComponentFromProject(componentName, pkgProject, callback);
151149
} else {
152-
this._installProject(project, args.dev, (err, result) => {
150+
utils.installProject(project, args.dev, (err, result) => {
153151
this._addComponentFromProject(componentName, pkgProject, callback);
154152
});
155153
}
@@ -179,29 +177,6 @@ ComponentManager.prototype._addComponentFromProject = function(name, project, ca
179177
});
180178
};
181179

182-
ComponentManager.prototype._installProject = function(projectName, isDev, callback) {
183-
let projectRoot = utils.getRootPath();
184-
let cmd = isDev ?
185-
`npm install ${projectName} --save-dev`:
186-
`npm install ${projectName} --save`;
187-
let job = spawn(cmd, {cwd: projectRoot});
188-
189-
this._logger.info(cmd);
190-
this._logger.writeStream(job.stdout);
191-
this._logger.errorStream(job.stderr);
192-
193-
job.on('close', code => {
194-
this._logger.info(`npm exited with: ${code}`);
195-
if (code === 0) { // Success!
196-
return callback(null);
197-
} else {
198-
let err = `Could not find project (${projectName})!`;
199-
this._logger.error(err);
200-
return callback(err);
201-
}
202-
});
203-
};
204-
205180
ComponentManager.prototype._getJsonForConfig = function(installInfo, callback) {
206181
var self = this;
207182
this._getPathFromDependency(installInfo, function(err, componentPath) {

src/utils.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/*globals define*/
22
'use strict';
33

4+
const childProcess = require('child_process');
5+
const spawn = childProcess.exec;
6+
const Logger = require('./Logger');
7+
const logger = new Logger();
8+
const PROJECT_CONFIG = 'webgme-setup.json';
9+
410
var _ = require('lodash'),
511
fs = require('fs'),
612
path = require('path'),
713
exists = require('exists-file'),
814
assert = require('assert'),
9-
R = require('ramda'),
10-
PROJECT_CONFIG = 'webgme-setup.json';
15+
R = require('ramda');
1116

1217
var getRootPath = function(startPath) {
1318
// Walk back from current path until you find a webgme-setup.json file
@@ -377,6 +382,29 @@ var normalizePath = function(dirs) {
377382
return dirs;
378383
};
379384

385+
const installProject = function(projectName, isDev, callback) {
386+
let projectRoot = getRootPath();
387+
let cmd = isDev ?
388+
`npm install ${projectName} --save-dev`:
389+
`npm install ${projectName} --save`;
390+
let job = spawn(cmd, {cwd: projectRoot});
391+
392+
logger.info(cmd);
393+
logger.writeStream(job.stdout);
394+
logger.errorStream(job.stderr);
395+
396+
job.on('close', code => {
397+
logger.info(`npm exited with: ${code}`);
398+
if (code === 0) { // Success!
399+
return callback(null);
400+
} else {
401+
let err = `Could not find project (${projectName})!`;
402+
logger.error(err);
403+
return callback(err);
404+
}
405+
});
406+
};
407+
380408
module.exports = {
381409
PROJECT_CONFIG: PROJECT_CONFIG,
382410
saveConfig: saveConfig,
@@ -391,5 +419,6 @@ module.exports = {
391419
loadPaths: loadPaths,
392420
getPackageName: getPackageName,
393421
normalize: normalizePath,
422+
installProject: installProject,
394423
mkdir: createDir
395424
};

0 commit comments

Comments
 (0)