Skip to content

Commit 238406a

Browse files
committed
Merge pull request #64 from CleverStack/feat-module-dependencies
feat(peerDependencies): Re-implement module installation dependencies without bundledDependencies[]
2 parents 5c69f55 + 875bf3e commit 238406a

File tree

5 files changed

+194
-105
lines changed

5 files changed

+194
-105
lines changed

bin/clever-init

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var path = require('path')
99
, rimraf = require('rimraf')
1010
, async = require('async')
1111
, exec = require('child_process').exec
12+
, spawn = require('child_process').spawn
1213
, Promise = require('bluebird')
1314
, singleSeed = true
1415
, seedsToInstall = []
@@ -58,7 +59,7 @@ program.on('--help', function() {
5859
/** Parse CLI Arguments
5960
================================*/
6061
program.parse(process.argv);
61-
if (!program.args[ 0 ] || program.args[ 0 ].toString().trim() === '') {
62+
if (!program.args[0] || program.args[0].toString().trim() === '') {
6263
program.help();
6364
}
6465

@@ -76,13 +77,16 @@ if (!program.args[ 0 ] || program.args[ 0 ].toString().trim() === '') {
7677
* @api private
7778
*/
7879
function writeLocalJSON(projectDir) {
80+
var configDir = path.resolve(path.join(projectDir, 'config'));
81+
7982
return new Promise(function(resolve, reject) {
80-
var localJSONFile = require(path.join(projectDir, 'config', 'local.example.json'));
83+
var localJSONFile = require(path.join(configDir, 'local.example.json'));
8184

82-
utils.info([ ' Creating local configuration file config/local.json', '...' ].join(''));
83-
utils.running('Creating config/local.json...');
85+
utils
86+
.info(' Creating local configuration file config/local.json...')
87+
.running('Creating config/local.json...');
8488

85-
fs.writeFile(path.join(projectDir, 'config', 'local.json'), JSON.stringify(localJSONFile, null, 2), function (err) {
89+
fs.writeFile(path.join(configDir, 'local.json'), JSON.stringify(localJSONFile, null, 2), function(err) {
8690
if (!!err) {
8791
return reject(err);
8892
}
@@ -101,22 +105,34 @@ function writeLocalJSON(projectDir) {
101105
*/
102106
function installNPMPackages(projectDir) {
103107
return new Promise(function(resolve, reject) {
104-
utils.info([ ' Installing NPM modules...' ].join(''));
105-
utils.running('Installing NPM modules...');
106-
var proc = exec('npm install --silent', { cwd: projectDir }, function(err) {
107-
if (!!err && err.message !== 'Command failed: ') {
108-
utils.fail(err);
109-
reject(err);
108+
utils
109+
.info(' Installing NPM modules...')
110+
.running('Installing NPM modules...');
111+
112+
var args = ['install']
113+
, opts = { env: process.env, cwd: projectDir };
114+
115+
if (!!program.verbose) {
116+
opts.stdio = 'inherit';
117+
} else {
118+
args.push('--silent');
119+
}
120+
121+
var proc = spawn('npm', args, opts)
122+
, error = '';
123+
124+
proc.on('error', function(err) {
125+
error += err;
126+
});
127+
128+
proc.on('close', function(code) {
129+
if (code !== 0 || !!error && error !== 'Command failed: ') {
130+
utils.fail(error);
131+
reject(error);
110132
} else {
111133
resolve();
112134
}
113135
});
114-
115-
// Pipe the output of exec if verbose has been specified
116-
if (program.verbose) {
117-
proc.stdout.pipe(process.stdout);
118-
proc.stderr.pipe(process.stdout);
119-
}
120136
});
121137
}
122138

@@ -147,8 +163,10 @@ function setupBackend() {
147163
return reject(err);
148164
}
149165

150-
utils.info([ ' Downloading and extracting ', csPackage.name, '...' ].join(''));
151-
utils.running('Downloading and extracting...');
166+
utils
167+
.info('Downloading and extracting ' + csPackage.name + '...')
168+
.running('Downloading and extracting ' + csPackage.name + '...');
169+
152170
lib.packages
153171
.get(csPackage, projectDir)
154172
.then(function() {
@@ -159,6 +177,14 @@ function setupBackend() {
159177
utils.progress();
160178
return installNPMPackages(projectDir);
161179
})
180+
.then(function() {
181+
utils
182+
.info(' Installing module dependencies...', '└── ')
183+
.running('Installing module dependencies...')
184+
.progress();
185+
186+
return lib.util.dependencies.installPeerDependencies(projectDir);
187+
})
162188
.then(function() {
163189
utils.progress();
164190

@@ -546,7 +572,7 @@ async.waterfall(
546572
lib.utils.finishProgress();
547573
process.exit(0);
548574
} else {
549-
utils.fail(err);
575+
utils.fail(err.stack ? (err + '\n' + err.stack) : err);
550576
}
551577
}
552578
);

lib/install.js

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Promise = require('bluebird')
22
, path = require('path')
33
, _ = require('lodash')
44
, https = require('follow-redirects').https
5+
, async = require('async')
56
, utils = GLOBAL.lib.utils
67
, search = GLOBAL.lib.search
78
, packages = GLOBAL.lib.packages
@@ -76,31 +77,91 @@ function install(repos) {
7677
});
7778
}
7879

80+
function installBackendModules(backendPath, npm) {
81+
return new Promise(function(resolve, reject) {
82+
npm = npm.map(function(n) {
83+
return n.name;
84+
});
85+
86+
var walker = require('findit')(path.join(backendPath.moduleDir, backendPath.modulePath))
87+
, dirs = [];
88+
89+
walker.on('directory', function(dir, stat, stop) {
90+
var _dirs = dir.split(path.sep)
91+
, _dir = _dirs.pop()
92+
, mdir = path.dirname(dir).split(path.sep).pop();
93+
94+
if (mdir === 'modules') {
95+
if (npm.indexOf(_dir) === -1) {
96+
return stop();
97+
}
98+
99+
dirs.push(path.join(_dirs.join(path.sep), _dir));
100+
}
101+
});
102+
103+
walker.on('end', function() {
104+
if (dirs.length > 0) {
105+
lib.utils.info([ ' Installing module peerDependencies...' ].join(''));
106+
107+
async.each(
108+
dirs,
109+
function installModulePeerDependencies(dir, installed) {
110+
util
111+
.dependencies
112+
.installPeerDependencies(dir, path.join(backendPath.moduleDir, backendPath.modulePath))
113+
.then(function () {
114+
installed();
115+
})
116+
.catch(installed);
117+
},
118+
function peerDependenciesInstalled(err) {
119+
if (!!err) {
120+
return reject(err);
121+
}
122+
123+
async.eachSeries(
124+
npm,
125+
function(module, next) {
126+
util
127+
.grunt
128+
.runTasks(backendPath.moduleDir, path.join(backendPath.moduleDir, backendPath.modulePath, module))
129+
.then(next, next);
130+
},
131+
function( err) {
132+
if (!!err) {
133+
return reject(err);
134+
}
135+
136+
resolve();
137+
}
138+
);
139+
}
140+
);
141+
}
142+
});
143+
});
144+
}
145+
146+
79147
exports.run = function(args) {
80148
return new Promise(function(resolve, reject) {
81149
install(args)
82150
.spread(function(backendPath, frontendPath, npm /*, bower */) {
83151
var actions = [];
84152

85-
npm.forEach(function(module) {
86-
actions.push(
87-
util
88-
.grunt
89-
.runTasks(backendPath.moduleDir, path.join(backendPath.moduleDir, backendPath.modulePath, module.name))
90-
);
91-
});
153+
if (npm.length > 0) {
154+
actions.push(installBackendModules(backendPath, npm));
155+
}
92156

93157
Promise
94158
.all(actions)
95-
.then(function() {
159+
.then(function runNpmTasksForModules() {
96160
resolve();
97-
}, function (err) {
98-
reject(err);
99-
});
100-
101-
}, function (err) {
102-
reject(err);
103-
});
161+
})
162+
.catch(reject);
163+
})
164+
.catch(reject);
104165
});
105166
};
106167

lib/search.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,29 @@ var searchNPMRegistry = exports.npmRegistry = function(queries) {
2222
return new Promise(function(resolve, reject) {
2323
var repos = [];
2424

25-
// Keep this as SERIES until we come up with a better system
26-
// than bundleDeps.. when we do clever install clever-orm clever-auth
27-
// auth HAS to come AFTER ORM.. kind of a bad system
28-
// so we'll definitely need some sort of dependency management system.
29-
async.eachSeries(
25+
async.each(
3026
queries,
3127
function(q, next) {
3228
var search = q.split('@');
33-
if (typeof search[ 1 ] === 'undefined') {
34-
search[ 1 ] = '*';
29+
if (typeof search[1] === 'undefined') {
30+
search[1] = '*';
3531
}
3632

3733
_pkg.getJSON({
3834
hostname: 'registry.npmjs.org',
39-
path: '/' + encodeURIComponent(search[ 0 ])
35+
path: '/' + encodeURIComponent(search[0])
4036
})
4137
.then(function(body) {
4238
if (typeof body === 'undefined' || (body.error && body.error === 'not_found')) {
4339
return next();
4440
}
4541

46-
var versions = Object.keys(body.versions)
47-
, maxVersion;
42+
var versions = Object.keys(body.versions)
43+
, maxVersion = semver.maxSatisfying(versions, search[1])
44+
, pkg = body.versions[maxVersion];
4845

49-
if ([ '>', '<', '=', '*' ].indexOf(search[ 1 ]) > -1) {
50-
maxVersion = semver.maxSatisfying(versions, search[ 1 ]);
51-
} else {
52-
maxVersion = semver.clean(search[ 1 ]);
53-
}
54-
55-
var pkg = body.versions[ maxVersion ];
5646
if (typeof pkg === 'undefined') {
57-
utils.fail('Invalid version ' + search[ 1 ] + ' for module ' + search[ 0 ], true);
47+
utils.fail('Invalid version ' + search[1] + ' for module ' + search[0], true);
5848
return next();
5949
}
6050

0 commit comments

Comments
 (0)