-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
executable file
Β·160 lines (138 loc) Β· 4.22 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const prompt = require("prompt");
// Ensure templates directory exists
const templatesDir = path.join(__dirname, "src", "templates");
const templatesSrcDir = path.join(templatesDir, "src");
// Configure prompt
prompt.message = "";
prompt.delimiter = "";
prompt.colors = false;
// Function to ask user for confirmation
function askForConfirmation() {
return new Promise((resolve, reject) => {
const schema = {
properties: {
confirm: {
description: "π€ Templates directory already exists. Would you like to delete everything and start fresh? (y/N)",
type: "string",
pattern: /^[ynYN\s]*$/,
message: "Please enter y or n",
default: "n",
required: true
}
}
};
prompt.start();
prompt.get(schema, (err, result) => {
if (err) {
if (err.message === "canceled") {
console.log("\nβ Operation cancelled by user");
process.exit(1);
}
reject(err);
return;
}
resolve(result.confirm.toLowerCase().startsWith("y"));
});
});
}
// Function to clean templates directory
function cleanTemplates() {
if (fs.existsSync(templatesSrcDir)) {
fs.rmSync(templatesSrcDir, { recursive: true, force: true });
}
// Clean package.json and tsconfig.json in templates
const templateFiles = ["package.json", "tsconfig.json"].map(file => path.join(templatesDir, file));
templateFiles.forEach(file => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
});
}
// Create directories if they don't exist
function ensureDirectoryExists(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
// Copy directory recursively
function copyDir(src, dest) {
ensureDirectoryExists(dest);
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
// Main build process
async function build() {
const args = process.argv.slice(2);
const shouldRebuild = args.includes("-r");
if (fs.existsSync(templatesSrcDir)) {
if (shouldRebuild) {
console.log("ποΈ Cleaning existing templates directory...");
cleanTemplates();
} else {
const shouldDelete = await askForConfirmation();
if (shouldDelete) {
console.log("ποΈ Cleaning existing templates directory...");
cleanTemplates();
}
}
}
console.log("π Starting build process...");
// Ensure templates directory exists
ensureDirectoryExists(templatesDir);
ensureDirectoryExists(templatesSrcDir);
// Copy source files
console.log("π Copying source files...");
const srcDir = path.join(__dirname, "src", "base", "src");
copyDir(srcDir, templatesSrcDir);
// Copy tsconfig.json if it exists
const tsConfigPath = path.join(__dirname, "tsconfig.json");
if (fs.existsSync(tsConfigPath)) {
const tsConfig = require(tsConfigPath);
tsConfig.compilerOptions = {
...tsConfig.compilerOptions,
rootDir: "./src"
};
tsConfig.exclude = ["node_modules"];
fs.writeFileSync(path.join(templatesDir, "tsconfig.json"), JSON.stringify(tsConfig, null, 2));
}
// Update templates/package.json if needed
console.log("π¦ Updating package.json...");
const mainPackageJson = require("./package.json");
const templatePackageJson = {
name: "expressr-app",
version: "1.0.0",
description: "Express app with dynamic folder routing",
main: "dist/index.js",
scripts: {
dev: "ts-node-dev --respawn --transpile-only src/index.ts",
build: "tsc",
start: "node dist/index.js"
},
dependencies: {
express: mainPackageJson.dependencies.express
},
devDependencies: {
"@types/express": mainPackageJson.devDependencies["@types/express"],
"@types/node": mainPackageJson.devDependencies["@types/node"],
"ts-node-dev": mainPackageJson.devDependencies["ts-node-dev"],
typescript: mainPackageJson.devDependencies.typescript
}
};
fs.writeFileSync(path.join(templatesDir, "package.json"), JSON.stringify(templatePackageJson, null, 2));
console.log("β¨ Build completed successfully!");
}
build().catch(error => {
console.error("β Build failed:", error);
process.exit(1);
});