create-shiftapi 0.0.6 → 0.0.8
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/dist/index.js +71 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -16,10 +16,14 @@ var renameFiles = {
|
|
|
16
16
|
};
|
|
17
17
|
var pkgDir = path.resolve(fileURLToPath(import.meta.url), "../..");
|
|
18
18
|
var templatesDir = path.join(pkgDir, "templates");
|
|
19
|
-
var pkgVersion =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
var pkgVersion = "0.0.0";
|
|
20
|
+
try {
|
|
21
|
+
pkgVersion = JSON.parse(
|
|
22
|
+
fs.readFileSync(path.join(pkgDir, "package.json"), "utf-8")
|
|
23
|
+
).version;
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
function replacePlaceholders(content, opts) {
|
|
23
27
|
return content.replaceAll("{{name}}", opts.name).replaceAll("{{modulePath}}", opts.modulePath).replaceAll("{{port}}", opts.port).replaceAll("{{version}}", pkgVersion);
|
|
24
28
|
}
|
|
25
29
|
function renamePath(filePath, opts) {
|
|
@@ -37,7 +41,7 @@ function copyDir(srcDir, destDir, opts) {
|
|
|
37
41
|
} else {
|
|
38
42
|
const content = fs.readFileSync(srcPath, "utf-8");
|
|
39
43
|
fs.mkdirSync(path.dirname(destPath), { recursive: true });
|
|
40
|
-
fs.writeFileSync(destPath,
|
|
44
|
+
fs.writeFileSync(destPath, replacePlaceholders(content, opts));
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
47
|
}
|
|
@@ -52,6 +56,39 @@ function gitInit(cwd) {
|
|
|
52
56
|
});
|
|
53
57
|
});
|
|
54
58
|
}
|
|
59
|
+
function checkCommand(cmd) {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
execFile(cmd, ["version"], (err) => {
|
|
62
|
+
if (err) {
|
|
63
|
+
reject(new Error(`"${cmd}" is not installed or not on PATH. Please install it and try again.`));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
resolve();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function installDeps(cwd) {
|
|
71
|
+
await checkCommand("go");
|
|
72
|
+
await new Promise((resolve, reject) => {
|
|
73
|
+
execFile("go", ["mod", "tidy"], { cwd }, (err) => {
|
|
74
|
+
if (err) {
|
|
75
|
+
reject(err);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
resolve();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
await checkCommand("npm");
|
|
82
|
+
await new Promise((resolve, reject) => {
|
|
83
|
+
execFile("npm", ["install"], { cwd }, (err) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
reject(err);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
resolve();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
55
92
|
async function scaffold(opts) {
|
|
56
93
|
copyDir(path.join(templatesDir, "base"), opts.targetDir, opts);
|
|
57
94
|
copyDir(path.join(templatesDir, opts.framework), opts.targetDir, opts);
|
|
@@ -86,7 +123,12 @@ async function main() {
|
|
|
86
123
|
rawName: () => positionalArg ? Promise.resolve(positionalArg) : p.text({
|
|
87
124
|
message: "Project name",
|
|
88
125
|
placeholder: "my-app",
|
|
89
|
-
defaultValue: "my-app"
|
|
126
|
+
defaultValue: "my-app",
|
|
127
|
+
validate: (value) => {
|
|
128
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(value)) {
|
|
129
|
+
return "Project name must only contain letters, numbers, hyphens, and underscores";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
90
132
|
}),
|
|
91
133
|
framework: () => p.select({
|
|
92
134
|
message: "Framework",
|
|
@@ -112,7 +154,14 @@ async function main() {
|
|
|
112
154
|
port: () => p.text({
|
|
113
155
|
message: "Server port",
|
|
114
156
|
placeholder: "8080",
|
|
115
|
-
defaultValue: "8080"
|
|
157
|
+
defaultValue: "8080",
|
|
158
|
+
validate: (value) => {
|
|
159
|
+
if (!value) return;
|
|
160
|
+
const n = parseInt(value, 10);
|
|
161
|
+
if (isNaN(n) || n < 1 || n > 65535) {
|
|
162
|
+
return "Port must be a number between 1 and 65535";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
116
165
|
})
|
|
117
166
|
},
|
|
118
167
|
{
|
|
@@ -137,11 +186,22 @@ async function main() {
|
|
|
137
186
|
targetDir
|
|
138
187
|
});
|
|
139
188
|
s.stop("Project scaffolded");
|
|
189
|
+
const shouldInstallDeps = await p.confirm({
|
|
190
|
+
message: "Install dependencies? (go mod tidy & npm install)",
|
|
191
|
+
initialValue: true
|
|
192
|
+
});
|
|
193
|
+
if (p.isCancel(shouldInstallDeps)) {
|
|
194
|
+
p.cancel("Cancelled.");
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
if (shouldInstallDeps) {
|
|
198
|
+
s.start("Installing dependencies");
|
|
199
|
+
await installDeps(targetDir);
|
|
200
|
+
s.stop("Dependencies installed");
|
|
201
|
+
}
|
|
140
202
|
const relDir = path2.relative(process.cwd(), targetDir) || ".";
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"Next steps"
|
|
144
|
-
);
|
|
203
|
+
const steps = shouldInstallDeps ? [`cd ${relDir}`, "npm run dev"] : [`cd ${relDir}`, "go mod tidy", "npm install", "npm run dev"];
|
|
204
|
+
p.note(steps.join("\n"), "Next steps");
|
|
145
205
|
p.outro("Happy hacking!");
|
|
146
206
|
}
|
|
147
207
|
main().catch((err) => {
|