create-better-schemic 0.1.0-beta.6 → 0.1.0-beta.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/lib/index.js +93 -85
- package/package.json +4 -5
- package/src/index.ts +186 -168
package/lib/index.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// src/index.ts
|
|
3
3
|
import { spawnSync } from "node:child_process";
|
|
4
|
-
import {
|
|
5
|
-
existsSync,
|
|
6
|
-
mkdirSync,
|
|
7
|
-
readdirSync,
|
|
8
|
-
readFileSync,
|
|
9
|
-
writeFileSync
|
|
10
|
-
} from "node:fs";
|
|
4
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
5
|
import { createRequire } from "node:module";
|
|
12
|
-
import
|
|
6
|
+
import nodePath from "node:path";
|
|
13
7
|
|
|
14
8
|
// ../../node_modules/.bun/@clack+core@1.6.0/node_modules/@clack/core/dist/index.mjs
|
|
15
9
|
import { styleText } from "node:util";
|
|
@@ -1234,40 +1228,52 @@ ${i}
|
|
|
1234
1228
|
}
|
|
1235
1229
|
}).prompt();
|
|
1236
1230
|
// package.json
|
|
1237
|
-
var version = "0.1.0-beta.
|
|
1231
|
+
var version = "0.1.0-beta.8";
|
|
1238
1232
|
|
|
1239
1233
|
// src/index.ts
|
|
1240
1234
|
var RANGE = `^${version}`;
|
|
1235
|
+
var JSON_INDENT = 2;
|
|
1236
|
+
var ARGV_START = 2;
|
|
1237
|
+
var INSTALL_ERROR_TAIL = 8;
|
|
1238
|
+
var ARG_STEP = 2;
|
|
1241
1239
|
var bold = (s) => `\x1B[1m${s}\x1B[0m`;
|
|
1242
1240
|
var dim = (s) => `\x1B[2m${s}\x1B[0m`;
|
|
1241
|
+
var exitProcess = process.exit.bind(process);
|
|
1242
|
+
var exit = (code) => Reflect.apply(exitProcess, undefined, [code]);
|
|
1243
1243
|
var PMS = ["bun", "npm", "pnpm", "yarn"];
|
|
1244
1244
|
function parseArgs(argv) {
|
|
1245
1245
|
const o = { git: true, yes: false };
|
|
1246
|
-
|
|
1247
|
-
|
|
1246
|
+
const consume = (index) => {
|
|
1247
|
+
if (index >= argv.length)
|
|
1248
|
+
return;
|
|
1249
|
+
const a = argv[index];
|
|
1248
1250
|
if (a === "-y" || a === "--yes")
|
|
1249
1251
|
o.yes = true;
|
|
1250
1252
|
else if (a === "--no-install")
|
|
1251
1253
|
o.install = false;
|
|
1252
1254
|
else if (a === "--no-git")
|
|
1253
1255
|
o.git = false;
|
|
1254
|
-
else if (a === "--pm")
|
|
1255
|
-
o.pm = argv[
|
|
1256
|
-
|
|
1256
|
+
else if (a === "--pm") {
|
|
1257
|
+
o.pm = argv[index + 1];
|
|
1258
|
+
consume(index + ARG_STEP);
|
|
1259
|
+
return;
|
|
1260
|
+
} else if (a === "-h" || a === "--help") {
|
|
1257
1261
|
printHelp();
|
|
1258
|
-
|
|
1262
|
+
exit(0);
|
|
1259
1263
|
} else if (a.startsWith("-"))
|
|
1260
1264
|
unknownOption(a);
|
|
1261
|
-
else
|
|
1262
|
-
o.dir
|
|
1263
|
-
|
|
1265
|
+
else
|
|
1266
|
+
o.dir ??= a;
|
|
1267
|
+
consume(index + 1);
|
|
1268
|
+
};
|
|
1269
|
+
consume(0);
|
|
1264
1270
|
return o;
|
|
1265
1271
|
}
|
|
1266
1272
|
function unknownOption(option) {
|
|
1267
1273
|
console.error(`Unknown option: ${option}
|
|
1268
1274
|
`);
|
|
1269
1275
|
printHelp();
|
|
1270
|
-
|
|
1276
|
+
exit(1);
|
|
1271
1277
|
}
|
|
1272
1278
|
function printHelp() {
|
|
1273
1279
|
console.log(`create-better-schemic — scaffold a new Better-schemic project
|
|
@@ -1281,17 +1287,15 @@ Options:
|
|
|
1281
1287
|
-y, --yes accept defaults, no prompts
|
|
1282
1288
|
-h, --help show this help`);
|
|
1283
1289
|
}
|
|
1290
|
+
var text2 = (value) => typeof value === "string" ? value : "";
|
|
1284
1291
|
function detectPm() {
|
|
1285
1292
|
const ua = process.env.npm_config_user_agent ?? "";
|
|
1286
|
-
|
|
1287
|
-
if (ua.startsWith(pm))
|
|
1288
|
-
return pm;
|
|
1289
|
-
return "npm";
|
|
1293
|
+
return PMS.find((pm) => ua.startsWith(pm)) ?? "npm";
|
|
1290
1294
|
}
|
|
1291
1295
|
function abortIfCancel(value) {
|
|
1292
1296
|
if (isCancel(value)) {
|
|
1293
1297
|
cancel("Cancelled.");
|
|
1294
|
-
|
|
1298
|
+
exit(1);
|
|
1295
1299
|
}
|
|
1296
1300
|
return value;
|
|
1297
1301
|
}
|
|
@@ -1303,56 +1307,58 @@ function depMap() {
|
|
|
1303
1307
|
}
|
|
1304
1308
|
var TSCONFIG = `${JSON.stringify({
|
|
1305
1309
|
compilerOptions: {
|
|
1306
|
-
|
|
1310
|
+
lib: ["ESNext"],
|
|
1307
1311
|
module: "ESNext",
|
|
1308
|
-
moduleResolution: "bundler",
|
|
1309
1312
|
moduleDetection: "force",
|
|
1310
|
-
|
|
1311
|
-
skipLibCheck: true,
|
|
1312
|
-
resolveJsonModule: true,
|
|
1313
|
+
moduleResolution: "bundler",
|
|
1313
1314
|
noEmit: true,
|
|
1314
|
-
|
|
1315
|
+
resolveJsonModule: true,
|
|
1316
|
+
skipLibCheck: true,
|
|
1317
|
+
strict: true,
|
|
1318
|
+
target: "ESNext",
|
|
1315
1319
|
types: ["node"]
|
|
1316
1320
|
},
|
|
1317
1321
|
exclude: ["node_modules"]
|
|
1318
|
-
}, null,
|
|
1322
|
+
}, null, JSON_INDENT)}
|
|
1319
1323
|
`;
|
|
1320
1324
|
function applyPackageJson(target, name) {
|
|
1321
|
-
const path = join(target, "package.json");
|
|
1325
|
+
const path = nodePath.join(target, "package.json");
|
|
1322
1326
|
const deps = depMap();
|
|
1323
1327
|
if (!existsSync(path)) {
|
|
1324
1328
|
const fresh = {
|
|
1329
|
+
dependencies: Object.fromEntries(Object.entries(deps).sort()),
|
|
1330
|
+
devDependencies: { "@types/node": "^20", typescript: "^5" },
|
|
1325
1331
|
name,
|
|
1326
|
-
version: "0.0.0",
|
|
1327
1332
|
private: true,
|
|
1328
|
-
type: "module",
|
|
1329
1333
|
scripts: SCRIPTS,
|
|
1330
|
-
|
|
1331
|
-
|
|
1334
|
+
type: "module",
|
|
1335
|
+
version: "0.0.0"
|
|
1332
1336
|
};
|
|
1333
|
-
writeFileSync(path, `${JSON.stringify(fresh, null,
|
|
1337
|
+
writeFileSync(path, `${JSON.stringify(fresh, null, JSON_INDENT)}
|
|
1334
1338
|
`);
|
|
1335
1339
|
return "+ package.json";
|
|
1336
1340
|
}
|
|
1337
1341
|
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
1338
|
-
pkg.dependencies ??= {};
|
|
1342
|
+
const dependencies = pkg.dependencies ??= {};
|
|
1339
1343
|
const added = [];
|
|
1340
|
-
|
|
1341
|
-
if (!(k in
|
|
1342
|
-
|
|
1344
|
+
Object.entries(deps).forEach(([k, v]) => {
|
|
1345
|
+
if (!(k in dependencies)) {
|
|
1346
|
+
dependencies[k] = v;
|
|
1343
1347
|
added.push(k);
|
|
1344
1348
|
}
|
|
1345
|
-
|
|
1346
|
-
pkg.
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1349
|
+
});
|
|
1350
|
+
pkg.dependencies = Object.fromEntries(Object.entries(dependencies).sort());
|
|
1351
|
+
const scripts = pkg.scripts ??= {};
|
|
1352
|
+
Object.entries(SCRIPTS).forEach(([k, v]) => {
|
|
1353
|
+
if (!(k in scripts))
|
|
1354
|
+
scripts[k] = v;
|
|
1355
|
+
});
|
|
1356
|
+
writeFileSync(path, `${JSON.stringify(pkg, null, JSON_INDENT)}
|
|
1351
1357
|
`);
|
|
1352
|
-
return added.length ? `~ package.json (added ${added.join(", ")})` : dim("· package.json (deps already present)");
|
|
1358
|
+
return added.length > 0 ? `~ package.json (added ${added.join(", ")})` : dim("· package.json (deps already present)");
|
|
1353
1359
|
}
|
|
1354
1360
|
function applyTsconfig(target) {
|
|
1355
|
-
const path = join(target, "tsconfig.json");
|
|
1361
|
+
const path = nodePath.join(target, "tsconfig.json");
|
|
1356
1362
|
if (!existsSync(path)) {
|
|
1357
1363
|
writeFileSync(path, TSCONFIG);
|
|
1358
1364
|
return "+ tsconfig.json";
|
|
@@ -1365,7 +1371,7 @@ var GITIGNORE = `node_modules/
|
|
|
1365
1371
|
.DS_Store
|
|
1366
1372
|
`;
|
|
1367
1373
|
function writeIfAbsent(dir, file, content) {
|
|
1368
|
-
const path = join(dir, file);
|
|
1374
|
+
const path = nodePath.join(dir, file);
|
|
1369
1375
|
if (existsSync(path))
|
|
1370
1376
|
return dim(`· ${file} (exists, skipped)`);
|
|
1371
1377
|
writeFileSync(path, content);
|
|
@@ -1374,68 +1380,68 @@ function writeIfAbsent(dir, file, content) {
|
|
|
1374
1380
|
function run(cmd, args, cwd, capture = false) {
|
|
1375
1381
|
const r = spawnSync(cmd, args, {
|
|
1376
1382
|
cwd,
|
|
1377
|
-
|
|
1378
|
-
|
|
1383
|
+
encoding: "utf8",
|
|
1384
|
+
stdio: capture ? "pipe" : "inherit"
|
|
1379
1385
|
});
|
|
1380
|
-
return { ok: r.status === 0, out: `${r.stdout
|
|
1386
|
+
return { ok: r.status === 0, out: `${text2(r.stdout)}${text2(r.stderr)}` };
|
|
1381
1387
|
}
|
|
1382
1388
|
function resolveCliEntry(target) {
|
|
1383
|
-
const require2 = createRequire(join(target, "package.json"));
|
|
1389
|
+
const require2 = createRequire(nodePath.join(target, "package.json"));
|
|
1384
1390
|
const manifestPath = require2.resolve("better-schemic/package.json");
|
|
1385
1391
|
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
1386
1392
|
const bin = manifest.bin;
|
|
1387
1393
|
const rel = typeof bin === "string" ? bin : bin?.["better-schemic"];
|
|
1388
|
-
if (!rel)
|
|
1394
|
+
if (!rel) {
|
|
1389
1395
|
throw new Error("the installed `better-schemic` package exposes no `better-schemic` bin — reinstall it.");
|
|
1390
|
-
|
|
1396
|
+
}
|
|
1397
|
+
return nodePath.join(nodePath.dirname(manifestPath), rel);
|
|
1391
1398
|
}
|
|
1392
1399
|
async function main() {
|
|
1393
|
-
const opts = parseArgs(process.argv.slice(
|
|
1394
|
-
const tty =
|
|
1400
|
+
const opts = parseArgs(process.argv.slice(ARGV_START));
|
|
1401
|
+
const tty = process.stdin.isTTY && !opts.yes;
|
|
1395
1402
|
intro(bold("create-better-schemic"));
|
|
1396
1403
|
let dir = opts.dir;
|
|
1397
1404
|
if (!dir && tty) {
|
|
1398
1405
|
dir = abortIfCancel(await text({
|
|
1406
|
+
defaultValue: "better-schemic-app",
|
|
1399
1407
|
message: "Project directory",
|
|
1400
|
-
placeholder: "better-schemic-app"
|
|
1401
|
-
defaultValue: "better-schemic-app"
|
|
1408
|
+
placeholder: "better-schemic-app"
|
|
1402
1409
|
}));
|
|
1403
1410
|
}
|
|
1404
|
-
dir
|
|
1405
|
-
const target = resolve(process.cwd(), dir);
|
|
1406
|
-
const name = basename(target);
|
|
1411
|
+
dir = dir && dir.length > 0 ? dir : "better-schemic-app";
|
|
1412
|
+
const target = nodePath.resolve(process.cwd(), dir);
|
|
1413
|
+
const name = nodePath.basename(target);
|
|
1407
1414
|
mkdirSync(target, { recursive: true });
|
|
1408
|
-
const isExisting = existsSync(join(target, "package.json"));
|
|
1409
|
-
if (readdirSync(target).length && !isExisting && tty) {
|
|
1415
|
+
const isExisting = existsSync(nodePath.join(target, "package.json"));
|
|
1416
|
+
if (readdirSync(target).length > 0 && !isExisting && tty) {
|
|
1410
1417
|
const ok = abortIfCancel(await confirm({
|
|
1411
|
-
|
|
1412
|
-
|
|
1418
|
+
initialValue: false,
|
|
1419
|
+
message: `${bold(dir)} is not empty — continue?`
|
|
1413
1420
|
}));
|
|
1414
1421
|
if (!ok) {
|
|
1415
1422
|
cancel("Aborted.");
|
|
1416
|
-
|
|
1423
|
+
exit(1);
|
|
1417
1424
|
}
|
|
1418
1425
|
}
|
|
1419
1426
|
const detected = detectPm();
|
|
1420
1427
|
let install = opts.install;
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
})) : true;
|
|
1428
|
+
install ??= tty ? abortIfCancel(await confirm({
|
|
1429
|
+
initialValue: true,
|
|
1430
|
+
message: "Install dependencies now?"
|
|
1431
|
+
})) : true;
|
|
1426
1432
|
let pm = opts.pm ?? detected;
|
|
1427
1433
|
if (!PMS.includes(pm)) {
|
|
1428
1434
|
cancel(`Unknown package manager "${pm}". Known: ${PMS.join(", ")}.`);
|
|
1429
|
-
|
|
1435
|
+
exit(1);
|
|
1430
1436
|
}
|
|
1431
1437
|
if (install && !opts.pm && tty) {
|
|
1432
1438
|
pm = abortIfCancel(await select({
|
|
1439
|
+
initialValue: detected,
|
|
1433
1440
|
message: "Install with which package manager?",
|
|
1434
1441
|
options: [detected, ...PMS.filter((x) => x !== detected)].map((m) => ({
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
}))
|
|
1438
|
-
initialValue: detected
|
|
1442
|
+
label: m === detected ? `${m} (detected)` : m,
|
|
1443
|
+
value: m
|
|
1444
|
+
}))
|
|
1439
1445
|
}));
|
|
1440
1446
|
}
|
|
1441
1447
|
const written = [
|
|
@@ -1447,7 +1453,7 @@ async function main() {
|
|
|
1447
1453
|
log.step(`${bold(header)} ${dim("(SurrealDB)")}
|
|
1448
1454
|
${written.join(`
|
|
1449
1455
|
`)}`);
|
|
1450
|
-
if (opts.git && !existsSync(join(target, ".git")))
|
|
1456
|
+
if (opts.git && !existsSync(nodePath.join(target, ".git")))
|
|
1451
1457
|
run("git", ["init", "-q"], target);
|
|
1452
1458
|
if (install) {
|
|
1453
1459
|
const s = spinner();
|
|
@@ -1456,10 +1462,10 @@ ${written.join(`
|
|
|
1456
1462
|
if (!r.ok) {
|
|
1457
1463
|
s.stop(`${pm} install failed`);
|
|
1458
1464
|
log.error(r.out.trim().split(`
|
|
1459
|
-
`).slice(-
|
|
1465
|
+
`).slice(-INSTALL_ERROR_TAIL).join(`
|
|
1460
1466
|
`));
|
|
1461
1467
|
outro(`Fix the install, then run ${bold("better-schemic init")}.`);
|
|
1462
|
-
|
|
1468
|
+
exit(1);
|
|
1463
1469
|
}
|
|
1464
1470
|
s.stop("Dependencies installed");
|
|
1465
1471
|
log.step("Scaffolding the schema");
|
|
@@ -1479,7 +1485,9 @@ ${dbCmd("gen")}`;
|
|
|
1479
1485
|
note(steps, "Next steps");
|
|
1480
1486
|
outro(`${bold(name)} is ready.`);
|
|
1481
1487
|
}
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1488
|
+
try {
|
|
1489
|
+
await main();
|
|
1490
|
+
} catch (error) {
|
|
1491
|
+
log.error(error instanceof Error ? error.message : String(error));
|
|
1492
|
+
exit(1);
|
|
1493
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-better-schemic",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.8",
|
|
4
4
|
"description": "Scaffold a new Better-schemic project — package.json, tsconfig, a driver, and the database/ schema, in one command.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "NONSTANDARDCODE",
|
|
@@ -27,12 +27,11 @@
|
|
|
27
27
|
"build": "bun run ../../scripts/build.ts",
|
|
28
28
|
"prepack": "bun run build",
|
|
29
29
|
"test": "bun test",
|
|
30
|
-
"typecheck": "bun check"
|
|
31
|
-
"lint": "biome check ."
|
|
30
|
+
"typecheck": "bun check"
|
|
32
31
|
},
|
|
33
32
|
"devDependencies": {
|
|
34
|
-
"@biomejs/biome": "^2.6.8",
|
|
35
33
|
"@clack/prompts": "^1.9.0",
|
|
36
|
-
"@types/bun": "^1.4.
|
|
34
|
+
"@types/bun": "^1.4.5",
|
|
35
|
+
"better-schemic": "0.1.0-beta.8"
|
|
37
36
|
}
|
|
38
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,58 +1,72 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process"
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from "node:fs";
|
|
9
|
-
import { createRequire } from "node:module";
|
|
10
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
11
|
-
import * as p from "@clack/prompts";
|
|
1
|
+
import { spawnSync } from "node:child_process"
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
|
|
3
|
+
import { createRequire } from "node:module"
|
|
4
|
+
import nodePath from "node:path"
|
|
5
|
+
|
|
6
|
+
import * as p from "@clack/prompts"
|
|
7
|
+
|
|
12
8
|
// The better-schemic version this scaffolder pins is its OWN version (the packages release lockstep), so a
|
|
13
9
|
// fresh project always gets a matching set. Inlined at build by the Bun bundler.
|
|
14
|
-
import { version as BETTER_SCHEMIC_VERSION } from "../package.json"
|
|
10
|
+
import { version as BETTER_SCHEMIC_VERSION } from "../package.json"
|
|
11
|
+
|
|
12
|
+
const RANGE = `^${BETTER_SCHEMIC_VERSION}`
|
|
13
|
+
/** Pretty-print indent for generated JSON files. */
|
|
14
|
+
const JSON_INDENT = 2
|
|
15
|
+
/** `process.argv` starts with the runtime and the script path. */
|
|
16
|
+
const ARGV_START = 2
|
|
17
|
+
/** Tail of the install log shown when installation fails. */
|
|
18
|
+
const INSTALL_ERROR_TAIL = 8
|
|
19
|
+
/** `--pm` consumes the flag and its value. */
|
|
20
|
+
const ARG_STEP = 2
|
|
21
|
+
const bold = (s: string): string => `\u001B[1m${s}\u001B[0m`
|
|
22
|
+
const dim = (s: string): string => `\u001B[2m${s}\u001B[0m`
|
|
15
23
|
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
const
|
|
24
|
+
/** The CLI's single exit seam — every path reports its code through this typed function. */
|
|
25
|
+
const exitProcess = process.exit.bind(process)
|
|
26
|
+
const exit = (code?: null | number | string): never => Reflect.apply(exitProcess, undefined, [code])
|
|
19
27
|
|
|
20
|
-
const PMS = ["bun", "npm", "pnpm", "yarn"] as const
|
|
21
|
-
type Pm = (typeof PMS)[number]
|
|
28
|
+
const PMS = ["bun", "npm", "pnpm", "yarn"] as const
|
|
29
|
+
type Pm = (typeof PMS)[number]
|
|
22
30
|
|
|
23
|
-
|
|
24
|
-
dir?: string
|
|
25
|
-
|
|
26
|
-
install?: boolean
|
|
27
|
-
|
|
28
|
-
yes: boolean
|
|
31
|
+
type Options = {
|
|
32
|
+
dir?: string
|
|
33
|
+
git: boolean
|
|
34
|
+
install?: boolean // undefined = ask
|
|
35
|
+
pm?: string
|
|
36
|
+
yes: boolean
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
function parseArgs(argv: string[]): Options {
|
|
32
|
-
const o: Options = { git: true, yes: false }
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
else if (a === "--no-
|
|
38
|
-
else if (a === "--
|
|
39
|
-
else if (a === "
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const o: Options = { git: true, yes: false }
|
|
41
|
+
const consume = (index: number): void => {
|
|
42
|
+
if (index >= argv.length) return
|
|
43
|
+
const a = argv[index]
|
|
44
|
+
if (a === "-y" || a === "--yes") o.yes = true
|
|
45
|
+
else if (a === "--no-install") o.install = false
|
|
46
|
+
else if (a === "--no-git") o.git = false
|
|
47
|
+
else if (a === "--pm") {
|
|
48
|
+
o.pm = argv[index + 1]
|
|
49
|
+
consume(index + ARG_STEP)
|
|
50
|
+
return
|
|
51
|
+
} else if (a === "-h" || a === "--help") {
|
|
52
|
+
printHelp()
|
|
53
|
+
exit(0)
|
|
42
54
|
} else if (a.startsWith("-"))
|
|
43
55
|
// An unknown OPTION is an error, never a directory name — a typo must not scaffold a folder
|
|
44
56
|
// named after the flag's value.
|
|
45
|
-
unknownOption(a)
|
|
46
|
-
else
|
|
57
|
+
unknownOption(a)
|
|
58
|
+
else o.dir ??= a
|
|
59
|
+
consume(index + 1)
|
|
47
60
|
}
|
|
48
|
-
|
|
61
|
+
consume(0)
|
|
62
|
+
return o
|
|
49
63
|
}
|
|
50
64
|
|
|
51
65
|
/** Refuse an unknown option with the usage (an exit, so the caller never continues). */
|
|
52
|
-
function unknownOption(option: string):
|
|
53
|
-
console.error(`Unknown option: ${option}\n`)
|
|
54
|
-
printHelp()
|
|
55
|
-
|
|
66
|
+
function unknownOption(option: string): void {
|
|
67
|
+
console.error(`Unknown option: ${option}\n`)
|
|
68
|
+
printHelp()
|
|
69
|
+
exit(1)
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
function printHelp(): void {
|
|
@@ -65,23 +79,25 @@ Options:
|
|
|
65
79
|
--no-install scaffold only; don't install or run \`better-schemic init\`
|
|
66
80
|
--no-git don't run \`git init\`
|
|
67
81
|
-y, --yes accept defaults, no prompts
|
|
68
|
-
-h, --help show this help`)
|
|
82
|
+
-h, --help show this help`)
|
|
69
83
|
}
|
|
70
84
|
|
|
85
|
+
/** With `stdio: inherit`, spawnSync reports null stdout/stderr at runtime despite the types. */
|
|
86
|
+
const text = (value: unknown): string => (typeof value === "string" ? value : "")
|
|
87
|
+
|
|
71
88
|
/** Which package manager invoked us (from npm_config_user_agent), for a sensible default. */
|
|
72
89
|
function detectPm(): Pm {
|
|
73
|
-
const ua = process.env.npm_config_user_agent ?? ""
|
|
74
|
-
|
|
75
|
-
return "npm";
|
|
90
|
+
const ua = process.env.npm_config_user_agent ?? ""
|
|
91
|
+
return PMS.find((pm) => ua.startsWith(pm)) ?? "npm"
|
|
76
92
|
}
|
|
77
93
|
|
|
78
94
|
/** Abort cleanly on Ctrl-C / cancel from any prompt. */
|
|
79
95
|
function abortIfCancel<T>(value: T): Exclude<T, symbol> {
|
|
80
96
|
if (p.isCancel(value)) {
|
|
81
|
-
p.cancel("Cancelled.")
|
|
82
|
-
|
|
97
|
+
p.cancel("Cancelled.")
|
|
98
|
+
exit(1)
|
|
83
99
|
}
|
|
84
|
-
return value as Exclude<T, symbol
|
|
100
|
+
return value as Exclude<T, symbol>
|
|
85
101
|
}
|
|
86
102
|
|
|
87
103
|
// --- templates ---------------------------------------------------------------------------------
|
|
@@ -90,7 +106,7 @@ function abortIfCancel<T>(value: T): Exclude<T, symbol> {
|
|
|
90
106
|
// etc. (bun/pnpm/yarn forward the subcommand directly; npm needs `npm run db -- gen`).
|
|
91
107
|
const SCRIPTS: Record<string, string> = {
|
|
92
108
|
db: "better-schemic",
|
|
93
|
-
}
|
|
109
|
+
}
|
|
94
110
|
|
|
95
111
|
/**
|
|
96
112
|
* The runtime deps a scaffolded project needs. `better-schemic` is ONE package: the authoring
|
|
@@ -98,7 +114,7 @@ const SCRIPTS: Record<string, string> = {
|
|
|
98
114
|
* dependencies, not peers), so the project never installs them itself.
|
|
99
115
|
*/
|
|
100
116
|
function depMap(): Record<string, string> {
|
|
101
|
-
return { "better-schemic": RANGE }
|
|
117
|
+
return { "better-schemic": RANGE }
|
|
102
118
|
}
|
|
103
119
|
|
|
104
120
|
// Kept conservative so it works across TypeScript 5, 6, and 7: `module: "ESNext"` (5.0+) rather than
|
|
@@ -107,88 +123,90 @@ function depMap(): Record<string, string> {
|
|
|
107
123
|
const TSCONFIG = `${JSON.stringify(
|
|
108
124
|
{
|
|
109
125
|
compilerOptions: {
|
|
110
|
-
|
|
126
|
+
lib: ["ESNext"],
|
|
111
127
|
module: "ESNext",
|
|
112
|
-
moduleResolution: "bundler",
|
|
113
128
|
moduleDetection: "force",
|
|
114
|
-
|
|
115
|
-
skipLibCheck: true,
|
|
116
|
-
resolveJsonModule: true,
|
|
129
|
+
moduleResolution: "bundler",
|
|
117
130
|
noEmit: true,
|
|
118
|
-
|
|
131
|
+
resolveJsonModule: true,
|
|
132
|
+
skipLibCheck: true,
|
|
133
|
+
strict: true,
|
|
134
|
+
target: "ESNext",
|
|
119
135
|
types: ["node"],
|
|
120
136
|
},
|
|
121
137
|
exclude: ["node_modules"],
|
|
122
138
|
},
|
|
123
139
|
null,
|
|
124
|
-
|
|
125
|
-
)}\n
|
|
140
|
+
JSON_INDENT,
|
|
141
|
+
)}\n`
|
|
126
142
|
|
|
127
143
|
/** NEW project → write a fresh package.json. EXISTING → MERGE deps + scripts (never clobber). */
|
|
128
144
|
function applyPackageJson(target: string, name: string): string {
|
|
129
|
-
const path = join(target, "package.json")
|
|
130
|
-
const deps = depMap()
|
|
145
|
+
const path = nodePath.join(target, "package.json")
|
|
146
|
+
const deps = depMap()
|
|
131
147
|
if (!existsSync(path)) {
|
|
132
148
|
const fresh = {
|
|
149
|
+
dependencies: Object.fromEntries(Object.entries(deps).sort()),
|
|
150
|
+
devDependencies: { "@types/node": "^20", typescript: "^5" },
|
|
133
151
|
name,
|
|
134
|
-
version: "0.0.0",
|
|
135
152
|
private: true,
|
|
136
|
-
type: "module",
|
|
137
153
|
scripts: SCRIPTS,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
writeFileSync(path, `${JSON.stringify(fresh, null,
|
|
142
|
-
return "+ package.json"
|
|
154
|
+
type: "module",
|
|
155
|
+
version: "0.0.0",
|
|
156
|
+
}
|
|
157
|
+
writeFileSync(path, `${JSON.stringify(fresh, null, JSON_INDENT)}\n`)
|
|
158
|
+
return "+ package.json"
|
|
143
159
|
}
|
|
144
160
|
const pkg = JSON.parse(readFileSync(path, "utf8")) as {
|
|
145
|
-
dependencies?: Record<string, string
|
|
146
|
-
scripts?: Record<string, string
|
|
147
|
-
}
|
|
148
|
-
pkg.dependencies ??= {}
|
|
149
|
-
const added: string[] = []
|
|
150
|
-
|
|
151
|
-
if (!(k in
|
|
152
|
-
|
|
153
|
-
added.push(k)
|
|
161
|
+
dependencies?: Record<string, string>
|
|
162
|
+
scripts?: Record<string, string>
|
|
163
|
+
}
|
|
164
|
+
const dependencies = (pkg.dependencies ??= {})
|
|
165
|
+
const added: string[] = []
|
|
166
|
+
Object.entries(deps).forEach(([k, v]) => {
|
|
167
|
+
if (!(k in dependencies)) {
|
|
168
|
+
dependencies[k] = v
|
|
169
|
+
added.push(k)
|
|
154
170
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
171
|
+
})
|
|
172
|
+
pkg.dependencies = Object.fromEntries(Object.entries(dependencies).sort())
|
|
173
|
+
const scripts = (pkg.scripts ??= {})
|
|
174
|
+
Object.entries(SCRIPTS).forEach(([k, v]) => {
|
|
175
|
+
if (!(k in scripts)) scripts[k] = v
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
writeFileSync(path, `${JSON.stringify(pkg, null, JSON_INDENT)}\n`)
|
|
179
|
+
return added.length > 0
|
|
163
180
|
? `~ package.json (added ${added.join(", ")})`
|
|
164
|
-
: dim("· package.json (deps already present)")
|
|
181
|
+
: dim("· package.json (deps already present)")
|
|
165
182
|
}
|
|
166
183
|
|
|
167
184
|
/** NEW project → write a fresh tsconfig. EXISTING → keep theirs, with a compatibility note. */
|
|
168
185
|
function applyTsconfig(target: string): string {
|
|
169
|
-
const path = join(target, "tsconfig.json")
|
|
186
|
+
const path = nodePath.join(target, "tsconfig.json")
|
|
170
187
|
if (!existsSync(path)) {
|
|
171
|
-
writeFileSync(path, TSCONFIG)
|
|
172
|
-
return "+ tsconfig.json"
|
|
188
|
+
writeFileSync(path, TSCONFIG)
|
|
189
|
+
return "+ tsconfig.json"
|
|
173
190
|
}
|
|
174
191
|
return dim(
|
|
175
192
|
"· tsconfig.json (kept — needs module esnext/preserve/nodenext + resolveJsonModule for seed imports)",
|
|
176
|
-
)
|
|
193
|
+
)
|
|
177
194
|
}
|
|
178
195
|
|
|
179
196
|
const GITIGNORE = `node_modules/
|
|
180
197
|
.env
|
|
181
198
|
*.log
|
|
182
199
|
.DS_Store
|
|
183
|
-
|
|
200
|
+
`
|
|
184
201
|
|
|
185
202
|
// --- write + run -------------------------------------------------------------------------------
|
|
186
203
|
|
|
187
204
|
function writeIfAbsent(dir: string, file: string, content: string): string {
|
|
188
|
-
const path = join(dir, file)
|
|
189
|
-
if (existsSync(path)) return dim(`· ${file} (exists, skipped)`)
|
|
190
|
-
|
|
191
|
-
|
|
205
|
+
const path = nodePath.join(dir, file)
|
|
206
|
+
if (existsSync(path)) return dim(`· ${file} (exists, skipped)`)
|
|
207
|
+
|
|
208
|
+
writeFileSync(path, content)
|
|
209
|
+
return `+ ${file}`
|
|
192
210
|
}
|
|
193
211
|
|
|
194
212
|
function run(
|
|
@@ -199,10 +217,10 @@ function run(
|
|
|
199
217
|
): { ok: boolean; out: string } {
|
|
200
218
|
const r = spawnSync(cmd, args, {
|
|
201
219
|
cwd,
|
|
202
|
-
stdio: capture ? "pipe" : "inherit",
|
|
203
220
|
encoding: "utf8",
|
|
204
|
-
|
|
205
|
-
|
|
221
|
+
stdio: capture ? "pipe" : "inherit",
|
|
222
|
+
})
|
|
223
|
+
return { ok: r.status === 0, out: `${text(r.stdout)}${text(r.stderr)}` }
|
|
206
224
|
}
|
|
207
225
|
|
|
208
226
|
/**
|
|
@@ -211,84 +229,84 @@ function run(
|
|
|
211
229
|
* scaffolder.
|
|
212
230
|
*/
|
|
213
231
|
function resolveCliEntry(target: string): string {
|
|
214
|
-
const require = createRequire(join(target, "package.json"))
|
|
215
|
-
const manifestPath = require.resolve("better-schemic/package.json")
|
|
232
|
+
const require = createRequire(nodePath.join(target, "package.json"))
|
|
233
|
+
const manifestPath = require.resolve("better-schemic/package.json")
|
|
216
234
|
const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as {
|
|
217
|
-
bin?:
|
|
218
|
-
}
|
|
219
|
-
const bin = manifest.bin
|
|
220
|
-
const rel = typeof bin === "string" ? bin : bin?.["better-schemic"]
|
|
221
|
-
if (!rel)
|
|
235
|
+
bin?: Record<string, string> | string
|
|
236
|
+
}
|
|
237
|
+
const bin = manifest.bin
|
|
238
|
+
const rel = typeof bin === "string" ? bin : bin?.["better-schemic"]
|
|
239
|
+
if (!rel) {
|
|
222
240
|
throw new Error(
|
|
223
241
|
"the installed `better-schemic` package exposes no `better-schemic` bin — reinstall it.",
|
|
224
|
-
)
|
|
225
|
-
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
return nodePath.join(nodePath.dirname(manifestPath), rel)
|
|
226
245
|
}
|
|
227
246
|
|
|
228
247
|
async function main(): Promise<void> {
|
|
229
|
-
const opts = parseArgs(process.argv.slice(
|
|
230
|
-
const tty =
|
|
248
|
+
const opts = parseArgs(process.argv.slice(ARGV_START))
|
|
249
|
+
const tty = process.stdin.isTTY && !opts.yes
|
|
231
250
|
|
|
232
|
-
p.intro(bold("create-better-schemic"))
|
|
251
|
+
p.intro(bold("create-better-schemic"))
|
|
233
252
|
|
|
234
|
-
|
|
235
|
-
let dir = opts.dir;
|
|
253
|
+
let dir = opts.dir
|
|
236
254
|
if (!dir && tty) {
|
|
237
255
|
dir = abortIfCancel(
|
|
238
256
|
await p.text({
|
|
257
|
+
defaultValue: "better-schemic-app",
|
|
239
258
|
message: "Project directory",
|
|
240
259
|
placeholder: "better-schemic-app",
|
|
241
|
-
defaultValue: "better-schemic-app",
|
|
242
260
|
}),
|
|
243
|
-
)
|
|
261
|
+
)
|
|
244
262
|
}
|
|
245
|
-
dir
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
263
|
+
// An empty `--dir ""` resolves to the default, matching the old `||=` behavior.
|
|
264
|
+
dir = dir && dir.length > 0 ? dir : "better-schemic-app"
|
|
265
|
+
const target = nodePath.resolve(process.cwd(), dir)
|
|
266
|
+
const name = nodePath.basename(target)
|
|
267
|
+
mkdirSync(target, { recursive: true })
|
|
268
|
+
const isExisting = existsSync(nodePath.join(target, "package.json"))
|
|
269
|
+
// Warn for a non-empty NON-project dir (random files); a package.json = a deliberate adapt.
|
|
270
|
+
if (readdirSync(target).length > 0 && !isExisting && tty) {
|
|
252
271
|
const ok = abortIfCancel(
|
|
253
272
|
await p.confirm({
|
|
254
|
-
message: `${bold(dir)} is not empty — continue?`,
|
|
255
273
|
initialValue: false,
|
|
274
|
+
message: `${bold(dir)} is not empty — continue?`,
|
|
256
275
|
}),
|
|
257
|
-
)
|
|
276
|
+
)
|
|
258
277
|
if (!ok) {
|
|
259
|
-
p.cancel("Aborted.")
|
|
260
|
-
|
|
278
|
+
p.cancel("Aborted.")
|
|
279
|
+
exit(1)
|
|
261
280
|
}
|
|
262
281
|
}
|
|
263
282
|
|
|
264
283
|
// 2. install? + which package manager (ask, default = detected)
|
|
265
|
-
const detected = detectPm()
|
|
266
|
-
let install = opts.install
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
let pm: Pm = (opts.pm as Pm) ?? detected;
|
|
284
|
+
const detected = detectPm()
|
|
285
|
+
let install = opts.install
|
|
286
|
+
install ??= tty
|
|
287
|
+
? abortIfCancel(
|
|
288
|
+
await p.confirm({
|
|
289
|
+
initialValue: true,
|
|
290
|
+
message: "Install dependencies now?",
|
|
291
|
+
}),
|
|
292
|
+
)
|
|
293
|
+
: true
|
|
294
|
+
let pm: Pm = (opts.pm as Pm | undefined) ?? detected
|
|
277
295
|
if (!PMS.includes(pm)) {
|
|
278
|
-
p.cancel(`Unknown package manager "${pm}". Known: ${PMS.join(", ")}.`)
|
|
279
|
-
|
|
296
|
+
p.cancel(`Unknown package manager "${pm}". Known: ${PMS.join(", ")}.`)
|
|
297
|
+
exit(1)
|
|
280
298
|
}
|
|
281
299
|
if (install && !opts.pm && tty) {
|
|
282
300
|
pm = abortIfCancel(
|
|
283
301
|
await p.select({
|
|
302
|
+
initialValue: detected,
|
|
284
303
|
message: "Install with which package manager?",
|
|
285
304
|
options: [detected, ...PMS.filter((x) => x !== detected)].map((m) => ({
|
|
286
|
-
value: m,
|
|
287
305
|
label: m === detected ? `${m} (detected)` : m,
|
|
306
|
+
value: m,
|
|
288
307
|
})),
|
|
289
|
-
initialValue: detected,
|
|
290
308
|
}),
|
|
291
|
-
)
|
|
309
|
+
)
|
|
292
310
|
}
|
|
293
311
|
|
|
294
312
|
// 3. write/merge the project envelope (fresh for a new project, merge into an existing one)
|
|
@@ -296,43 +314,43 @@ async function main(): Promise<void> {
|
|
|
296
314
|
applyPackageJson(target, name),
|
|
297
315
|
applyTsconfig(target),
|
|
298
316
|
writeIfAbsent(target, ".gitignore", GITIGNORE),
|
|
299
|
-
]
|
|
300
|
-
const header = isExisting ? `Adding Better-schemic to ${name}` : name
|
|
301
|
-
p.log.step(`${bold(header)} ${dim("(SurrealDB)")}\n${written.join("\n")}`)
|
|
302
|
-
if (opts.git && !existsSync(join(target, ".git")))
|
|
303
|
-
run("git", ["init", "-q"], target);
|
|
317
|
+
]
|
|
318
|
+
const header = isExisting ? `Adding Better-schemic to ${name}` : name
|
|
319
|
+
p.log.step(`${bold(header)} ${dim("(SurrealDB)")}\n${written.join("\n")}`)
|
|
320
|
+
if (opts.git && !existsSync(nodePath.join(target, ".git"))) run("git", ["init", "-q"], target)
|
|
304
321
|
|
|
305
322
|
// 4. install + compose on `better-schemic init` (which scaffolds the config + database/)
|
|
306
323
|
if (install) {
|
|
307
|
-
const s = p.spinner()
|
|
308
|
-
s.start(`Installing dependencies with ${pm}`)
|
|
309
|
-
const r = run(pm, ["install"], target, true)
|
|
324
|
+
const s = p.spinner()
|
|
325
|
+
s.start(`Installing dependencies with ${pm}`)
|
|
326
|
+
const r = run(pm, ["install"], target, true)
|
|
310
327
|
if (!r.ok) {
|
|
311
|
-
s.stop(`${pm} install failed`)
|
|
312
|
-
p.log.error(r.out.trim().split("\n").slice(-
|
|
313
|
-
p.outro(`Fix the install, then run ${bold("better-schemic init")}.`)
|
|
314
|
-
|
|
328
|
+
s.stop(`${pm} install failed`)
|
|
329
|
+
p.log.error(r.out.trim().split("\n").slice(-INSTALL_ERROR_TAIL).join("\n"))
|
|
330
|
+
p.outro(`Fix the install, then run ${bold("better-schemic init")}.`)
|
|
331
|
+
exit(1)
|
|
315
332
|
}
|
|
316
|
-
s.stop("Dependencies installed")
|
|
317
|
-
p.log.step("Scaffolding the schema")
|
|
318
|
-
const runtime = pm === "bun" ? "bun" : "node"
|
|
319
|
-
const cliJs = resolveCliEntry(target)
|
|
320
|
-
run(runtime, [cliJs, "init"], target)
|
|
333
|
+
s.stop("Dependencies installed")
|
|
334
|
+
p.log.step("Scaffolding the schema")
|
|
335
|
+
const runtime = pm === "bun" ? "bun" : "node"
|
|
336
|
+
const cliJs = resolveCliEntry(target)
|
|
337
|
+
run(runtime, [cliJs, "init"], target)
|
|
321
338
|
}
|
|
322
339
|
|
|
323
|
-
|
|
324
|
-
const cd = dir === "." ? "" : `cd ${dir}\n`;
|
|
340
|
+
const cd = dir === "." ? "" : `cd ${dir}\n`
|
|
325
341
|
// npm needs `npm run db -- <sub>` to forward args; bun/pnpm/yarn forward `<pm> run db <sub>` directly.
|
|
326
|
-
const dbCmd = (sub: string) =>
|
|
327
|
-
pm === "npm" ? `npm run db -- ${sub}` : `${pm} run db ${sub}
|
|
342
|
+
const dbCmd = (sub: string): string =>
|
|
343
|
+
pm === "npm" ? `npm run db -- ${sub}` : `${pm} run db ${sub}`
|
|
328
344
|
const steps = install
|
|
329
345
|
? `${cd}cp .env.example .env ${dim("# set your connection")}\n${dbCmd("gen")}\n${dbCmd("migrate")}`
|
|
330
|
-
: `${cd}${pm} install\n${pm} exec better-schemic init\ncp .env.example .env\n${dbCmd("gen")}
|
|
331
|
-
p.note(steps, "Next steps")
|
|
332
|
-
p.outro(`${bold(name)} is ready.`)
|
|
346
|
+
: `${cd}${pm} install\n${pm} exec better-schemic init\ncp .env.example .env\n${dbCmd("gen")}`
|
|
347
|
+
p.note(steps, "Next steps")
|
|
348
|
+
p.outro(`${bold(name)} is ready.`)
|
|
333
349
|
}
|
|
334
350
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
351
|
+
try {
|
|
352
|
+
await main()
|
|
353
|
+
} catch (error: unknown) {
|
|
354
|
+
p.log.error(error instanceof Error ? error.message : String(error))
|
|
355
|
+
exit(1)
|
|
356
|
+
}
|