seedling/create.js

93 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

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';
2023-12-22 17:48:27 +00:00
const defaultContextDir = '.';
const rootDir = __dirname;
2023-12-22 17:48:27 +00:00
const contextDir = `${argv._[0] ?? defaultContextDir}`;
// create app folder
const appsDir = `${contextDir}/apps`;
const uiDir = `${contextDir}/apps/${appName}-ui`;
2023-12-22 17:48:27 +00:00
const restDir = `${contextDir}/apps/${appName}-rest`;
2024-01-10 21:08:53 +00:00
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`);
2023-12-22 17:48:27 +00:00
fs.mkdirSync(restDir);
fs.mkdirSync(`${restDir}/src`);
2023-12-28 21:58:08 +00:00
fs.mkdirSync(testDir);
fs.mkdirSync(`${testDir}/src`);
}
// copy folder content
try {
2024-01-10 21:08:53 +00:00
fs.cpSync(`${rootDir}/source/global/`, contextDir, {
force: true,
recursive: true,
});
fs.cpSync(`${rootDir}/source/ui/`, uiDir, {
force: true,
recursive: true,
});
2023-12-22 17:48:27 +00:00
fs.cpSync(`${rootDir}/source/rest/`, restDir, {
force: true,
recursive: true,
});
2024-01-10 21:08:53 +00:00
fs.cpSync(`${rootDir}/source/e2e/`, testDir, {
2023-12-28 21:58:08 +00:00
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`);
2023-12-22 17:48:27 +00:00
fs.renameSync(`${restDir}/.tsconfig.json`, `${restDir}/tsconfig.json`);
2023-12-28 21:58:08 +00:00
fs.renameSync(`${testDir}/.tsconfig.json`, `${testDir}/tsconfig.json`);
fs.renameSync(`${testDir}/src/.tsconfig.json`, `${testDir}/src/tsconfig.json`);
// replace in files
replaceInFiles.sync({
2023-12-22 17:48:27 +00:00
files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`],
from: /\/\/.@ts-nocheck\n/gm,
to: '',
});
replaceInFiles.sync({
2023-12-22 17:48:27 +00:00
files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`],
from: /\$\(appName\)/gm,
to: appName,
});
replaceInFiles.sync({
2023-12-22 17:48:27 +00:00
files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`],
from: /\$\(AppName\)/gm,
to: appName.charAt(0).toUpperCase() + appName.slice(1),
});
replaceInFiles.sync({
2023-12-22 17:48:27 +00:00
files: [`${contextDir}/**/*`, `${contextDir}/.devcontainer/*`],
from: /\$\(appPort\)/gm,
to: appPort,
});
// install deps
2023-12-22 17:48:27 +00:00
execSync(`pnpm i`, { cwd: `${contextDir}` });