strapi/scripts/setup.js

196 lines
6.4 KiB
JavaScript
Raw Normal View History

2017-03-14 12:25:39 +01:00
const shell = require('shelljs');
2018-04-17 10:30:30 +02:00
// Check npm version
const npm = shell.exec('npm -v').stdout;
if (parseFloat(npm) < 5) {
throw new Error('[ERROR: Strapi] You need npm version @>=5');
}
const nodeVersion = shell.exec('node -v').stdout.replace('v', '');
if (parseFloat(nodeVersion) < 8.6) {
throw new Error('[ERROR: Strapi] You need to use node version @>=9');
}
2017-03-14 12:25:39 +01:00
2017-08-09 22:21:08 +02:00
// Store installation start date.
const silent = process.env.npm_config_debug !== 'true';
2017-08-09 22:21:08 +02:00
const installationStartDate = new Date();
2018-04-17 11:43:47 +02:00
const watcher = (label, cmd, withSuccess = true) => {
if (label.length > 0) {
shell.echo(label);
}
const data = shell.exec(cmd, {
silent
});
if (data.stderr && data.code !== 0) {
console.error(data.stderr);
process.exit(1);
}
if (label.length > 0 && withSuccess) {
shell.echo('✅ Success');
shell.echo('');
}
2018-04-17 10:30:30 +02:00
};
2018-04-18 11:51:16 +02:00
const asyncWatcher = (label, cmd, withSuccess = true, resolve) => {
2018-04-17 11:43:47 +02:00
if (label.length > 0) {
shell.echo(label);
}
2018-04-18 11:51:16 +02:00
return shell.exec(cmd, { silent, async: true }, (code, stdout, stderr) => {
if (stderr && code !== 0) {
console.error(stderr);
2018-04-17 11:43:47 +02:00
process.exit(1);
}
2018-04-18 11:51:16 +02:00
return resolve();
2018-04-17 11:43:47 +02:00
});
};
shell.echo('');
shell.echo('🕓 The setup process can take few minutes.');
shell.echo('');
2017-08-09 22:21:08 +02:00
2017-03-14 12:25:39 +01:00
// Remove existing binary.
shell.rm('-f', '/usr/local/bin/strapi.js');
2018-05-02 15:31:08 +02:00
shell.cd('packages/strapi-utils');
watcher('📦 Linking strapi-utils...', 'npm link');
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-lint');
watcher('📦 Linking strapi-lint', 'npm link');
2017-07-17 16:48:09 +02:00
shell.cd('../strapi-generate');
watcher('', 'npm install ../strapi-utils');
watcher('📦 Linking strapi-generate...', 'npm link');
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-generate-api');
watcher('📦 Linking strapi-generate-api...', 'npm link');
shell.cd('../strapi-helper-plugin');
2018-04-30 16:04:29 +02:00
watcher('📦 Linking strapi-helper-plugin...', 'npm link');
2017-07-17 16:48:09 +02:00
shell.cd('../strapi-admin');
2018-01-10 15:38:28 +01:00
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
watcher('', 'npm install ../strapi-utils --no-optional');
shell.rm('-f', 'package-lock.json');
// Without these line Travis failed.
if (shell.test('-e', 'admin/src/config/plugins.json') === false) {
shell.config.silent = silent;
shell.cd('admin/src/config/');
shell.ShellString('[]').to('plugins.json');
shell.cd('../../../');
}
watcher('📦 Linking strapi-admin', 'npm link --no-optional', false);
2017-08-09 22:09:41 +02:00
2017-07-17 16:48:09 +02:00
shell.cd('../strapi-generate-admin');
watcher('', 'npm install ../strapi-admin');
watcher('📦 Linking strapi-generate-admin...', 'npm link');
2017-08-09 22:09:41 +02:00
2017-07-17 16:48:09 +02:00
shell.cd('../strapi-generate-new');
watcher('', 'npm install ../strapi-utils');
watcher('📦 Linking strapi-generate-new', 'npm link');
2017-08-09 22:09:41 +02:00
2017-07-17 16:48:09 +02:00
shell.cd('../strapi-mongoose');
watcher('', 'npm install ../strapi-utils');
watcher('📦 Linking strapi-mongoose...', 'npm link');
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-knex');
watcher('📦 Linking strapi-knex...', 'npm link');
shell.cd('../strapi-bookshelf');
watcher('', 'npm install ../strapi-utils');
watcher('', 'npm install ../strapi-knex');
watcher('📦 Linking strapi-bookshelf...', 'npm link');
2017-07-17 16:48:09 +02:00
shell.cd('../strapi');
2018-05-02 15:31:08 +02:00
watcher('', 'npm install ../strapi-generate ../strapi-generate-admin ../strapi-generate-api ../strapi-generate-new ../strapi-generate-plugin ../strapi-generate-policy ../strapi-generate-service ../strapi-utils');
watcher('📦 Linking strapi...', 'npm link');
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-plugin-graphql');
watcher('📦 Linking strapi-plugin-graphql...', 'npm link --no-optional', false);
2018-04-17 10:30:30 +02:00
// Upload plugins
shell.cd('../strapi-upload-local');
watcher('📦 Linking strapi-upload-local...', 'npm link --no-optional', false);
// Plugins with admin
2017-11-20 15:10:40 +01:00
shell.cd('../strapi-plugin-email');
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-email...', 'npm link --no-optional', false);
2017-11-20 15:10:40 +01:00
2017-11-14 10:11:16 +01:00
shell.cd('../strapi-plugin-users-permissions');
2018-01-10 15:38:28 +01:00
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
2017-11-14 10:11:16 +01:00
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-users-permissions...', 'npm link --no-optional', false);
2017-11-14 10:11:16 +01:00
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-plugin-content-manager');
2018-01-10 15:38:28 +01:00
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
2017-08-09 22:09:41 +02:00
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-content-manager...', 'npm link --no-optional', false);
2017-08-09 22:09:41 +02:00
shell.cd('../strapi-plugin-settings-manager');
2018-01-10 15:38:28 +01:00
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
2017-08-09 22:09:41 +02:00
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-settings-manager...', 'npm link --no-optional', false);
2018-02-20 15:57:34 +01:00
2018-04-17 10:30:30 +02:00
// Plugins with admin and other plugin's dependencies
2018-02-23 15:14:21 +01:00
shell.cd('../strapi-plugin-upload');
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
2018-02-23 15:35:46 +01:00
watcher('', 'npm install ../strapi-upload-local --no-optional');
2018-02-23 15:14:21 +01:00
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-upload...', 'npm link --no-optional', false);
shell.cd('../strapi-plugin-content-type-builder');
2018-01-10 15:38:28 +01:00
watcher('', 'npm install ../strapi-helper-plugin --no-optional');
watcher('', 'npm install ../strapi-generate --no-optional');
watcher('', 'npm install ../strapi-generate-api --no-optional');
shell.rm('-f', 'package-lock.json');
watcher('📦 Linking strapi-plugin-content-type-builder...', 'npm link --no-optional', false);
2018-04-17 10:30:30 +02:00
2018-05-03 18:13:22 +02:00
2018-04-18 14:09:59 +02:00
const pluginsToBuild = ['admin', 'content-manager', 'content-type-builder', 'upload', 'users-permissions', 'settings-manager'];
2018-04-17 10:30:30 +02:00
2018-04-17 11:43:47 +02:00
const buildPlugins = async () => {
const build = (pckgName) => {
2018-04-18 11:51:16 +02:00
return new Promise(resolve => {
2018-04-17 11:43:47 +02:00
const name = pckgName === 'admin' ? pckgName: `plugin-${pckgName}`;
asyncWatcher(`🏗 Building ${name}...`, `cd ../strapi-${name} && IS_MONOREPO=true npm run build`, false, resolve);
2018-04-17 11:43:47 +02:00
});
};
return Promise.all(pluginsToBuild.map(plugin => build(plugin)));
};
const setup = async () => {
if (process.env.npm_config_build) {
if (process.platform === 'darwin') { // Allow async build for darwin platform
await buildPlugins();
} else {
pluginsToBuild.map(name => {
const pluginName = name === 'admin' ? name : `plugin-${name}`;
shell.cd(`../strapi-${pluginName}`);
return watcher(`🏗 Building ${pluginName}...`, 'IS_MONOREPO=true npm run build');
2018-04-17 11:43:47 +02:00
});
}
}
// Log installation duration.
const installationEndDate = new Date();
const duration = (installationEndDate.getTime() - installationStartDate.getTime()) / 1000;
shell.echo('✅ Strapi has been succesfully installed.');
shell.echo(`⏳ The installation took ${Math.floor(duration / 60) > 0 ? `${Math.floor(duration / 60)} minutes and ` : ''}${Math.floor(duration % 60)} seconds.`);
};
2018-04-18 14:09:59 +02:00
setup();