2014-02-02 10:07:39 +04:00
|
|
|
|
|
|
|
var _ = require('lodash'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-02-02 10:07:39 +04:00
|
|
|
spawn = require('child_process').spawn,
|
|
|
|
win32 = process.platform === 'win32';
|
|
|
|
|
|
|
|
function AppDependencies(appPath) {
|
|
|
|
this.appPath = appPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppDependencies.prototype.install = function installAppDependencies() {
|
2014-08-17 10:17:23 +04:00
|
|
|
var spawnOpts,
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-03-12 23:12:56 +03:00
|
|
|
fs.stat(path.join(self.appPath, 'package.json'), function (err) {
|
|
|
|
if (err) {
|
|
|
|
// File doesn't exist - nothing to do, resolve right away?
|
2014-08-17 10:17:23 +04:00
|
|
|
resolve();
|
2014-09-10 08:06:24 +04:00
|
|
|
} else {
|
2014-08-17 10:17:23 +04:00
|
|
|
// Run npm install in the app directory
|
|
|
|
spawnOpts = {
|
|
|
|
cwd: self.appPath
|
|
|
|
};
|
|
|
|
|
|
|
|
self.spawnCommand('npm', ['install', '--production'], spawnOpts)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('exit', function (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-02-02 10:07:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Normalize a command across OS and spawn it; taken from yeoman/generator
|
|
|
|
AppDependencies.prototype.spawnCommand = function (command, args, opt) {
|
|
|
|
var winCommand = win32 ? 'cmd' : command,
|
|
|
|
winArgs = win32 ? ['/c'].concat(command, args) : args;
|
|
|
|
|
|
|
|
opt = opt || {};
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
return spawn(winCommand, winArgs, _.defaults({stdio: 'inherit'}, opt));
|
2014-02-02 10:07:39 +04:00
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = AppDependencies;
|