seedling/create.js

84 lines
2.1 KiB
JavaScript
Raw 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';
const defaultContextDir = './';
const rootDir = __dirname;
const contextDir = `${argv._[0]}/` ?? defaultContextDir;
// create app folder
const appsDir = `${contextDir}/apps`;
const uiDir = `${contextDir}/apps/${appName}-ui`;
2023-10-24 20:43:21 +02:00
const apiDir = `${contextDir}/apps/${appName}-rest`;
// 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(apiDir);
fs.mkdirSync(`${apiDir}/src`);
}
// copy folder content
try {
2023-10-24 20:43:21 +02:00
fs.cpSync(`${rootDir}/source/common/`, contextDir, {
force: true,
recursive: true,
});
fs.cpSync(`${rootDir}/source/ui/`, uiDir, {
force: true,
recursive: true,
});
2023-10-24 20:43:21 +02:00
fs.cpSync(`${rootDir}/source/rest/`, apiDir, {
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(`${apiDir}/.tsconfig.json`, `${apiDir}/tsconfig.json`);
// replace in files
replaceInFiles.sync({
files: `./${contextDir}/**/*.{ts,tsx}`,
from: /\/\/.\@ts\-nocheck\n/gm,
to: '',
});
replaceInFiles.sync({
files: `./${contextDir}/**/*`,
from: /\$\(appName\)/gm,
to: appName,
});
replaceInFiles.sync({
files: `./${contextDir}/**/*`,
from: /\$\(AppName\)/gm,
to: appName.charAt(0).toUpperCase() + appName.slice(1),
});
replaceInFiles.sync({
files: `./${contextDir}/**/*`,
from: /\$\(appPort\)/gm,
to: appPort,
});
// install deps
execSync(`pnpm i`, { cwd: `./${contextDir}` })