create-strapi-app
Version:
Generate a new Strapi application.
170 lines (166 loc) • 8 kB
JavaScript
var path = require('node:path');
var os = require('node:os');
var chalk = require('chalk');
var commander = require('commander');
var crypto = require('crypto');
var fse = require('fs-extra');
var prompts = require('./prompts.js');
var cloud = require('./cloud.js');
var createStrapi = require('./create-strapi.js');
var checkRequirements = require('./utils/check-requirements.js');
var checkInstallPath = require('./utils/check-install-path.js');
var machineId = require('./utils/machine-id.js');
var usage = require('./utils/usage.js');
var database = require('./utils/database.js');
var logger = require('./utils/logger.js');
const { version } = fse.readJSONSync(path.join(__dirname, '..', 'package.json'));
const command = new commander.Command('create-strapi-app').version(version).arguments('[directory]').usage('[directory] [options]').option('--quickstart', 'Quickstart app creation (deprecated)').option('--no-run', 'Do not start the application after it is created.')// setup options
.option('--ts, --typescript', 'Initialize the project with TypeScript (default)').option('--js, --javascript', 'Initialize the project with Javascript')// Package manager options
.option('--use-npm', 'Use npm as the project package manager').option('--use-yarn', 'Use yarn as the project package manager').option('--use-pnpm', 'Use pnpm as the project package manager')// dependencies options
.option('--install', 'Install dependencies').option('--no-install', 'Do not install dependencies')// Cloud options
.option('--skip-cloud', 'Skip cloud login and project creation')// Example app
.option('--example', 'Use an example app').option('--no-example', 'Do not use an example app')// git options
.option('--git-init', 'Initialize a git repository').option('--no-git-init', 'Do no initialize a git repository')// Database options
.option('--dbclient <dbclient>', 'Database client').option('--dbhost <dbhost>', 'Database host').option('--dbport <dbport>', 'Database port').option('--dbname <dbname>', 'Database name').option('--dbusername <dbusername>', 'Database username').option('--dbpassword <dbpassword>', 'Database password').option('--dbssl <dbssl>', 'Database SSL').option('--dbfile <dbfile>', 'Database file path for sqlite').option('--skip-db', 'Skip database configuration').option('--template <template>', 'Specify a Strapi template').option('--template-branch <templateBranch>', 'Specify a branch for the template').option('--template-path <templatePath>', 'Specify a path to the template inside the repository').description('create a new application');
async function run(args) {
const options = command.parse(args).opts();
const directory = command.args[0];
logger.logger.title('Strapi', `${chalk.green(chalk.bold(`v${version}`))} ${chalk.bold("🚀 Let's create your new project")}\n`);
if ((options.javascript !== undefined || options.typescript !== undefined) && options.template !== undefined) {
logger.logger.fatal(`You cannot use ${chalk.bold('--javascript')} or ${chalk.bold('--typescript')} with ${chalk.bold('--template')}`);
}
if (options.javascript === true && options.typescript === true) {
logger.logger.fatal(`You cannot use both ${chalk.bold('--typescript')} (--ts) and ${chalk.bold('--javascript')} (--js) flags together`);
}
// Only prompt the example app option if there is no template option
if (options.example === true && options.template !== undefined) {
logger.logger.fatal(`You cannot use ${chalk.bold('--example')} with ${chalk.bold('--template')}`);
}
if (options.template !== undefined && options.template.startsWith('-')) {
logger.logger.fatal(`Template name ${chalk.bold(`"${options.template}"`)} is invalid`);
}
if ([
options.useNpm,
options.usePnpm,
options.useYarn
].filter(Boolean).length > 1) {
logger.logger.fatal(`You cannot specify multiple package managers at the same time ${chalk.bold('(--use-npm, --use-pnpm, --use-yarn)')}`);
}
if (options.quickstart && !directory) {
logger.logger.fatal(`Please specify the ${chalk.bold('<directory>')} of your project when using ${chalk.bold('--quickstart')}`);
}
checkRequirements.checkNodeRequirements();
const appDirectory = directory || await prompts.directory();
const rootPath = await checkInstallPath.checkInstallPath(appDirectory);
if (!options.skipCloud) {
await cloud.handleCloudLogin();
}
const tmpPath = path.join(os.tmpdir(), `strapi${crypto.randomBytes(6).toString('hex')}`);
const scope = {
rootPath,
name: path.basename(rootPath),
packageManager: getPkgManager(options),
database: await database.getDatabaseInfos(options),
template: options.template,
templateBranch: options.templateBranch,
templatePath: options.templatePath,
isQuickstart: options.quickstart,
useExample: false,
runApp: options.quickstart === true && options.run !== false,
strapiVersion: version,
packageJsonStrapi: {
template: options.template
},
uuid: (process.env.STRAPI_UUID_PREFIX || '') + crypto.randomUUID(),
docker: process.env.DOCKER === 'true',
deviceId: machineId.machineID(),
tmpPath,
gitInit: true,
devDependencies: {},
dependencies: {
'@strapi/strapi': version,
'@strapi/plugin-users-permissions': version,
'@strapi/plugin-cloud': version,
// third party
react: '^18.0.0',
'react-dom': '^18.0.0',
'react-router-dom': '^6.0.0',
'styled-components': '^6.0.0'
}
};
if (options.template !== undefined) {
scope.useExample = false;
} else if (options.example === true) {
scope.useExample = true;
} else if (options.example === false || options.quickstart === true) {
scope.useExample = false;
} else {
scope.useExample = await prompts.example();
}
if (options.javascript === true) {
scope.useTypescript = false;
} else if (options.typescript === true || options.quickstart) {
scope.useTypescript = true;
} else if (!options.template) {
scope.useTypescript = await prompts.typescript();
}
if (options.install === true || options.quickstart) {
scope.installDependencies = true;
} else if (options.install === false) {
scope.installDependencies = false;
} else {
scope.installDependencies = await prompts.installDependencies(scope.packageManager);
}
if (scope.useTypescript) {
scope.devDependencies = {
...scope.devDependencies,
typescript: '^5',
'@types/node': '^20',
'@types/react': '^18',
'@types/react-dom': '^18'
};
}
if (options.gitInit === true || options.quickstart) {
scope.gitInit = true;
} else if (options.gitInit === false) {
scope.gitInit = false;
} else {
scope.gitInit = await prompts.gitInit();
}
database.addDatabaseDependencies(scope);
try {
await createStrapi.createStrapi(scope);
} catch (error) {
if (!(error instanceof Error)) {
throw error;
}
await usage.trackError({
scope,
error
});
logger.logger.fatal(error.message);
}
}
function getPkgManager(options) {
if (options.useNpm === true) {
return 'npm';
}
if (options.usePnpm === true) {
return 'pnpm';
}
if (options.useYarn === true) {
return 'yarn';
}
const userAgent = process.env.npm_config_user_agent || '';
if (userAgent.startsWith('yarn')) {
return 'yarn';
}
if (userAgent.startsWith('pnpm')) {
return 'pnpm';
}
return 'npm';
}
exports.createStrapi = createStrapi.createStrapi;
exports.run = run;
//# sourceMappingURL=index.js.map
;