@schemic/cli 0.1.0-alpha.0 → 0.1.0-alpha.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/lib/cli.js +21 -10
- package/package.json +1 -1
- package/src/cli/resolve.ts +28 -8
package/lib/cli.js
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
mkdirSync as mkdirSync3,
|
|
8
8
|
writeFileSync as writeFileSync3
|
|
9
9
|
} from "fs";
|
|
10
|
-
import { dirname as dirname2, join as
|
|
10
|
+
import { dirname as dirname2, join as join3, relative as relative2 } from "path";
|
|
11
11
|
import { createInterface } from "readline/promises";
|
|
12
12
|
import {
|
|
13
13
|
actionLabel,
|
|
@@ -423,6 +423,9 @@ async function closeQuietly(conn) {
|
|
|
423
423
|
}
|
|
424
424
|
|
|
425
425
|
// src/cli/resolve.ts
|
|
426
|
+
import { createRequire } from "module";
|
|
427
|
+
import { join as join2 } from "path";
|
|
428
|
+
import { pathToFileURL } from "url";
|
|
426
429
|
import {
|
|
427
430
|
driverNames,
|
|
428
431
|
getDriver as getDriver3,
|
|
@@ -434,12 +437,18 @@ async function ensureDriver(name) {
|
|
|
434
437
|
if (driverNames().includes(name)) return;
|
|
435
438
|
const pkg = `@schemic/${name}`;
|
|
436
439
|
try {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
)
|
|
440
|
+
const fromCwd = createRequire(join2(process.cwd(), "noop.js"));
|
|
441
|
+
await import(pathToFileURL(fromCwd.resolve(pkg)).href);
|
|
442
|
+
} catch {
|
|
443
|
+
try {
|
|
444
|
+
await import(pkg);
|
|
445
|
+
} catch (e) {
|
|
446
|
+
throw new Error(
|
|
447
|
+
`could not load the "${name}" database driver (package ${pkg}). Install it in your project, then re-run:
|
|
448
|
+
bun add ${pkg}
|
|
449
|
+
(${e instanceof Error ? e.message : String(e)})`
|
|
450
|
+
);
|
|
451
|
+
}
|
|
443
452
|
}
|
|
444
453
|
if (!driverNames().includes(name))
|
|
445
454
|
throw new Error(`package ${pkg} did not register a "${name}" driver.`);
|
|
@@ -480,7 +489,9 @@ async function resolveTargets(opts) {
|
|
|
480
489
|
const picked = key !== void 0 ? list.find((c) => c.key === key) : list.length === 1 ? list[0] : void 0;
|
|
481
490
|
if (!picked) {
|
|
482
491
|
if (key !== void 0)
|
|
483
|
-
throw new Error(
|
|
492
|
+
throw new Error(
|
|
493
|
+
`Connection "${name}" has no element with key "${key}".`
|
|
494
|
+
);
|
|
484
495
|
throw new Error(
|
|
485
496
|
`Connection "${name}" resolved to ${list.length} connections (a collection); address one with --connection ${name}:<key> or use --all.`
|
|
486
497
|
);
|
|
@@ -823,7 +834,7 @@ kindFlags(
|
|
|
823
834
|
const abs = kind === "table" ? loc.get(name) : void 0;
|
|
824
835
|
return abs ? relative2(config.root, abs) : relative2(
|
|
825
836
|
config.root,
|
|
826
|
-
|
|
837
|
+
join3(
|
|
827
838
|
config.schemaPath,
|
|
828
839
|
driver.registry.display(kind).folder,
|
|
829
840
|
`${name}.ts`
|
|
@@ -1262,7 +1273,7 @@ configFlag(
|
|
|
1262
1273
|
"`schemic new` needs a schema directory \u2014 your schema is a single file."
|
|
1263
1274
|
);
|
|
1264
1275
|
const content = driver.scaffoldEntity(kind, name);
|
|
1265
|
-
const target =
|
|
1276
|
+
const target = join3(
|
|
1266
1277
|
config.schemaPath,
|
|
1267
1278
|
driver.registry.display(kind).folder,
|
|
1268
1279
|
`${name}.ts`
|
package/package.json
CHANGED
package/src/cli/resolve.ts
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
// connects the sibling on demand (the dependency graph falls out of access; cycles error) and we close
|
|
11
11
|
// anything it opened once resolution settles. The returned configs are connected FRESH by each command.
|
|
12
12
|
|
|
13
|
+
import { createRequire } from "node:module";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { pathToFileURL } from "node:url";
|
|
13
16
|
import {
|
|
14
17
|
type ConnectionEntry,
|
|
15
18
|
type ConnectionOverrides,
|
|
@@ -32,12 +35,23 @@ import {
|
|
|
32
35
|
export async function ensureDriver(name: string): Promise<void> {
|
|
33
36
|
if (driverNames().includes(name)) return;
|
|
34
37
|
const pkg = `@schemic/${name}`;
|
|
38
|
+
// Resolve the driver from the USER's project (cwd) first, then fall back to the CLI's own location.
|
|
39
|
+
// The CLI is often run via `bunx`/`npx` from a temp dir, so a plain `import(pkg)` (resolved relative
|
|
40
|
+
// to the CLI module) would MISS a driver installed in the user's project — resolve from cwd instead.
|
|
35
41
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
const fromCwd = createRequire(join(process.cwd(), "noop.js"));
|
|
43
|
+
await import(pathToFileURL(fromCwd.resolve(pkg)).href);
|
|
44
|
+
} catch {
|
|
45
|
+
try {
|
|
46
|
+
await import(pkg);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`could not load the "${name}" database driver (package ${pkg}). ` +
|
|
50
|
+
`Install it in your project, then re-run:\n bun add ${pkg}\n (${
|
|
51
|
+
e instanceof Error ? e.message : String(e)
|
|
52
|
+
})`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
41
55
|
}
|
|
42
56
|
if (!driverNames().includes(name))
|
|
43
57
|
throw new Error(`package ${pkg} did not register a "${name}" driver.`);
|
|
@@ -71,9 +85,13 @@ function parseArgs(arg: string[] | undefined): Record<string, string> {
|
|
|
71
85
|
}
|
|
72
86
|
|
|
73
87
|
/** Split a `<name>` / `<name>:<key>` address on the FIRST colon. */
|
|
74
|
-
function splitAddress(
|
|
88
|
+
function splitAddress(
|
|
89
|
+
address: string,
|
|
90
|
+
): [name: string, key: string | undefined] {
|
|
75
91
|
const i = address.indexOf(":");
|
|
76
|
-
return i < 0
|
|
92
|
+
return i < 0
|
|
93
|
+
? [address, undefined]
|
|
94
|
+
: [address.slice(0, i), address.slice(i + 1)];
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
/**
|
|
@@ -120,7 +138,9 @@ export async function resolveTargets(
|
|
|
120
138
|
: undefined;
|
|
121
139
|
if (!picked) {
|
|
122
140
|
if (key !== undefined)
|
|
123
|
-
throw new Error(
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Connection "${name}" has no element with key "${key}".`,
|
|
143
|
+
);
|
|
124
144
|
throw new Error(
|
|
125
145
|
`Connection "${name}" resolved to ${list.length} connections (a collection); address one with --connection ${name}:<key> or use --all.`,
|
|
126
146
|
);
|