2019-06-20 16:38:15 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { join } = require('path');
|
|
|
|
const fse = require('fs-extra');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const execa = require('execa');
|
2019-06-20 18:04:09 +02:00
|
|
|
const ora = require('ora');
|
2020-01-07 23:10:00 +05:30
|
|
|
const _ = require('lodash');
|
2019-06-20 16:38:15 +02:00
|
|
|
|
2019-06-20 18:04:09 +02:00
|
|
|
const stopProcess = require('./utils/stop-process');
|
2019-10-15 16:21:59 +02:00
|
|
|
const { trackUsage, captureStderr } = require('./utils/usage');
|
2019-06-20 16:38:15 +02:00
|
|
|
const packageJSON = require('./resources/json/package.json');
|
|
|
|
const databaseJSON = require('./resources/json/database.json.js');
|
|
|
|
|
2020-04-21 18:24:15 +02:00
|
|
|
module.exports = async function createProject(scope, { client, connection, dependencies }) {
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log('Creating files.');
|
2019-06-20 16:38:15 +02:00
|
|
|
|
|
|
|
const { rootPath } = scope;
|
|
|
|
const resources = join(__dirname, 'resources');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// copy files
|
|
|
|
await fse.copy(join(resources, 'files'), rootPath);
|
|
|
|
|
2019-07-15 17:00:49 +02:00
|
|
|
// copy dot files
|
|
|
|
const dotFiles = await fse.readdir(join(resources, 'dot-files'));
|
|
|
|
await Promise.all(
|
|
|
|
dotFiles.map(name => {
|
2020-04-17 12:30:51 +02:00
|
|
|
return fse.copy(join(resources, 'dot-files', name), join(rootPath, `.${name}`));
|
2019-07-15 17:00:49 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-04-17 12:30:51 +02:00
|
|
|
await trackUsage({ event: 'didCopyProjectFiles', scope });
|
|
|
|
|
2019-06-20 16:38:15 +02:00
|
|
|
// copy templates
|
|
|
|
await fse.writeJSON(
|
|
|
|
join(rootPath, 'package.json'),
|
|
|
|
packageJSON({
|
|
|
|
strapiDependencies: scope.strapiDependencies,
|
|
|
|
additionalsDependencies: dependencies,
|
|
|
|
strapiVersion: scope.strapiVersion,
|
2020-01-07 23:10:00 +05:30
|
|
|
projectName: _.kebabCase(scope.name),
|
2019-06-20 16:38:15 +02:00
|
|
|
uuid: scope.uuid,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
spaces: 2,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-17 12:30:51 +02:00
|
|
|
await trackUsage({ event: 'didWritePackageJSON', scope });
|
|
|
|
|
2019-06-20 16:38:15 +02:00
|
|
|
// ensure node_modules is created
|
|
|
|
await fse.ensureDir(join(rootPath, 'node_modules'));
|
|
|
|
|
2020-04-06 17:39:48 +02:00
|
|
|
await fse.writeFile(
|
|
|
|
join(rootPath, `config/database.js`),
|
|
|
|
databaseJSON({
|
2020-04-21 18:24:15 +02:00
|
|
|
client,
|
2020-04-06 17:39:48 +02:00
|
|
|
connection,
|
2019-06-20 16:38:15 +02:00
|
|
|
})
|
|
|
|
);
|
2020-04-17 12:30:51 +02:00
|
|
|
|
|
|
|
await trackUsage({ event: 'didCopyConfigurationFiles', scope });
|
2019-06-20 16:38:15 +02:00
|
|
|
} catch (err) {
|
|
|
|
await fse.remove(scope.rootPath);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:30:51 +02:00
|
|
|
await trackUsage({ event: 'willInstallProjectDependencies', scope });
|
|
|
|
|
2019-06-20 18:04:09 +02:00
|
|
|
const installPrefix = chalk.yellow('Installing dependencies:');
|
|
|
|
const loader = ora(installPrefix).start();
|
|
|
|
|
|
|
|
const logInstall = (chunk = '') => {
|
|
|
|
loader.text = `${installPrefix} ${chunk
|
|
|
|
.toString()
|
|
|
|
.split('\n')
|
|
|
|
.join(' ')}`;
|
|
|
|
};
|
2019-06-20 16:38:15 +02:00
|
|
|
|
|
|
|
try {
|
2019-07-18 16:25:20 +02:00
|
|
|
if (scope.installDependencies !== false) {
|
|
|
|
const runner = runInstall(scope);
|
2019-06-20 18:04:09 +02:00
|
|
|
|
2019-07-18 16:25:20 +02:00
|
|
|
runner.stdout.on('data', logInstall);
|
|
|
|
runner.stderr.on('data', logInstall);
|
2019-06-20 18:04:09 +02:00
|
|
|
|
2019-07-18 16:25:20 +02:00
|
|
|
await runner;
|
|
|
|
}
|
2019-06-20 18:04:09 +02:00
|
|
|
|
|
|
|
loader.stop();
|
|
|
|
console.log(`Dependencies installed ${chalk.green('successfully')}.`);
|
2020-04-17 12:30:51 +02:00
|
|
|
|
|
|
|
await trackUsage({ event: 'didInstallProjectDependencies', scope });
|
2019-06-20 16:38:15 +02:00
|
|
|
} catch (error) {
|
2019-06-20 18:04:09 +02:00
|
|
|
loader.stop();
|
2019-06-20 16:38:15 +02:00
|
|
|
await trackUsage({
|
|
|
|
event: 'didNotInstallProjectDependencies',
|
|
|
|
scope,
|
2019-09-26 12:33:36 +02:00
|
|
|
error: error.stderr.slice(-1024),
|
2019-06-20 16:38:15 +02:00
|
|
|
});
|
2019-06-20 18:04:09 +02:00
|
|
|
|
2019-10-15 16:54:23 +02:00
|
|
|
console.error(`${chalk.red('Error')} while installing dependencies:`);
|
|
|
|
console.error(error.stderr);
|
2019-10-08 08:56:51 +02:00
|
|
|
|
2019-10-15 16:21:59 +02:00
|
|
|
await captureStderr('didNotInstallProjectDependencies', error);
|
2019-09-26 12:33:36 +02:00
|
|
|
|
2019-10-29 08:54:46 +01:00
|
|
|
console.log(chalk.black.bgWhite(' Keep trying! '));
|
|
|
|
console.log();
|
|
|
|
console.log(
|
|
|
|
chalk.bold(
|
|
|
|
'Oh, it seems that you encountered errors while installing dependencies in your project.'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
console.log(`Don't give up, your project was created correctly.`);
|
|
|
|
console.log(
|
|
|
|
`Fix the issues mentionned in the installation errors and try to run the following command:`
|
|
|
|
);
|
|
|
|
console.log();
|
|
|
|
console.log(
|
2020-04-17 12:30:51 +02:00
|
|
|
`cd ${chalk.green(rootPath)} && ${chalk.cyan(scope.useYarn ? 'yarn' : 'npm')} install`
|
2019-10-29 08:54:46 +01:00
|
|
|
);
|
|
|
|
console.log();
|
|
|
|
|
|
|
|
stopProcess();
|
2019-06-20 16:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await trackUsage({ event: 'didCreateProject', scope });
|
|
|
|
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log();
|
2019-06-20 16:38:15 +02:00
|
|
|
console.log(`Your application was created at ${chalk.green(rootPath)}.\n`);
|
|
|
|
|
2019-07-02 17:38:06 +02:00
|
|
|
const cmd = chalk.cyan(scope.useYarn ? 'yarn' : 'npm run');
|
2019-06-20 16:38:15 +02:00
|
|
|
|
|
|
|
console.log('Available commands in your project:');
|
|
|
|
console.log();
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log(` ${cmd} develop`);
|
2019-07-08 16:13:36 +02:00
|
|
|
console.log(' Start Strapi in watch mode.');
|
2019-06-20 16:38:15 +02:00
|
|
|
console.log();
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log(` ${cmd} start`);
|
2019-07-08 16:13:36 +02:00
|
|
|
console.log(' Start Strapi without watch mode.');
|
2019-06-20 16:38:15 +02:00
|
|
|
console.log();
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log(` ${cmd} build`);
|
2019-07-08 16:13:36 +02:00
|
|
|
console.log(' Build Strapi admin panel.');
|
2019-06-20 16:38:15 +02:00
|
|
|
console.log();
|
2019-07-03 11:20:14 +02:00
|
|
|
console.log(` ${cmd} strapi`);
|
|
|
|
console.log(` Display all available commands.`);
|
|
|
|
console.log();
|
2019-06-20 16:38:15 +02:00
|
|
|
console.log('You can start by doing:');
|
|
|
|
console.log();
|
2019-06-20 18:04:09 +02:00
|
|
|
console.log(` ${chalk.cyan('cd')} ${rootPath}`);
|
|
|
|
console.log(` ${cmd} develop`);
|
|
|
|
console.log();
|
2019-06-20 16:38:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const installArguments = ['install', '--production', '--no-optional'];
|
2019-07-02 17:38:06 +02:00
|
|
|
function runInstall({ rootPath, useYarn }) {
|
|
|
|
if (useYarn) {
|
2019-06-20 16:38:15 +02:00
|
|
|
return execa('yarnpkg', installArguments, {
|
|
|
|
cwd: rootPath,
|
2019-06-20 18:04:09 +02:00
|
|
|
stdin: 'ignore',
|
2019-06-20 16:38:15 +02:00
|
|
|
});
|
|
|
|
}
|
2019-06-20 18:04:09 +02:00
|
|
|
|
|
|
|
return execa('npm', installArguments, { cwd: rootPath, stdin: 'ignore' });
|
2019-06-20 16:38:15 +02:00
|
|
|
}
|