create-ts-prod/index.js

128 lines
2.9 KiB
JavaScript
Raw Normal View History

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-07-20 21:06:26 +03:00
const { prompt } = require("enquirer");
const Listr = require("listr");
2023-08-16 17:46:37 +03:00
const { glob } = require("glob");
const Handlebars = require("handlebars");
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-07-20 21:06:26 +03:00
const resp = await prompt([
{
type: "confirm",
name: "prisma",
message: "Do you want to install Prisma?",
},
2023-08-21 17:56:50 +03:00
{
type: "confirm",
name: "dotenv",
message: "Do you want to install dotenv?",
},
2023-07-20 21:06:26 +03:00
]);
2023-07-19 15:38:42 +03:00
2023-07-20 21:06:26 +03:00
let tasks = new Listr([
{
title: "Check if package.json exists",
task: async (_, task) => {
if (!fs.existsSync("package.json")) {
await execa("npm", ["init", "-y"]);
} else {
task.skip("package.json exists, skipping...");
}
},
},
{
title: "Install packages",
task: async () => {
await execa("npm", ["i", "-D", ...config.packages.dev]);
await execa("npm", ["i", ...config.packages.dep]);
},
},
{
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-21 17:56:50 +03:00
ignore: [resp.dotenv ? "./config/**" : ""],
2023-08-16 17:46:37 +03:00
});
for (const file of files) {
const content = fs.readFileSync(
path.join(__dirname, "files", file),
"utf-8"
);
await fs.outputFile(
path.join("./", file),
Handlebars.compile(content)(resp)
);
}
},
2023-07-20 21:06:26 +03:00
},
{
title: "Modify package.json",
task: async () => {
const package_json = JSON.parse(
fs.readFileSync("package.json", "utf-8")
);
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-16 17:46:37 +03:00
{
title: "Rename files",
task: async () => {
fs.renameSync("ts-prod-ignore", ".gitignore");
},
},
2023-07-20 21:06:26 +03:00
{
title: "Install Prisma",
enabled: () => resp.prisma,
task: async () => {
await execa("npm", ["i", "-D", "prisma"]);
await execa("npx", [
"prisma",
"init",
"--datasource-provider",
"sqlite",
]);
2023-08-21 17:56:50 +03:00
},
},
{
title: "Install Dotenv",
enabled: () => resp.dotenv,
task: async () => {
await execa("npm", ["i", "dotenv"]);
2023-07-20 21:06:26 +03:00
},
},
]);
2023-07-19 15:38:42 +03:00
2023-07-20 21:06:26 +03:00
tasks
.run()
.then(() => console.log("Done!"))
.catch(console.error);
}
2023-07-19 15:50:35 +03:00
2023-07-20 21:06:26 +03:00
run();