113 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
const { execSync } = require('child_process');
2016-03-18 11:12:50 +01:00
// Public node modules.
const _ = require('lodash');
2016-03-18 11:12:50 +01:00
const fs = require('fs-extra');
const npm = require('enpeem');
2017-08-24 12:29:08 +02:00
const getInstalledPath = require('get-installed-path');
2016-03-18 11:12:50 +01:00
// Logger.
const logger = require('strapi-utils').logger;
/**
* Runs after this generator has finished
*
* @param {Object} scope
* @param {Function} cb
*/
module.exports = (scope, cb) => {
const packageJSON = require(path.resolve(scope.rootPath, 'package.json'));
const strapiRootPath = path.resolve(scope.strapiRoot, '..');
2016-03-18 11:12:50 +01:00
process.chdir(scope.rootPath);
// Copy the default files.
fs.copySync(path.resolve(__dirname, '..', 'files'), path.resolve(scope.rootPath));
2017-08-17 16:46:27 +02:00
const availableDependencies = [];
2017-08-24 12:29:08 +02:00
const dependencies = _.get(packageJSON, 'dependencies');
const strapiDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') !== -1);
const othersDependencies = Object.keys(dependencies).filter(key => key.indexOf('strapi') === -1);
2016-03-18 11:12:50 +01:00
// Verify if the dependencies are available into the global
2017-08-24 12:29:08 +02:00
_.forEach(strapiDependencies, (key) => {
try {
2017-08-24 12:29:08 +02:00
const isInstalled = getInstalledPath.sync(key);
2017-08-17 16:46:27 +02:00
availableDependencies.push({
key,
2017-08-24 12:29:08 +02:00
global: true,
path: isInstalled
2017-08-17 16:46:27 +02:00
});
2017-08-24 12:29:08 +02:00
} catch (e) {
othersDependencies.push(key);
2016-03-18 11:12:50 +01:00
}
});
2017-08-24 12:29:08 +02:00
logger.info('Installing dependencies...');
if (!_.isEmpty(othersDependencies)) {
npm.install({
2017-08-24 12:29:08 +02:00
dependencies: othersDependencies,
loglevel: 'silent',
2017-08-24 12:29:08 +02:00
production: true,
'cache-min': 999999999
}, err => {
if (err) {
console.log();
logger.warn('You should run `npm install` into your application before starting it.');
console.log();
logger.warn('Some dependencies could not be installed:');
2017-08-24 12:29:08 +02:00
_.forEach(othersDependencies, value => logger.warn('• ' + value));
console.log();
return cb();
}
2017-08-24 12:29:08 +02:00
pluginsInstallation();
});
} else {
pluginsInstallation();
}
// Install default plugins and link dependencies.
2017-08-24 12:29:08 +02:00
function pluginsInstallation() {
// Define the list of default plugins.
const defaultPlugins = ['settings-manager', 'content-type-builder', 'content-manager'];
// Install each plugin.
defaultPlugins.forEach(defaultPlugin => {
try {
execSync(`strapi install ${defaultPlugin} ${scope.developerMode ? '--dev' : ''}`);
logger.info(`The plugin ${defaultPlugin} has been successfully installed.`);
} catch (error) {
logger.error(`An error occurred during ${defaultPlugin} plugin installation.`);
logger.error(error);
2017-08-24 12:29:08 +02:00
}
});
2017-08-24 12:29:08 +02:00
// Link dependencies.
availableDependencies.forEach(dependency => {
logger.info(`Linking \`${dependency.key}\` dependency to the project...`);
2017-08-24 12:29:08 +02:00
if (dependency.global) {
fs.symlinkSync(dependency.path, path.resolve(scope.rootPath, 'node_modules', dependency.key), 'dir');
} else {
fs.symlinkSync(path.resolve(scope.strapiRoot, 'node_modules', dependency.key), path.resolve(scope.rootPath, 'node_modules', dependency.key), 'dir');
}
});
logger.info('Your new application `' + scope.name + '` is ready at `' + scope.rootPath + '`.');
2017-08-17 16:46:27 +02:00
cb();
}
2016-03-18 11:12:50 +01:00
};