200 lines
6.0 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');
2018-06-22 15:25:15 +02:00
const { exec, execSync } = require('child_process');
2016-03-18 11:12:50 +01:00
// Public node modules.
const _ = require('lodash');
2018-06-22 15:25:15 +02:00
const {green, cyan} = require('chalk');
2016-03-18 11:12:50 +01:00
const fs = require('fs-extra');
const npm = require('enpeem');
2018-06-22 15:25:15 +02:00
const ora = require('ora');
2018-06-21 15:07:50 +02:00
const shell = require('shelljs');
2016-03-18 11:12:50 +01:00
// Logger.
const { packageManager } = require('strapi-utils');
2016-03-18 11:12:50 +01:00
/**
* Runs after this generator has finished
*
* @param {Object} scope
* @param {Function} cb
*/
2018-05-04 17:50:39 +02:00
/* eslint-disable no-console */
/* eslint-disable prefer-template */
module.exports = (scope, cb) => {
2018-06-22 15:25:15 +02:00
console.log(`The app has been connected to the database ${green('successfully')}!`);
console.log();
console.log('🏗 Application generation:');
let loader = ora('Copy dashboard').start();
const packageJSON = require(path.resolve(scope.rootPath, 'package.json'));
2018-05-04 17:50:39 +02:00
// 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));
2018-06-22 15:25:15 +02:00
loader.succeed();
let 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);
2018-06-22 16:46:48 +02:00
// Add this check to know if we are in development mode so the creation is faster.
const isStrapiInstalledWithNPM = packageManager.isStrapiInstalledWithNPM();
2018-07-10 11:08:58 +02:00
const globalRootPath = execSync(packageManager.commands('root -g'));
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 {
2018-06-21 15:07:50 +02:00
const depPath = isStrapiInstalledWithNPM ? path.resolve(_.trim(globalRootPath.toString()), key) : path.resolve(_.trim(globalRootPath.toString()), `node_modules/${key}`);
fs.accessSync(depPath, fs.constants.R_OK | fs.constants.F_OK);
2017-08-24 12:29:08 +02:00
2017-08-17 16:46:27 +02:00
availableDependencies.push({
key,
2017-08-24 12:29:08 +02:00
global: true,
2018-06-21 15:07:50 +02:00
path: depPath
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
if (!_.isEmpty(othersDependencies)) {
2018-06-21 15:07:50 +02:00
if (isStrapiInstalledWithNPM) {
npm.install({
dir: scope.rootPath,
dependencies: othersDependencies,
loglevel: 'silent',
production: true,
'cache-min': 999999999
}, err => {
if (err) {
console.log();
console.log('⚠️ You should run `npm install` into your application before starting it.');
2018-06-21 15:07:50 +02:00
console.log();
console.log('⚠️ Some dependencies could not be installed:');
_.forEach(othersDependencies, value => console.log('• ' + value));
2018-06-21 15:07:50 +02:00
console.log();
return cb();
}
pluginsInstallation();
});
} else {
const alphaDependencies = othersDependencies.map(dep => {
if (_.includes(dep, 'strapi') && !_.includes(dep, '@alpha')) { // We need this for yarn
return `${dep}@alpha`;
}
return dep;
}).join(' ');
const data = shell.exec(`yarn --cwd ${scope.rootPath} add ${alphaDependencies} --production`, { silent: true });
if (data.stderr && data.code !== 0) {
cb();
}
2017-08-24 12:29:08 +02:00
pluginsInstallation();
2018-06-21 15:07:50 +02:00
}
2017-08-24 12:29:08 +02:00
} else {
pluginsInstallation();
}
// Install default plugins and link dependencies.
2017-08-24 12:29:08 +02:00
function pluginsInstallation() {
2018-06-21 15:07:50 +02:00
const strapiBin = path.join(scope.strapiRoot, scope.strapiPackageJSON.bin.strapi);
// Define the list of default plugins.
const defaultPlugins = [{
name: 'settings-manager',
core: true
}, {
name: 'content-type-builder',
core: true
}, {
name: 'content-manager',
core: true
}, {
name: 'users-permissions',
core: true
}, {
name: 'email',
core: true
},{
name: 'upload',
core: true
}];
2018-06-22 15:25:15 +02:00
let installPlugin = new Promise(resolve => {
return resolve();
});
2017-08-24 12:29:08 +02:00
2018-06-22 15:25:15 +02:00
// Install each plugin.
defaultPlugins.forEach(defaultPlugin => {
installPlugin = installPlugin.then(() => {
return new Promise(resolve => {
loader = ora(`Install plugin ${cyan(defaultPlugin.name)}.`).start();
exec(`node ${strapiBin} install ${defaultPlugin.name} ${scope.developerMode && defaultPlugin.core ? '--dev' : ''}`, (err) => {
if (err) {
loader.warn(`An error occurred during ${defaultPlugin.name} plugin installation.`);
console.log(err);
return resolve();
}
loader.succeed();
return resolve();
});
});
});
});
2018-06-22 15:25:15 +02:00
installPlugin
.then(() => {
// Link dependencies.
availableDependencies.forEach(dependency => {
loader = ora(`Link ${cyan(dependency.key)} dependency to the project.`).start();
if (dependency.global) {
try {
fs.accessSync(dependency.path, fs.constants.R_OK | fs.constants.F_OK);
fs.symlinkSync(dependency.path, path.resolve(scope.rootPath, 'node_modules', dependency.key), 'dir');
} catch (e) {
// Silent.
}
} else {
try {
fs.accessSync(path.resolve(scope.strapiRoot, 'node_modules', dependency.key), fs.constants.R_OK | fs.constants.F_OK);
fs.symlinkSync(path.resolve(scope.strapiRoot, 'node_modules', dependency.key), path.resolve(scope.rootPath, 'node_modules', dependency.key), 'dir');
} catch (e) {
// Silent.
}
}
loader.succeed();
});
2017-08-17 16:46:27 +02:00
2018-06-22 15:25:15 +02:00
console.log();
console.log(`👌 Your new application ${green(scope.name)} is ready at ${cyan(scope.rootPath)}.`);
console.log();
2018-08-08 11:44:27 +02:00
console.log('⚡️ Change directory:');
2018-06-22 15:25:15 +02:00
console.log(`$ ${green(`cd ${scope.name}`)}`);
console.log();
2018-08-08 11:44:27 +02:00
console.log('⚡️ Start application:');
2018-06-22 15:25:15 +02:00
console.log(`$ ${green('strapi start')}`);
2018-06-07 11:59:38 +03:00
2018-06-22 15:25:15 +02:00
cb();
});
}
2016-03-18 11:12:50 +01:00
};