108 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-04-26 09:07:40 +02:00
import { join, resolve, basename } from 'node:path';
import { readFileSync } from 'node:fs';
import os from 'node:os';
import readline from 'node:readline';
import crypto from 'crypto';
import * as sentry from '@sentry/node';
import hasYarn from './utils/has-yarn';
import checkRequirements from './utils/check-requirements';
import { trackError, captureException } from './utils/usage';
import parseDatabaseArguments from './utils/parse-db-arguments';
import generateNew from './generate-new';
import machineID from './utils/machine-id';
import type { Scope, NewOptions } from './types';
2016-03-18 11:12:50 +01:00
2023-04-26 09:07:40 +02:00
export { default as checkInstallPath } from './utils/check-install-path';
2016-03-18 11:12:50 +01:00
2019-09-26 12:33:36 +02:00
sentry.init({
dsn: 'https://841d2b2c9b4d4b43a4cde92794cb705a@sentry.io/1762059',
});
2023-04-26 09:07:40 +02:00
const packageJson = JSON.parse(readFileSync(resolve(__dirname, '../package.json'), 'utf8'));
export const generateNewApp = (projectDirectory: string, options: Partial<NewOptions>) => {
2019-10-14 16:14:34 +02:00
checkRequirements();
2019-06-20 15:45:14 +02:00
const rootPath = resolve(projectDirectory);
2019-01-27 16:55:55 +01:00
const tmpPath = join(os.tmpdir(), `strapi${crypto.randomBytes(6).toString('hex')}`);
2016-03-18 11:12:50 +01:00
2023-04-26 09:07:40 +02:00
const useNpm = options.useNpm !== undefined;
2019-07-02 17:38:06 +02:00
2023-04-26 09:07:40 +02:00
const scope: Scope = {
rootPath,
name: basename(rootPath),
2019-06-20 16:38:15 +02:00
// disable quickstart run app after creation
2023-04-26 09:07:40 +02:00
runQuickstartApp: options.run !== false,
// use pacakge version as strapiVersion (all packages have the same version);
2023-04-26 09:07:40 +02:00
strapiVersion: packageJson.version,
debug: options.debug !== undefined,
quick: options.quickstart,
template: options.template,
packageJsonStrapi: {
2023-04-26 09:07:40 +02:00
template: options.template,
starter: options.starter,
},
uuid: (process.env.STRAPI_UUID_PREFIX || '') + crypto.randomUUID(),
docker: process.env.DOCKER === 'true',
2022-01-11 11:49:44 +02:00
deviceId: machineID(),
tmpPath,
2019-07-02 17:38:06 +02:00
// use yarn if available and --use-npm isn't true
useYarn: !useNpm && hasYarn(),
installDependencies: true,
strapiDependencies: [
2021-04-29 13:51:12 +02:00
'@strapi/strapi',
'@strapi/plugin-users-permissions',
'@strapi/plugin-i18n',
],
additionalsDependencies: {},
2023-04-26 09:07:40 +02:00
useTypescript: Boolean(options.typescript),
};
2016-03-18 11:12:50 +01:00
2023-04-26 09:07:40 +02:00
sentry.configureScope(function configureScope(sentryScope) {
2019-09-26 12:33:36 +02:00
const tags = {
2022-08-24 11:17:38 +02:00
os: os.type(),
2022-08-26 18:35:19 +02:00
osPlatform: os.platform(),
2022-08-24 11:17:38 +02:00
osArch: os.arch(),
osRelease: os.release(),
version: scope.strapiVersion,
nodeVersion: process.versions.node,
docker: scope.docker,
2019-09-26 12:33:36 +02:00
};
2023-04-26 09:07:40 +02:00
(Object.keys(tags) as Array<keyof typeof tags>).forEach((tag) => {
2019-09-26 12:33:36 +02:00
sentryScope.setTag(tag, tags[tag]);
});
});
2023-04-26 09:07:40 +02:00
parseDatabaseArguments({ scope, args: options });
initCancelCatcher();
2022-08-08 23:33:39 +02:00
return generateNew(scope).catch((error) => {
2019-06-20 15:45:14 +02:00
console.error(error);
return captureException(error).then(() => {
2019-09-26 12:33:36 +02:00
return trackError({ scope, error }).then(() => {
process.exit(1);
});
});
});
2019-06-20 15:45:14 +02:00
};
2019-06-20 18:04:09 +02:00
function initCancelCatcher() {
// Create interface for windows user to let them quit the program.
if (process.platform === 'win32') {
2023-04-26 09:07:40 +02:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
2022-08-08 23:33:39 +02:00
rl.on('SIGINT', function sigint() {
process.emit('SIGINT');
});
}
process.on('SIGINT', () => {
2019-06-20 18:04:09 +02:00
process.exit(1);
});
}