const fs = require('fs'); const replaceInFiles = require('replace-in-file'); const { execSync } = require('child_process'); const argv = require('minimist')(process.argv.slice(2)); // questions const appName = argv.name ?? 'app'; const appPort = argv.port ?? '0'; const defaultContextDir = '.'; const rootDir = __dirname; const contextDir = `${argv._[0] ?? defaultContextDir}`; // create app folder const appsDir = `${contextDir}/apps`; const uiDir = `${contextDir}/apps/${appName}-ui`; const restDir = `${contextDir}/apps/${appName}-rest`; const testDir = `${contextDir}/apps/${appName}-e2e`; // prepare structure folders if (!fs.existsSync(uiDir)) { if (!fs.existsSync(contextDir)) { fs.mkdirSync(contextDir); } fs.mkdirSync(appsDir); fs.mkdirSync(uiDir); fs.mkdirSync(`${uiDir}/src`); fs.mkdirSync(restDir); fs.mkdirSync(`${restDir}/src`); fs.mkdirSync(testDir); fs.mkdirSync(`${testDir}/src`); } // copy folder content try { fs.cpSync(`${rootDir}/source/global/`, contextDir, { force: true, recursive: true, }); fs.cpSync(`${rootDir}/source/ui/`, uiDir, { force: true, recursive: true, }); fs.cpSync(`${rootDir}/source/rest/`, restDir, { force: true, recursive: true, }); fs.cpSync(`${rootDir}/source/e2e/`, testDir, { force: true, recursive: true, }); } catch (err) { console.error(err); } // rename internal files if (contextDir === defaultContextDir) { fs.unlinkSync(`${contextDir}/.npmrc`); } fs.renameSync(`${contextDir}/npmrc`, `${contextDir}/.npmrc`); fs.renameSync(`${contextDir}/gitignore`, `${contextDir}/.gitignore`); fs.renameSync(`${contextDir}/prettierrc`, `${contextDir}/.prettierrc`); fs.renameSync(`${contextDir}/.tsconfig.json`, `${contextDir}/tsconfig.json`); fs.renameSync(`${uiDir}/.tsconfig.json`, `${uiDir}/tsconfig.json`); fs.renameSync(`${restDir}/.tsconfig.json`, `${restDir}/tsconfig.json`); fs.renameSync(`${testDir}/.tsconfig.json`, `${testDir}/tsconfig.json`); fs.renameSync(`${testDir}/src/.tsconfig.json`, `${testDir}/src/tsconfig.json`); // replace in files replaceInFiles.sync({ files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`], from: /\/\/.@ts-nocheck\n/gm, to: '', }); replaceInFiles.sync({ files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`], from: /\$\(appName\)/gm, to: appName, }); replaceInFiles.sync({ files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`], from: /\$\(AppName\)/gm, to: appName.charAt(0).toUpperCase() + appName.slice(1), }); replaceInFiles.sync({ files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`], from: /\$\(appPort\)/gm, to: appPort, }); // install deps execSync(`pnpm i`, { cwd: `${contextDir}` });