2023-07-19 15:38:42 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
packages: {
|
2023-07-19 15:50:35 +03:00
|
|
|
dev: [
|
|
|
|
"@types/node",
|
2023-08-16 17:46:37 +03:00
|
|
|
"tsc-watch",
|
2023-07-19 15:50:35 +03:00
|
|
|
"typescript",
|
|
|
|
"eslint",
|
|
|
|
"@typescript-eslint/parser",
|
|
|
|
"@typescript-eslint/eslint-plugin",
|
|
|
|
],
|
2023-08-21 17:56:50 +03:00
|
|
|
dep: [],
|
2023-07-19 15:38:42 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-08-16 17:46:37 +03:00
|
|
|
const fs = require("fs-extra");
|
2023-07-19 15:38:42 +03:00
|
|
|
const path = require("path");
|
2023-08-16 17:46:37 +03:00
|
|
|
const { glob } = require("glob");
|
|
|
|
const Handlebars = require("handlebars");
|
2023-08-27 13:56:26 +03:00
|
|
|
const p = require("@clack/prompts");
|
|
|
|
const runTasks = require("./runTasks");
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-07-20 21:06:26 +03:00
|
|
|
async function run() {
|
|
|
|
const { execa } = await import("execa");
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-08-27 13:56:26 +03:00
|
|
|
p.intro("Welcome to create-ts-prod");
|
|
|
|
|
|
|
|
const resp = await p.group(
|
2023-07-20 21:06:26 +03:00
|
|
|
{
|
2023-08-27 13:56:26 +03:00
|
|
|
prisma: () => p.confirm({ message: "Do you want to install Prisma?" }),
|
|
|
|
dotenv: () => p.confirm({ message: "Do you want to install dotenv?" }),
|
2023-07-20 21:06:26 +03:00
|
|
|
},
|
2023-08-21 17:56:50 +03:00
|
|
|
{
|
2023-08-27 13:56:26 +03:00
|
|
|
onCancel: () => {
|
|
|
|
p.cancel("Operation cancelled.");
|
|
|
|
process.exit(0);
|
|
|
|
},
|
2023-08-21 17:56:50 +03:00
|
|
|
},
|
2023-08-27 13:56:26 +03:00
|
|
|
);
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-08-27 13:56:26 +03:00
|
|
|
await runTasks([
|
2023-07-20 21:06:26 +03:00
|
|
|
{
|
|
|
|
title: "Check if package.json exists",
|
2023-08-27 13:56:26 +03:00
|
|
|
task: async () => {
|
2023-07-20 21:06:26 +03:00
|
|
|
if (!fs.existsSync("package.json")) {
|
|
|
|
await execa("npm", ["init", "-y"]);
|
2023-08-27 13:56:26 +03:00
|
|
|
return "package.jspn created";
|
2023-07-20 21:06:26 +03:00
|
|
|
} else {
|
2023-08-27 13:56:26 +03:00
|
|
|
return "package.json exists, skipping...";
|
2023-07-20 21:06:26 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Install packages",
|
|
|
|
task: async () => {
|
|
|
|
await execa("npm", ["i", "-D", ...config.packages.dev]);
|
|
|
|
await execa("npm", ["i", ...config.packages.dep]);
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "packages installed";
|
2023-07-20 21:06:26 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-16 17:46:37 +03:00
|
|
|
title: "Generate files",
|
|
|
|
task: async () => {
|
|
|
|
const files = await glob("**/*", {
|
|
|
|
cwd: path.join(__dirname, "files"),
|
|
|
|
nodir: true,
|
2023-08-27 13:56:26 +03:00
|
|
|
ignore: !resp.dotenv ? ["src/config/**"] : [],
|
2023-08-16 17:46:37 +03:00
|
|
|
});
|
|
|
|
for (const file of files) {
|
|
|
|
const content = fs.readFileSync(
|
|
|
|
path.join(__dirname, "files", file),
|
2023-08-27 13:56:26 +03:00
|
|
|
"utf-8",
|
2023-08-16 17:46:37 +03:00
|
|
|
);
|
|
|
|
await fs.outputFile(
|
|
|
|
path.join("./", file),
|
2023-08-27 13:56:26 +03:00
|
|
|
Handlebars.compile(content)(resp),
|
2023-08-16 17:46:37 +03:00
|
|
|
);
|
|
|
|
}
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "files generated";
|
2023-08-16 17:46:37 +03:00
|
|
|
},
|
2023-07-20 21:06:26 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Modify package.json",
|
|
|
|
task: async () => {
|
|
|
|
const package_json = JSON.parse(
|
2023-08-27 13:56:26 +03:00
|
|
|
fs.readFileSync("package.json", "utf-8"),
|
2023-07-20 21:06:26 +03:00
|
|
|
);
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-07-20 21:06:26 +03:00
|
|
|
package_json.main = "dist/app.js";
|
|
|
|
package_json.scripts.build = "tsc";
|
2023-08-16 17:46:37 +03:00
|
|
|
package_json.scripts.dev = 'tsc-watch --onSuccess "node ./dist/app.js"';
|
2023-07-20 21:06:26 +03:00
|
|
|
package_json.scripts.start = "node dist/app.js";
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-07-20 21:06:26 +03:00
|
|
|
fs.writeFileSync("package.json", JSON.stringify(package_json, null, 2));
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "package.json modified";
|
2023-07-20 21:06:26 +03:00
|
|
|
},
|
|
|
|
},
|
2023-08-16 17:46:37 +03:00
|
|
|
{
|
|
|
|
title: "Rename files",
|
|
|
|
task: async () => {
|
|
|
|
fs.renameSync("ts-prod-ignore", ".gitignore");
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "files renamed";
|
2023-08-16 17:46:37 +03:00
|
|
|
},
|
|
|
|
},
|
2023-07-20 21:06:26 +03:00
|
|
|
{
|
|
|
|
title: "Install Prisma",
|
2023-08-27 13:56:26 +03:00
|
|
|
enabled: resp.prisma,
|
2023-07-20 21:06:26 +03:00
|
|
|
task: async () => {
|
|
|
|
await execa("npm", ["i", "-D", "prisma"]);
|
|
|
|
await execa("npx", [
|
|
|
|
"prisma",
|
|
|
|
"init",
|
|
|
|
"--datasource-provider",
|
|
|
|
"sqlite",
|
|
|
|
]);
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "prisma installed";
|
2023-08-21 17:56:50 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Install Dotenv",
|
2023-08-27 13:56:26 +03:00
|
|
|
enabled: resp.dotenv,
|
2023-08-21 17:56:50 +03:00
|
|
|
task: async () => {
|
|
|
|
await execa("npm", ["i", "dotenv"]);
|
2023-08-27 13:56:26 +03:00
|
|
|
|
|
|
|
return "dotenv installed";
|
2023-07-20 21:06:26 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2023-07-19 15:38:42 +03:00
|
|
|
|
2023-08-27 13:56:26 +03:00
|
|
|
p.outro("All done!");
|
2023-07-20 21:06:26 +03:00
|
|
|
}
|
2023-07-19 15:50:35 +03:00
|
|
|
|
2023-07-20 21:06:26 +03:00
|
|
|
run();
|