@dimah-s3/cli 0.4.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/README.md +64 -0
- package/dist/index.js +726 -0
- package/dist/index.js.map +1 -0
- package/dist/snapshot/transform.js +50 -0
- package/dist/snapshot/transform.js.map +1 -0
- package/dist/templates/catalog.json +9 -0
- package/dist/templates/nextjs/.env.example +5 -0
- package/dist/templates/nextjs/README.md +31 -0
- package/dist/templates/nextjs/_gitignore +29 -0
- package/dist/templates/nextjs/app/api/s3/[...s3]/route.ts +4 -0
- package/dist/templates/nextjs/app/globals.css +130 -0
- package/dist/templates/nextjs/app/layout.tsx +40 -0
- package/dist/templates/nextjs/app/page.tsx +30 -0
- package/dist/templates/nextjs/components/s3-provider.tsx +9 -0
- package/dist/templates/nextjs/components/theme-provider.tsx +11 -0
- package/dist/templates/nextjs/components.json +27 -0
- package/dist/templates/nextjs/eslint.config.js +9 -0
- package/dist/templates/nextjs/lib/s3-client.ts +12 -0
- package/dist/templates/nextjs/lib/s3.ts +11 -0
- package/dist/templates/nextjs/lib/utils.ts +1 -0
- package/dist/templates/nextjs/next-env.d.ts +7 -0
- package/dist/templates/nextjs/next.config.ts +5 -0
- package/dist/templates/nextjs/package.json +40 -0
- package/dist/templates/nextjs/postcss.config.mjs +7 -0
- package/dist/templates/nextjs/tsconfig.json +31 -0
- package/package.json +64 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { defineCommand, runMain } from 'citty';
|
|
3
|
+
import { readFile, access, cp, rm, readdir, mkdir, writeFile, rename } from 'fs/promises';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import { join, dirname, resolve, basename, relative } from 'pathe';
|
|
6
|
+
import validateNpmPackageName from 'validate-npm-package-name';
|
|
7
|
+
import * as p from '@clack/prompts';
|
|
8
|
+
import pc2 from 'picocolors';
|
|
9
|
+
import { installDependencies } from 'nypm';
|
|
10
|
+
import { execFile } from 'child_process';
|
|
11
|
+
import { promisify } from 'util';
|
|
12
|
+
import { readFileSync } from 'fs';
|
|
13
|
+
|
|
14
|
+
// src/utils/errors.ts
|
|
15
|
+
var EXIT_ERROR = 1;
|
|
16
|
+
var EXIT_CANCEL = 130;
|
|
17
|
+
var CliError = class extends Error {
|
|
18
|
+
exitCode;
|
|
19
|
+
constructor(message, exitCode = EXIT_ERROR, options) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = "CliError";
|
|
22
|
+
this.exitCode = exitCode;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function errorMessage(error) {
|
|
26
|
+
return error instanceof Error ? error.message : String(error);
|
|
27
|
+
}
|
|
28
|
+
async function pathExists(path) {
|
|
29
|
+
try {
|
|
30
|
+
await access(path);
|
|
31
|
+
return true;
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function listDir(path) {
|
|
37
|
+
try {
|
|
38
|
+
return await readdir(path);
|
|
39
|
+
} catch {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function ensureDir(path) {
|
|
44
|
+
await mkdir(path, { recursive: true });
|
|
45
|
+
}
|
|
46
|
+
async function emptyDir(path, options = {}) {
|
|
47
|
+
const keep = new Set(options.keep ?? []);
|
|
48
|
+
for (const entry of await listDir(path)) {
|
|
49
|
+
if (keep.has(entry)) continue;
|
|
50
|
+
await rm(join(path, entry), { recursive: true, force: true });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function copyDir(src, dest) {
|
|
54
|
+
await cp(src, dest, {
|
|
55
|
+
recursive: true,
|
|
56
|
+
force: true,
|
|
57
|
+
errorOnExist: false
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async function readJson(path) {
|
|
61
|
+
const raw = await readFile(path, "utf8");
|
|
62
|
+
return JSON.parse(raw);
|
|
63
|
+
}
|
|
64
|
+
async function writeJson(path, value) {
|
|
65
|
+
await writeFile(path, `${JSON.stringify(value, null, 2)}
|
|
66
|
+
`, "utf8");
|
|
67
|
+
}
|
|
68
|
+
async function renameIfExists(from, to) {
|
|
69
|
+
if (!await pathExists(from)) return false;
|
|
70
|
+
await rename(from, to);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
async function removeDir(path) {
|
|
74
|
+
await rm(path, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/templates/registry.ts
|
|
78
|
+
function templatesRoot() {
|
|
79
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
80
|
+
return join(here, "templates");
|
|
81
|
+
}
|
|
82
|
+
function fallbackTemplatesRoot() {
|
|
83
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
84
|
+
return join(here, "..", "..", "dist", "templates");
|
|
85
|
+
}
|
|
86
|
+
async function resolveTemplatesRoot() {
|
|
87
|
+
const primary = templatesRoot();
|
|
88
|
+
if (await pathExists(join(primary, "catalog.json"))) return primary;
|
|
89
|
+
const fallback = fallbackTemplatesRoot();
|
|
90
|
+
if (await pathExists(join(fallback, "catalog.json"))) return fallback;
|
|
91
|
+
throw new CliError(
|
|
92
|
+
`Template catalog missing at ${join(primary, "catalog.json")}. Run the CLI package build first.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
var cachedCatalog;
|
|
96
|
+
async function loadCatalog() {
|
|
97
|
+
if (cachedCatalog) return cachedCatalog;
|
|
98
|
+
const root = await resolveTemplatesRoot();
|
|
99
|
+
const catalogPath = join(root, "catalog.json");
|
|
100
|
+
let parsed;
|
|
101
|
+
try {
|
|
102
|
+
parsed = JSON.parse(await readFile(catalogPath, "utf8"));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw new CliError(
|
|
105
|
+
`Template catalog at ${catalogPath} is not readable JSON: ${errorMessage(error)}`,
|
|
106
|
+
void 0,
|
|
107
|
+
{ cause: error }
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (!Array.isArray(parsed.templates) || parsed.templates.length === 0) {
|
|
111
|
+
throw new CliError("Template catalog is empty.");
|
|
112
|
+
}
|
|
113
|
+
for (const entry of parsed.templates) {
|
|
114
|
+
if (!entry?.id || !entry?.title) {
|
|
115
|
+
throw new CliError("Each catalog entry needs id and title.");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
cachedCatalog = parsed.templates;
|
|
119
|
+
return cachedCatalog;
|
|
120
|
+
}
|
|
121
|
+
function findTemplate(templates, id) {
|
|
122
|
+
const found = templates.find((t) => t.id === id);
|
|
123
|
+
if (!found) {
|
|
124
|
+
const ids = templates.map((t) => t.id).join(", ");
|
|
125
|
+
throw new CliError(`Unknown template "${id}". Available: ${ids}`);
|
|
126
|
+
}
|
|
127
|
+
return found;
|
|
128
|
+
}
|
|
129
|
+
function normalizeTemplateId(id) {
|
|
130
|
+
return id.trim().toLowerCase();
|
|
131
|
+
}
|
|
132
|
+
async function resolveTemplate(id) {
|
|
133
|
+
const templates = await loadCatalog();
|
|
134
|
+
const meta = findTemplate(templates, normalizeTemplateId(id));
|
|
135
|
+
const dir = join(await resolveTemplatesRoot(), meta.id);
|
|
136
|
+
if (!await pathExists(dir)) {
|
|
137
|
+
throw new CliError(
|
|
138
|
+
`Template "${meta.id}" is listed in the catalog but missing at ${dir}.`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
return { meta, dir };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/utils/package-manager.ts
|
|
145
|
+
var KNOWN = ["pnpm", "npm", "yarn", "bun"];
|
|
146
|
+
function detectPackageManager(userAgent = process.env.npm_config_user_agent) {
|
|
147
|
+
if (!userAgent) return "npm";
|
|
148
|
+
const name = userAgent.split("/")[0]?.toLowerCase();
|
|
149
|
+
if (name === "pnpm" || name === "npm" || name === "yarn" || name === "bun") {
|
|
150
|
+
return name;
|
|
151
|
+
}
|
|
152
|
+
return "npm";
|
|
153
|
+
}
|
|
154
|
+
function parsePackageManagerFlag(value) {
|
|
155
|
+
if (!value) return void 0;
|
|
156
|
+
const normalized = value.toLowerCase();
|
|
157
|
+
if (KNOWN.includes(normalized)) {
|
|
158
|
+
return normalized;
|
|
159
|
+
}
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Unknown package manager "${value}". Expected one of: ${KNOWN.join(", ")}`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
function installCommand(pm) {
|
|
165
|
+
switch (pm) {
|
|
166
|
+
case "pnpm":
|
|
167
|
+
return "pnpm install";
|
|
168
|
+
case "yarn":
|
|
169
|
+
return "yarn";
|
|
170
|
+
case "bun":
|
|
171
|
+
return "bun install";
|
|
172
|
+
default:
|
|
173
|
+
return "npm install";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function runDevCommand(pm) {
|
|
177
|
+
switch (pm) {
|
|
178
|
+
case "pnpm":
|
|
179
|
+
return "pnpm dev";
|
|
180
|
+
case "yarn":
|
|
181
|
+
return "yarn dev";
|
|
182
|
+
case "bun":
|
|
183
|
+
return "bun run dev";
|
|
184
|
+
default:
|
|
185
|
+
return "npm run dev";
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
var INVALID_CHARS = /[^a-z0-9._-]+/gi;
|
|
189
|
+
function sanitizeProjectName(input) {
|
|
190
|
+
const base = input.trim().replace(/\\/g, "/").split("/").filter(Boolean).at(-1) ?? "";
|
|
191
|
+
const lowered = base.toLowerCase().replace(INVALID_CHARS, "-");
|
|
192
|
+
const trimmed = lowered.replace(/^[-._]+|[-._]+$/g, "");
|
|
193
|
+
return trimmed || "my-app";
|
|
194
|
+
}
|
|
195
|
+
function assertValidPackageName(name) {
|
|
196
|
+
const result = validateNpmPackageName(name);
|
|
197
|
+
if (result.validForNewPackages) return;
|
|
198
|
+
const problems = [...result.errors ?? [], ...result.warnings ?? []];
|
|
199
|
+
throw new CliError(
|
|
200
|
+
`Invalid package name "${name}"${problems.length ? `: ${problems.join("; ")}` : ""}`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
function intro2(message = "dimah-s3") {
|
|
204
|
+
p.intro(pc2.bgCyan(pc2.black(` ${message} `)));
|
|
205
|
+
}
|
|
206
|
+
function outro2(message) {
|
|
207
|
+
p.outro(message);
|
|
208
|
+
}
|
|
209
|
+
function note2(message, title) {
|
|
210
|
+
p.note(message, title);
|
|
211
|
+
}
|
|
212
|
+
function logInfo(message) {
|
|
213
|
+
p.log.info(message);
|
|
214
|
+
}
|
|
215
|
+
function logWarn(message) {
|
|
216
|
+
p.log.warn(message);
|
|
217
|
+
}
|
|
218
|
+
function logError(message) {
|
|
219
|
+
p.log.error(message);
|
|
220
|
+
}
|
|
221
|
+
function spinner2() {
|
|
222
|
+
return p.spinner({ indicator: "timer" });
|
|
223
|
+
}
|
|
224
|
+
function isInteractive() {
|
|
225
|
+
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
226
|
+
}
|
|
227
|
+
async function ask(prompt) {
|
|
228
|
+
const value = await prompt;
|
|
229
|
+
if (p.isCancel(value)) {
|
|
230
|
+
p.cancel("Operation cancelled.");
|
|
231
|
+
throw new CliError("Cancelled", EXIT_CANCEL);
|
|
232
|
+
}
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
var PRESERVED_ENTRIES = [".git", ".env"];
|
|
236
|
+
function resolveTarget(input, cwd) {
|
|
237
|
+
const trimmed = input.trim();
|
|
238
|
+
const targetDir = resolve(cwd, trimmed || ".");
|
|
239
|
+
return {
|
|
240
|
+
targetDir,
|
|
241
|
+
projectName: sanitizeProjectName(basename(targetDir)),
|
|
242
|
+
inPlace: targetDir === resolve(cwd)
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/create/config.ts
|
|
247
|
+
var DEFAULT_PROJECT_NAME = "my-app";
|
|
248
|
+
async function resolveOption(flag, options) {
|
|
249
|
+
if (flag !== void 0) return flag;
|
|
250
|
+
return options.interactive ? options.prompt() : options.fallback;
|
|
251
|
+
}
|
|
252
|
+
function validateNameInput(value) {
|
|
253
|
+
try {
|
|
254
|
+
assertValidPackageName(sanitizeProjectName(value || DEFAULT_PROJECT_NAME));
|
|
255
|
+
return void 0;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
return errorMessage(error);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
async function findConflicts(targetDir) {
|
|
261
|
+
const entries = await listDir(targetDir);
|
|
262
|
+
return entries.filter((entry) => !PRESERVED_ENTRIES.includes(entry));
|
|
263
|
+
}
|
|
264
|
+
async function resolveCreateConfig(flags) {
|
|
265
|
+
const cwd = process.cwd();
|
|
266
|
+
const templates = await loadCatalog();
|
|
267
|
+
const interactive = !flags.yes && isInteractive();
|
|
268
|
+
if (!flags.yes && !interactive) {
|
|
269
|
+
logWarn("Non-interactive terminal detected \u2014 continuing with defaults.");
|
|
270
|
+
}
|
|
271
|
+
let packageManager;
|
|
272
|
+
try {
|
|
273
|
+
packageManager = parsePackageManagerFlag(flags.packageManager) ?? detectPackageManager();
|
|
274
|
+
} catch (error) {
|
|
275
|
+
throw new CliError(errorMessage(error), void 0, { cause: error });
|
|
276
|
+
}
|
|
277
|
+
const dirFlag = flags.dir?.trim() || void 0;
|
|
278
|
+
if (!dirFlag && !interactive) {
|
|
279
|
+
throw new CliError(
|
|
280
|
+
"Project name is required in non-interactive mode. Pass a directory (e.g. create my-app) or run without --yes in a terminal."
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
const dirInput = dirFlag ?? await ask(
|
|
284
|
+
p.text({
|
|
285
|
+
message: "Project name",
|
|
286
|
+
placeholder: DEFAULT_PROJECT_NAME,
|
|
287
|
+
defaultValue: DEFAULT_PROJECT_NAME,
|
|
288
|
+
validate: validateNameInput
|
|
289
|
+
})
|
|
290
|
+
);
|
|
291
|
+
const target = resolveTarget(dirInput, cwd);
|
|
292
|
+
assertValidPackageName(target.projectName);
|
|
293
|
+
const templateId = await resolveOption(flags.template, {
|
|
294
|
+
// A single template needs no question.
|
|
295
|
+
interactive: interactive && templates.length > 1,
|
|
296
|
+
fallback: templates[0].id,
|
|
297
|
+
prompt: () => ask(
|
|
298
|
+
p.select({
|
|
299
|
+
message: "Framework",
|
|
300
|
+
options: templates.map((t) => ({
|
|
301
|
+
value: t.id,
|
|
302
|
+
label: t.title,
|
|
303
|
+
hint: t.hint
|
|
304
|
+
}))
|
|
305
|
+
})
|
|
306
|
+
)
|
|
307
|
+
});
|
|
308
|
+
const template = await resolveTemplate(templateId);
|
|
309
|
+
const install = await resolveOption(flags.install, {
|
|
310
|
+
interactive,
|
|
311
|
+
fallback: true,
|
|
312
|
+
prompt: () => ask(
|
|
313
|
+
p.confirm({
|
|
314
|
+
message: `Install dependencies with ${packageManager}?`,
|
|
315
|
+
initialValue: true
|
|
316
|
+
})
|
|
317
|
+
)
|
|
318
|
+
});
|
|
319
|
+
const git2 = await resolveOption(flags.git, {
|
|
320
|
+
interactive,
|
|
321
|
+
fallback: true,
|
|
322
|
+
prompt: () => ask(
|
|
323
|
+
p.confirm({
|
|
324
|
+
message: "Initialize a git repository?",
|
|
325
|
+
initialValue: true
|
|
326
|
+
})
|
|
327
|
+
)
|
|
328
|
+
});
|
|
329
|
+
const overwrite = await resolveOverwrite(target.targetDir, {
|
|
330
|
+
flag: Boolean(flags.overwrite),
|
|
331
|
+
interactive
|
|
332
|
+
});
|
|
333
|
+
return {
|
|
334
|
+
config: {
|
|
335
|
+
projectName: target.projectName,
|
|
336
|
+
targetDir: target.targetDir,
|
|
337
|
+
inPlace: target.inPlace,
|
|
338
|
+
template: template.meta.id,
|
|
339
|
+
packageManager,
|
|
340
|
+
install,
|
|
341
|
+
git: git2,
|
|
342
|
+
overwrite
|
|
343
|
+
},
|
|
344
|
+
template
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
async function resolveOverwrite(targetDir, options) {
|
|
348
|
+
const conflicts = await findConflicts(targetDir);
|
|
349
|
+
if (conflicts.length === 0) return false;
|
|
350
|
+
if (options.flag) return true;
|
|
351
|
+
if (!options.interactive) {
|
|
352
|
+
throw new CliError(
|
|
353
|
+
`Target directory "${targetDir}" is not empty (${conflicts.length} entries). Pass --overwrite to replace its contents.`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
const preview = conflicts.slice(0, 5).join(", ");
|
|
357
|
+
const more = conflicts.length > 5 ? `, +${conflicts.length - 5} more` : "";
|
|
358
|
+
const confirmed = await ask(
|
|
359
|
+
p.confirm({
|
|
360
|
+
message: `"${targetDir}" already contains ${conflicts.length} entries (${preview}${more}). Delete them? (${PRESERVED_ENTRIES.join(" and ")} are kept)`,
|
|
361
|
+
initialValue: false
|
|
362
|
+
})
|
|
363
|
+
);
|
|
364
|
+
if (!confirmed) {
|
|
365
|
+
p.cancel("Target directory is not empty.");
|
|
366
|
+
throw new CliError("Cancelled", EXIT_CANCEL);
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// src/create/step.ts
|
|
372
|
+
function createReporter() {
|
|
373
|
+
if (!isInteractive()) {
|
|
374
|
+
return {
|
|
375
|
+
start: (title) => logInfo(title),
|
|
376
|
+
update: () => {
|
|
377
|
+
},
|
|
378
|
+
finish: (message) => logInfo(message)
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const s = spinner2();
|
|
382
|
+
return {
|
|
383
|
+
start: (title) => s.start(title),
|
|
384
|
+
update: (message) => s.message(message),
|
|
385
|
+
finish: (message) => s.stop(message)
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
function stepTitle(step, ctx) {
|
|
389
|
+
return typeof step.title === "function" ? step.title(ctx) : step.title;
|
|
390
|
+
}
|
|
391
|
+
async function runSteps(steps, ctx) {
|
|
392
|
+
const failedSteps = [];
|
|
393
|
+
for (const step of steps) {
|
|
394
|
+
if (step.enabled && !step.enabled(ctx)) continue;
|
|
395
|
+
const title = stepTitle(step, ctx);
|
|
396
|
+
const reporter = createReporter();
|
|
397
|
+
reporter.start(title);
|
|
398
|
+
try {
|
|
399
|
+
const done = await step.run(ctx, reporter.update);
|
|
400
|
+
reporter.finish(done || title);
|
|
401
|
+
} catch (error) {
|
|
402
|
+
reporter.finish(`${title} \u2014 failed`);
|
|
403
|
+
if (step.recoverable) {
|
|
404
|
+
logWarn(errorMessage(error));
|
|
405
|
+
failedSteps.push(step.id);
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
throw error instanceof CliError ? error : new CliError(`${step.id}: ${errorMessage(error)}`, void 0, {
|
|
409
|
+
cause: error
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return { failedSteps };
|
|
414
|
+
}
|
|
415
|
+
var installStep = {
|
|
416
|
+
id: "install",
|
|
417
|
+
title: (ctx) => `Installing dependencies with ${ctx.config.packageManager}`,
|
|
418
|
+
enabled: (ctx) => ctx.config.install,
|
|
419
|
+
recoverable: true,
|
|
420
|
+
async run(ctx) {
|
|
421
|
+
const { packageManager, targetDir } = ctx.config;
|
|
422
|
+
try {
|
|
423
|
+
await installDependencies({
|
|
424
|
+
cwd: targetDir,
|
|
425
|
+
packageManager,
|
|
426
|
+
silent: true,
|
|
427
|
+
ignoreWorkspace: true
|
|
428
|
+
});
|
|
429
|
+
} catch (error) {
|
|
430
|
+
throw new Error(
|
|
431
|
+
`${errorMessage(error)}
|
|
432
|
+
Run ${installCommand(packageManager)} inside the project to retry.`,
|
|
433
|
+
{ cause: error }
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
ctx.installed = true;
|
|
437
|
+
return `Installed dependencies with ${packageManager}`;
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
var MIN_NODE = { major: 20, minor: 19 };
|
|
441
|
+
function packageRoot() {
|
|
442
|
+
return join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
443
|
+
}
|
|
444
|
+
var cachedVersion;
|
|
445
|
+
function cliVersion() {
|
|
446
|
+
if (cachedVersion) return cachedVersion;
|
|
447
|
+
try {
|
|
448
|
+
const pkg = JSON.parse(
|
|
449
|
+
readFileSync(join(packageRoot(), "package.json"), "utf8")
|
|
450
|
+
);
|
|
451
|
+
cachedVersion = pkg.version ?? "0.0.0";
|
|
452
|
+
} catch {
|
|
453
|
+
cachedVersion = "0.0.0";
|
|
454
|
+
}
|
|
455
|
+
return cachedVersion;
|
|
456
|
+
}
|
|
457
|
+
function assertSupportedNode(version = process.versions.node) {
|
|
458
|
+
const [major = 0, minor = 0] = version.split(".").map(Number);
|
|
459
|
+
const supported = major > MIN_NODE.major || major === MIN_NODE.major && minor >= MIN_NODE.minor;
|
|
460
|
+
if (supported) return;
|
|
461
|
+
throw new CliError(
|
|
462
|
+
`Node.js ${MIN_NODE.major}.${MIN_NODE.minor} or newer is required (running ${version}).`
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/create/steps/init-git.ts
|
|
467
|
+
var execFileAsync = promisify(execFile);
|
|
468
|
+
async function git(args, cwd) {
|
|
469
|
+
await execFileAsync("git", args, { cwd });
|
|
470
|
+
}
|
|
471
|
+
async function isInsideRepo(cwd) {
|
|
472
|
+
try {
|
|
473
|
+
await git(["rev-parse", "--is-inside-work-tree"], cwd);
|
|
474
|
+
return true;
|
|
475
|
+
} catch {
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
async function isGitAvailable() {
|
|
480
|
+
try {
|
|
481
|
+
await execFileAsync("git", ["--version"]);
|
|
482
|
+
return true;
|
|
483
|
+
} catch {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
var initGitStep = {
|
|
488
|
+
id: "git",
|
|
489
|
+
title: "Initializing git repository",
|
|
490
|
+
enabled: (ctx) => ctx.config.git,
|
|
491
|
+
recoverable: true,
|
|
492
|
+
async run(ctx, report) {
|
|
493
|
+
const cwd = ctx.config.targetDir;
|
|
494
|
+
if (!await isGitAvailable()) {
|
|
495
|
+
return "Skipped git init (git is not installed)";
|
|
496
|
+
}
|
|
497
|
+
if (await isInsideRepo(cwd)) {
|
|
498
|
+
return "Skipped git init (already inside a git repository)";
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
await git(["init", "-b", "main"], cwd);
|
|
502
|
+
} catch {
|
|
503
|
+
await git(["init"], cwd);
|
|
504
|
+
}
|
|
505
|
+
report("Creating the initial commit");
|
|
506
|
+
try {
|
|
507
|
+
await git(["add", "-A"], cwd);
|
|
508
|
+
await git(
|
|
509
|
+
[
|
|
510
|
+
"commit",
|
|
511
|
+
"--no-verify",
|
|
512
|
+
"-m",
|
|
513
|
+
`Initial commit from @dimah-s3/cli v${cliVersion()}`
|
|
514
|
+
],
|
|
515
|
+
cwd
|
|
516
|
+
);
|
|
517
|
+
} catch {
|
|
518
|
+
return "Initialized git repository (no initial commit \u2014 configure git user.name/user.email)";
|
|
519
|
+
}
|
|
520
|
+
return "Initialized git repository with an initial commit";
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
async function prepareTarget(ctx) {
|
|
524
|
+
const { targetDir, overwrite } = ctx.config;
|
|
525
|
+
const existed = await pathExists(targetDir);
|
|
526
|
+
if (existed && overwrite) {
|
|
527
|
+
await emptyDir(targetDir, { keep: PRESERVED_ENTRIES });
|
|
528
|
+
}
|
|
529
|
+
await ensureDir(targetDir);
|
|
530
|
+
ctx.createdTargetDir = !existed;
|
|
531
|
+
}
|
|
532
|
+
async function copyTemplate(ctx) {
|
|
533
|
+
await copyDir(ctx.templateDir, ctx.config.targetDir);
|
|
534
|
+
}
|
|
535
|
+
async function applyProjectName(ctx) {
|
|
536
|
+
const pkgPath = join(ctx.config.targetDir, "package.json");
|
|
537
|
+
if (!await pathExists(pkgPath)) {
|
|
538
|
+
throw new CliError(`Template is missing package.json at ${pkgPath}`);
|
|
539
|
+
}
|
|
540
|
+
const pkg = await readJson(pkgPath);
|
|
541
|
+
pkg.name = ctx.config.projectName;
|
|
542
|
+
await writeJson(pkgPath, pkg);
|
|
543
|
+
await renameIfExists(
|
|
544
|
+
join(ctx.config.targetDir, "_gitignore"),
|
|
545
|
+
join(ctx.config.targetDir, ".gitignore")
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
async function writeEnv(ctx) {
|
|
549
|
+
const example = join(ctx.config.targetDir, ".env.example");
|
|
550
|
+
const envPath = join(ctx.config.targetDir, ".env");
|
|
551
|
+
if (!await pathExists(example)) return;
|
|
552
|
+
if (await pathExists(envPath)) return;
|
|
553
|
+
await cp(example, envPath);
|
|
554
|
+
}
|
|
555
|
+
var scaffoldStep = {
|
|
556
|
+
id: "scaffold",
|
|
557
|
+
title: (ctx) => `Scaffolding ${ctx.template.title} template`,
|
|
558
|
+
async run(ctx, report) {
|
|
559
|
+
await prepareTarget(ctx);
|
|
560
|
+
report("Copying template files");
|
|
561
|
+
await copyTemplate(ctx);
|
|
562
|
+
report("Applying project name");
|
|
563
|
+
await applyProjectName(ctx);
|
|
564
|
+
await writeEnv(ctx);
|
|
565
|
+
return `Scaffolded ${ctx.template.title} template`;
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/create/pipeline.ts
|
|
570
|
+
var DOCS_URL = "https://dimah-s3.vercel.app";
|
|
571
|
+
var createSteps = [
|
|
572
|
+
scaffoldStep,
|
|
573
|
+
installStep,
|
|
574
|
+
initGitStep
|
|
575
|
+
];
|
|
576
|
+
async function runCreatePipeline({
|
|
577
|
+
config,
|
|
578
|
+
template
|
|
579
|
+
}) {
|
|
580
|
+
const ctx = {
|
|
581
|
+
config,
|
|
582
|
+
template: template.meta,
|
|
583
|
+
templateDir: template.dir,
|
|
584
|
+
cwd: process.cwd(),
|
|
585
|
+
createdTargetDir: false,
|
|
586
|
+
installed: false
|
|
587
|
+
};
|
|
588
|
+
let failedSteps;
|
|
589
|
+
try {
|
|
590
|
+
({ failedSteps } = await runSteps(createSteps, ctx));
|
|
591
|
+
} catch (error) {
|
|
592
|
+
await rollback(ctx);
|
|
593
|
+
throw error;
|
|
594
|
+
}
|
|
595
|
+
printNextSteps(ctx);
|
|
596
|
+
return { failedSteps };
|
|
597
|
+
}
|
|
598
|
+
async function rollback(ctx) {
|
|
599
|
+
if (!ctx.createdTargetDir) return;
|
|
600
|
+
try {
|
|
601
|
+
await removeDir(ctx.config.targetDir);
|
|
602
|
+
} catch {
|
|
603
|
+
logWarn(`Could not clean up ${ctx.config.targetDir}. Remove it manually.`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
function printNextSteps(ctx) {
|
|
607
|
+
const { config } = ctx;
|
|
608
|
+
const lines = [];
|
|
609
|
+
if (!config.inPlace) {
|
|
610
|
+
const rel = relative(ctx.cwd, config.targetDir) || config.projectName;
|
|
611
|
+
lines.push(`cd ${rel}`);
|
|
612
|
+
}
|
|
613
|
+
if (!ctx.installed) {
|
|
614
|
+
lines.push(installCommand(config.packageManager));
|
|
615
|
+
}
|
|
616
|
+
lines.push(runDevCommand(config.packageManager));
|
|
617
|
+
note2(
|
|
618
|
+
[
|
|
619
|
+
...lines.map((line) => pc2.cyan(line)),
|
|
620
|
+
pc2.dim("then fill the S3_* values in .env")
|
|
621
|
+
].join("\n"),
|
|
622
|
+
"Next steps"
|
|
623
|
+
);
|
|
624
|
+
outro2(`${pc2.bold(config.projectName)} is ready \u2014 docs: ${DOCS_URL}`);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// src/utils/exit.ts
|
|
628
|
+
async function withErrorBoundary(run) {
|
|
629
|
+
try {
|
|
630
|
+
await run();
|
|
631
|
+
} catch (error) {
|
|
632
|
+
if (error instanceof CliError) {
|
|
633
|
+
if (error.exitCode !== EXIT_CANCEL) {
|
|
634
|
+
logError(error.message);
|
|
635
|
+
}
|
|
636
|
+
process.exitCode = error.exitCode;
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
logError(errorMessage(error));
|
|
640
|
+
if (error instanceof Error && error.stack) {
|
|
641
|
+
process.stderr.write(`${error.stack}
|
|
642
|
+
`);
|
|
643
|
+
}
|
|
644
|
+
process.exitCode = EXIT_ERROR;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// src/commands/create.ts
|
|
649
|
+
var createCommand = defineCommand({
|
|
650
|
+
meta: {
|
|
651
|
+
name: "create",
|
|
652
|
+
description: "Scaffold a new dimah-s3 app from an official template"
|
|
653
|
+
},
|
|
654
|
+
args: {
|
|
655
|
+
dir: {
|
|
656
|
+
type: "positional",
|
|
657
|
+
description: "Project directory / name",
|
|
658
|
+
required: false
|
|
659
|
+
},
|
|
660
|
+
template: {
|
|
661
|
+
type: "string",
|
|
662
|
+
alias: "t",
|
|
663
|
+
description: "Template id (e.g. nextjs)"
|
|
664
|
+
},
|
|
665
|
+
"package-manager": {
|
|
666
|
+
type: "string",
|
|
667
|
+
description: "Package manager: pnpm | npm | yarn | bun"
|
|
668
|
+
},
|
|
669
|
+
install: {
|
|
670
|
+
type: "boolean",
|
|
671
|
+
description: "Install dependencies after scaffolding",
|
|
672
|
+
negativeDescription: "Skip dependency installation"
|
|
673
|
+
},
|
|
674
|
+
git: {
|
|
675
|
+
type: "boolean",
|
|
676
|
+
description: "Initialize a git repository",
|
|
677
|
+
negativeDescription: "Skip git init"
|
|
678
|
+
},
|
|
679
|
+
overwrite: {
|
|
680
|
+
type: "boolean",
|
|
681
|
+
description: "Replace the contents of a non-empty directory",
|
|
682
|
+
default: false
|
|
683
|
+
},
|
|
684
|
+
yes: {
|
|
685
|
+
type: "boolean",
|
|
686
|
+
alias: "y",
|
|
687
|
+
description: "Skip prompts and use defaults",
|
|
688
|
+
default: false
|
|
689
|
+
}
|
|
690
|
+
},
|
|
691
|
+
async run({ args }) {
|
|
692
|
+
await withErrorBoundary(async () => {
|
|
693
|
+
assertSupportedNode();
|
|
694
|
+
const flags = {
|
|
695
|
+
dir: typeof args.dir === "string" ? args.dir : void 0,
|
|
696
|
+
template: typeof args.template === "string" ? args.template : void 0,
|
|
697
|
+
packageManager: typeof args["package-manager"] === "string" ? args["package-manager"] : void 0,
|
|
698
|
+
install: typeof args.install === "boolean" ? args.install : void 0,
|
|
699
|
+
git: typeof args.git === "boolean" ? args.git : void 0,
|
|
700
|
+
overwrite: Boolean(args.overwrite),
|
|
701
|
+
yes: Boolean(args.yes)
|
|
702
|
+
};
|
|
703
|
+
intro2();
|
|
704
|
+
const resolved = await resolveCreateConfig(flags);
|
|
705
|
+
const { failedSteps } = await runCreatePipeline(resolved);
|
|
706
|
+
if (failedSteps.length > 0) {
|
|
707
|
+
process.exitCode = EXIT_ERROR;
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
// src/index.ts
|
|
714
|
+
var main = defineCommand({
|
|
715
|
+
meta: {
|
|
716
|
+
name: "dimah-s3",
|
|
717
|
+
version: cliVersion(),
|
|
718
|
+
description: "Official CLI for scaffolding dimah-s3 apps"
|
|
719
|
+
},
|
|
720
|
+
subCommands: {
|
|
721
|
+
create: createCommand
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
await runMain(main);
|
|
725
|
+
//# sourceMappingURL=index.js.map
|
|
726
|
+
//# sourceMappingURL=index.js.map
|