133 lines
3.3 KiB
JavaScript
Raw Normal View History

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');
2019-06-20 16:38:15 +02:00
2019-06-20 18:04:09 +02:00
const stopProcess = require('./utils/stop-process');
2019-06-20 16:38:15 +02:00
const { trackUsage } = require('./utils/usage');
const packageJSON = require('./resources/json/package.json');
const databaseJSON = require('./resources/json/database.json.js');
module.exports = async function createProject(
scope,
{ 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);
// copy templates
await fse.writeJSON(
join(rootPath, 'package.json'),
packageJSON({
strapiDependencies: scope.strapiDependencies,
additionalsDependencies: dependencies,
strapiVersion: scope.strapiVersion,
projectName: scope.name,
uuid: scope.uuid,
}),
{
spaces: 2,
}
);
// ensure node_modules is created
await fse.ensureDir(join(rootPath, 'node_modules'));
await Promise.all(
['development', 'staging', 'production'].map(env => {
return fse.writeJSON(
join(rootPath, `config/environments/${env}/database.json`),
databaseJSON({
connection,
env,
}),
{ spaces: 2 }
);
})
);
} catch (err) {
await fse.remove(scope.rootPath);
throw err;
}
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-06-20 18:04:09 +02:00
const runner = runInstall(scope);
runner.stdout.on('data', logInstall);
runner.stderr.on('data', logInstall);
await runner;
loader.stop();
console.log(`Dependencies installed ${chalk.green('successfully')}.`);
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,
error,
});
2019-06-20 18:04:09 +02:00
stopProcess(
`${chalk.red(
'Error'
)} while installing dependencies:\n${error.stderr.trim()}`
);
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`);
console.log(' Starts 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`);
console.log(' Starts 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`);
console.log(' Builds Strapi admin panel.');
2019-06-20 16:38:15 +02:00
console.log();
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
}