create-shiftapi 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. package/dist/index.js +70 -11
  2. 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 = JSON.parse(
20
- fs.readFileSync(path.join(pkgDir, "package.json"), "utf-8")
21
- ).version;
22
- function replaceplaceholders(content, opts) {
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, replaceplaceholders(content, opts));
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,13 @@ 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
+ const n = parseInt(value, 10);
160
+ if (isNaN(n) || n < 1 || n > 65535) {
161
+ return "Port must be a number between 1 and 65535";
162
+ }
163
+ }
116
164
  })
117
165
  },
118
166
  {
@@ -137,11 +185,22 @@ async function main() {
137
185
  targetDir
138
186
  });
139
187
  s.stop("Project scaffolded");
188
+ const shouldInstallDeps = await p.confirm({
189
+ message: "Install dependencies? (go mod tidy & npm install)",
190
+ initialValue: true
191
+ });
192
+ if (p.isCancel(shouldInstallDeps)) {
193
+ p.cancel("Cancelled.");
194
+ process.exit(1);
195
+ }
196
+ if (shouldInstallDeps) {
197
+ s.start("Installing dependencies");
198
+ await installDeps(targetDir);
199
+ s.stop("Dependencies installed");
200
+ }
140
201
  const relDir = path2.relative(process.cwd(), targetDir) || ".";
141
- p.note(
142
- [`cd ${relDir}`, "go mod tidy", "npm install", "npm run dev"].join("\n"),
143
- "Next steps"
144
- );
202
+ const steps = shouldInstallDeps ? [`cd ${relDir}`, "npm run dev"] : [`cd ${relDir}`, "go mod tidy", "npm install", "npm run dev"];
203
+ p.note(steps.join("\n"), "Next steps");
145
204
  p.outro("Happy hacking!");
146
205
  }
147
206
  main().catch((err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-shiftapi",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Scaffold a new ShiftAPI fullstack app",
5
5
  "author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
6
6
  "license": "MIT",