Use npm programmatically to install devDependencies

This commit is contained in:
Aurélien Georget 2017-01-04 19:57:45 +01:00
parent 7ca8dd2074
commit 958448683f
3 changed files with 43 additions and 15 deletions

View File

@ -7,5 +7,5 @@
"prefix": "", "prefix": "",
"static": true, "static": true,
"views": false, "views": false,
"websockets": true "websockets": false
} }

View File

@ -22,11 +22,11 @@ module.exports = scope => {
'version': '0.1.0', 'version': '0.1.0',
'description': 'A Strapi application.', 'description': 'A Strapi application.',
'devDependencies': { 'devDependencies': {
"babel-eslint": "^7.1.1", 'babel-eslint': '^7.1.1',
"eslint": "^3.12.2", 'eslint': '^3.12.2',
"eslint-config-airbnb": "^13.0.0", 'eslint-config-airbnb': '^13.0.0',
"eslint-plugin-import": "^2.2.0", 'eslint-plugin-import': '^2.2.0',
"eslint-plugin-react": "^6.8.0" 'eslint-plugin-react': '^6.8.0'
}, },
'dependencies': { 'dependencies': {
'lodash': '4.x.x', 'lodash': '4.x.x',

View File

@ -10,6 +10,7 @@ const path = require('path');
// Public node modules. // Public node modules.
const _ = require('lodash'); const _ = require('lodash');
const fs = require('fs-extra'); const fs = require('fs-extra');
const npm = require('npm');
// Logger. // Logger.
const logger = require('strapi-utils').logger; const logger = require('strapi-utils').logger;
@ -33,7 +34,7 @@ module.exports = (scope, cb) => {
const missingDependencies = []; const missingDependencies = [];
// Verify if the dependencies are available into the global // Verify if the dependencies are available into the global
_.forEach(_.get(packageJSON, 'dependencies'), (value, key) => { _.forEach(_.merge(_.get(packageJSON, 'dependencies'), _.get(packageJSON, 'devDependencies')), (value, key) => {
try { try {
fs.accessSync(path.resolve(strapiRootPath, key), fs.constants.R_OK | fs.constants.W_OK); fs.accessSync(path.resolve(strapiRootPath, key), fs.constants.R_OK | fs.constants.W_OK);
fs.symlinkSync(path.resolve(strapiRootPath, key), path.resolve(scope.rootPath, 'node_modules', key), 'dir'); fs.symlinkSync(path.resolve(strapiRootPath, key), path.resolve(scope.rootPath, 'node_modules', key), 'dir');
@ -50,13 +51,40 @@ module.exports = (scope, cb) => {
logger.info('Your new application `' + scope.name + '` is ready at `' + scope.rootPath + '`.'); logger.info('Your new application `' + scope.name + '` is ready at `' + scope.rootPath + '`.');
if (!_.isEmpty(missingDependencies)) { if (!_.isEmpty(missingDependencies)) {
npm.load({loglevel: 'silent'}, function(err) {
if (err) {
console.log(); console.log();
logger.warn('You should run `npm install` into your application before starting it.'); logger.warn('You should run `npm install` into your application before starting it.');
console.log(); console.log();
logger.warn('Some dependencies could not be installed:'); logger.warn('Some dependencies could not be installed:');
_.forEach(missingDependencies, value => logger.warn('• ' + value)); _.forEach(missingDependencies, value => logger.warn('• ' + value));
console.log(); console.log();
}
return cb(); return cb();
}
const installDependency = (dependency, index) => {
if (_.isEmpty(dependency)) {
console.log();
return cb();
}
console.log();
logger.info('Installing ' + dependency + '...');
console.log();
npm.commands.install([dependency], (err) => {
if (err) {
console.log();
logger.warn('You should run `npm install ' + dependency + '` into your application before starting it.');
console.log();
}
installDependency(missingDependencies[index++], index);
});
};
installDependency(missingDependencies[0], 0);
});
}
}; };