create-vanillaforge 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js
CHANGED
|
@@ -59,6 +59,17 @@ function parseArgs(argv) {
|
|
|
59
59
|
return { projectName, template };
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Validation
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
function validateProjectName(name) {
|
|
67
|
+
if (!/^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$/i.test(name)) {
|
|
68
|
+
console.error('Error: project name may only contain letters, numbers, and hyphens.');
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
// ---------------------------------------------------------------------------
|
|
63
74
|
// Prompt helpers
|
|
64
75
|
// ---------------------------------------------------------------------------
|
|
@@ -94,30 +105,28 @@ async function promptTemplate(rl) {
|
|
|
94
105
|
}
|
|
95
106
|
|
|
96
107
|
// ---------------------------------------------------------------------------
|
|
97
|
-
// File copy
|
|
108
|
+
// File copy — renames gitignore -> .gitignore and substitutes tokens in all files
|
|
109
|
+
// (npm strips .gitignore from published packages, so templates store it as 'gitignore')
|
|
98
110
|
// ---------------------------------------------------------------------------
|
|
99
111
|
|
|
100
|
-
function copyDir(src, dest) {
|
|
112
|
+
function copyDir(src, dest, tokens) {
|
|
101
113
|
fs.mkdirSync(dest, { recursive: true });
|
|
102
114
|
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
103
115
|
const srcPath = path.join(src, entry.name);
|
|
104
|
-
const
|
|
116
|
+
const destName = entry.name === 'gitignore' ? '.gitignore' : entry.name;
|
|
117
|
+
const destPath = path.join(dest, destName);
|
|
105
118
|
if (entry.isDirectory()) {
|
|
106
|
-
copyDir(srcPath, destPath);
|
|
119
|
+
copyDir(srcPath, destPath, tokens);
|
|
107
120
|
} else {
|
|
108
|
-
fs.
|
|
121
|
+
let content = fs.readFileSync(srcPath, 'utf8');
|
|
122
|
+
for (const [from, to] of Object.entries(tokens)) {
|
|
123
|
+
content = content.split(from).join(to);
|
|
124
|
+
}
|
|
125
|
+
fs.writeFileSync(destPath, content, 'utf8');
|
|
109
126
|
}
|
|
110
127
|
}
|
|
111
128
|
}
|
|
112
129
|
|
|
113
|
-
function rewriteFile(filePath, replacements) {
|
|
114
|
-
let content = fs.readFileSync(filePath, 'utf8');
|
|
115
|
-
for (const [from, to] of replacements) {
|
|
116
|
-
content = content.split(from).join(to);
|
|
117
|
-
}
|
|
118
|
-
fs.writeFileSync(filePath, content, 'utf8');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
130
|
// ---------------------------------------------------------------------------
|
|
122
131
|
// Main
|
|
123
132
|
// ---------------------------------------------------------------------------
|
|
@@ -128,6 +137,8 @@ async function main() {
|
|
|
128
137
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
129
138
|
|
|
130
139
|
const projectName = argName || (await promptProjectName(rl));
|
|
140
|
+
validateProjectName(projectName);
|
|
141
|
+
|
|
131
142
|
const template = argTemplate || (await promptTemplate(rl));
|
|
132
143
|
|
|
133
144
|
rl.close();
|
|
@@ -145,18 +156,8 @@ async function main() {
|
|
|
145
156
|
}
|
|
146
157
|
|
|
147
158
|
const templateDir = path.join(TEMPLATES_DIR, template);
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
// Patch the app name into the generated files
|
|
151
|
-
const htmlPath = path.join(targetDir, 'index.html');
|
|
152
|
-
if (fs.existsSync(htmlPath)) {
|
|
153
|
-
rewriteFile(htmlPath, [['{{project-name}}', projectName]]);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const pkgPath = path.join(targetDir, 'package.json');
|
|
157
|
-
if (fs.existsSync(pkgPath)) {
|
|
158
|
-
rewriteFile(pkgPath, [['{{project-name}}', projectName]]);
|
|
159
|
-
}
|
|
159
|
+
const tokens = { '{{project-name}}': projectName };
|
|
160
|
+
copyDir(templateDir, targetDir, tokens);
|
|
160
161
|
|
|
161
162
|
console.log(`\nScaffolded "${projectName}" with the "${template}" template.\n`);
|
|
162
163
|
console.log('Next steps:\n');
|
package/package.json
CHANGED