@hulla/db-cli 0.0.0-stage → 0.3.4
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/LICENSE +21 -0
- package/README.md +20 -2
- package/dist/build.d.ts +45 -0
- package/dist/build.js +141 -0
- package/dist/cli-address.d.ts +35 -0
- package/dist/cli-address.js +135 -0
- package/dist/cli-options.d.ts +13 -0
- package/dist/cli-options.js +43 -0
- package/dist/cli-output.d.ts +36 -0
- package/dist/cli-output.js +69 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +101 -0
- package/dist/codegen.d.ts +39 -0
- package/dist/codegen.js +358 -0
- package/dist/database-capture.d.ts +3 -0
- package/dist/database-capture.js +32 -0
- package/dist/database-codegen.d.ts +4 -0
- package/dist/database-codegen.js +46 -0
- package/dist/database-function-discovery.d.ts +10 -0
- package/dist/database-function-discovery.js +87 -0
- package/dist/dev-worker.d.ts +1 -0
- package/dist/dev-worker.js +138 -0
- package/dist/dev.d.ts +6 -0
- package/dist/dev.js +209 -0
- package/dist/development-compiler.d.ts +3 -0
- package/dist/development-compiler.js +5 -0
- package/dist/development-runtime.d.ts +2 -0
- package/dist/development-runtime.js +25 -0
- package/dist/function-analysis.d.ts +62 -0
- package/dist/function-analysis.js +425 -0
- package/dist/function-shapes.d.ts +8 -0
- package/dist/function-shapes.js +142 -0
- package/dist/host-codegen.d.ts +3 -0
- package/dist/host-codegen.js +45 -0
- package/dist/local-postgres-assets.d.ts +5 -0
- package/dist/local-postgres-assets.js +107 -0
- package/dist/local-postgres-assets.json +1 -0
- package/dist/local-postgres.d.ts +7 -0
- package/dist/local-postgres.js +180 -0
- package/dist/names.d.ts +2 -0
- package/dist/names.js +10 -0
- package/dist/project-codegen.d.ts +3 -0
- package/dist/project-codegen.js +107 -0
- package/dist/runtime-conditions.d.ts +2 -0
- package/dist/runtime-conditions.js +10 -0
- package/dist/schema-codegen.d.ts +16 -0
- package/dist/schema-codegen.js +212 -0
- package/dist/sdk-declarations.d.ts +1 -0
- package/dist/sdk-declarations.js +4 -0
- package/dist/setup-ignore.d.ts +6 -0
- package/dist/setup-ignore.js +35 -0
- package/dist/source-codegen.d.ts +7 -0
- package/dist/source-codegen.js +46 -0
- package/dist/studio.d.ts +8 -0
- package/dist/studio.js +28 -0
- package/dist/worker-codegen.d.ts +4 -0
- package/dist/worker-codegen.js +51 -0
- package/package.json +59 -4
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { basename, dirname, extname, resolve, relative, sep } from "node:path";
|
|
10
|
+
import { discoverFunctions } from "./codegen.js";
|
|
11
|
+
import { isFunctionDefinition } from "@hulla/db/internal/functions";
|
|
12
|
+
import { functionDatabases } from "@hulla/db/internal/codegen";
|
|
13
|
+
import { assertModuleName } from "./names.js";
|
|
14
|
+
import { compileWorker } from "./worker-codegen.js";
|
|
15
|
+
export function catalogueImports(analysis, declarations = []) {
|
|
16
|
+
const known = new Set(declarations.flatMap(value => value.functions?.kind === "import" ? [JSON.stringify([resolve(value.functions.importer), value.functions.specifier])] : []));
|
|
17
|
+
return async (file, specifier) => {
|
|
18
|
+
if (known.has(JSON.stringify([file, specifier])))
|
|
19
|
+
return true;
|
|
20
|
+
const values = await analysis.databaseDeclarations(file);
|
|
21
|
+
return values.some(value => value.functions?.kind === "import" && resolve(value.functions.importer) === file && value.functions.specifier === specifier);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Resolve explicit selectors before executing selected declaration modules. */
|
|
25
|
+
export async function databaseFunctions(database, source, outDir, declarations, options, analysis) {
|
|
26
|
+
if (database.functions !== undefined && options.functions !== undefined)
|
|
27
|
+
throw new TypeError("Declare functions in database initialization or hosting config, not both");
|
|
28
|
+
const selection = declarations.find(value => value.functions)?.functions;
|
|
29
|
+
let modules;
|
|
30
|
+
let rootModule;
|
|
31
|
+
if (database.functions === undefined) {
|
|
32
|
+
// An omitted catalogue means CRUD only. Legacy hosting may still opt into
|
|
33
|
+
// its explicit discovery directory; never expose nearby handlers implicitly.
|
|
34
|
+
modules = options.functions === undefined ? {} : await discoverFunctions({ ...options, schema: source, outDir }, analysis);
|
|
35
|
+
}
|
|
36
|
+
else if (!selection)
|
|
37
|
+
throw new TypeError("Cannot resolve functions configuration; declare it in the exported database constructor options");
|
|
38
|
+
else if (selection.kind !== "glob") {
|
|
39
|
+
const path = Bun.resolveSync(selection.specifier, dirname(selection.importer));
|
|
40
|
+
await analysis.open([path]);
|
|
41
|
+
const name = basename(path, extname(path));
|
|
42
|
+
const prefix = name === "index" ? basename(dirname(path)) : name;
|
|
43
|
+
modules = await analysis.catalogue(path, prefix);
|
|
44
|
+
if (modules[prefix] === path)
|
|
45
|
+
rootModule = prefix;
|
|
46
|
+
if (!Object.keys(modules).length)
|
|
47
|
+
throw new TypeError("The functions import exports no query or mutation declarations");
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const root = resolve(options.moduleRoot ?? dirname(selection.file));
|
|
51
|
+
const selected = Object.create(null);
|
|
52
|
+
for await (const path of new Bun.Glob(selection.pattern).scan({ cwd: dirname(selection.file), absolute: true, onlyFiles: true, followSymlinks: false })) {
|
|
53
|
+
const parts = relative(root, path).split(sep);
|
|
54
|
+
if (parts.some(part => part.startsWith(".") || ["_generated", "node_modules", "tests", "test", "__tests__", "__mocks__"].includes(part)) || !/\.[cm]?[tj]sx?$/.test(path) || /\.(?:d|test|spec)\.[cm]?[tj]sx?$/.test(path) || resolve(path) === source)
|
|
55
|
+
continue;
|
|
56
|
+
const module = parts.join("/").replace(/\.[cm]?[tj]sx?$/, "");
|
|
57
|
+
assertModuleName(module);
|
|
58
|
+
if (selected[module])
|
|
59
|
+
throw new TypeError(`Duplicate function module matched by glob: ${module}`);
|
|
60
|
+
selected[module] = path;
|
|
61
|
+
}
|
|
62
|
+
if (!Object.keys(selected).length)
|
|
63
|
+
throw new TypeError(`Function glob matched no declaration files: ${selection.pattern}`);
|
|
64
|
+
await analysis.open(Object.values(selected));
|
|
65
|
+
const declarations = await Promise.all(Object.entries(selected).map(async ([name, path]) => [name, (await analysis.functions(path)).length ? path : undefined]));
|
|
66
|
+
modules = Object.fromEntries(declarations.filter((entry) => entry[1] !== undefined));
|
|
67
|
+
if (!Object.keys(modules).length)
|
|
68
|
+
throw new TypeError(`Function glob matched no query or mutation declarations: ${selection.pattern}`);
|
|
69
|
+
}
|
|
70
|
+
const selected = Object.create(null);
|
|
71
|
+
const allowed = catalogueImports(analysis, declarations);
|
|
72
|
+
// Validate the selected graph once before evaluating any declaration modules.
|
|
73
|
+
// Namespace exports retain handler bodies and their transitive imports.
|
|
74
|
+
const paths = [...new Set(Object.values(modules))];
|
|
75
|
+
if (paths.length)
|
|
76
|
+
await compileWorker(source, paths.map((path, index) => `export * as module${index} from ${JSON.stringify(path)}`).join('\n'), allowed);
|
|
77
|
+
for (const [name, path] of Object.entries(modules)) {
|
|
78
|
+
assertModuleName(name);
|
|
79
|
+
const values = await import(__rewriteRelativeImportExtension(Bun.pathToFileURL(path).href));
|
|
80
|
+
const functions = Object.values(values).filter(isFunctionDefinition);
|
|
81
|
+
if (database.functions !== undefined && functions.some(value => functionDatabases.get(value) !== database.identity))
|
|
82
|
+
throw new TypeError(`Function catalogue contains declarations owned by another schema: ${name}`);
|
|
83
|
+
if (functions.some(value => functionDatabases.get(value) === database.identity))
|
|
84
|
+
selected[name] = path;
|
|
85
|
+
}
|
|
86
|
+
return { modules: selected, rootModule: rootModule && Object.hasOwn(selected, rootModule) ? rootModule : undefined };
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { generateProject } from "./project-codegen.js";
|
|
3
|
+
import { startService } from "@hulla/db-postgres/service";
|
|
4
|
+
import { startStudio } from "./studio.js";
|
|
5
|
+
import { initializeDatabase, migrateDatabase } from "@hulla/db-postgres/server";
|
|
6
|
+
import { DatabaseError } from "@hulla/db";
|
|
7
|
+
import { resolveDatabaseConnection } from "@hulla/db-postgres/server";
|
|
8
|
+
import { cliError, cliOutput } from "./cli-output.js";
|
|
9
|
+
import { loadHostConfiguration } from "@hulla/db-postgres/internal/host";
|
|
10
|
+
import { AddressConflict, addressURL, startCLIListener, StartupCancelled } from "./cli-address.js";
|
|
11
|
+
let stopping = false;
|
|
12
|
+
let service;
|
|
13
|
+
let studio;
|
|
14
|
+
let cancelDatabaseRequest;
|
|
15
|
+
const startup = new AbortController();
|
|
16
|
+
const addresses = JSON.parse(process.argv[4] ?? "{}");
|
|
17
|
+
function remembered(kind, requested) {
|
|
18
|
+
const choice = addresses[kind];
|
|
19
|
+
return choice?.requested.host === requested.host && choice.requested.port === requested.port ? choice.address : requested;
|
|
20
|
+
}
|
|
21
|
+
function remember(kind, requested, address) {
|
|
22
|
+
process.send?.({ listening: { kind, requested, address } });
|
|
23
|
+
}
|
|
24
|
+
function developmentURL(address) {
|
|
25
|
+
const host = address.host === "0.0.0.0" ? "127.0.0.1" : address.host === "::" ? "::1" : address.host;
|
|
26
|
+
return addressURL({ host, port: address.port });
|
|
27
|
+
}
|
|
28
|
+
const stop = () => {
|
|
29
|
+
stopping = true;
|
|
30
|
+
startup.abort();
|
|
31
|
+
cancelDatabaseRequest?.();
|
|
32
|
+
void service?.close().catch(() => { });
|
|
33
|
+
void studio?.close().catch(() => { });
|
|
34
|
+
};
|
|
35
|
+
process.on("SIGINT", stop);
|
|
36
|
+
process.on("SIGTERM", stop);
|
|
37
|
+
try {
|
|
38
|
+
const path = resolve(process.argv[2]);
|
|
39
|
+
await generateProject(path, config => {
|
|
40
|
+
const schema = resolve(dirname(path), config.schema ?? 'db/schema.ts');
|
|
41
|
+
process.send?.({ watch: [dirname(schema), ...(config.functions ? [resolve(dirname(path), config.functions)] : [])] });
|
|
42
|
+
const initial = remembered("service", { host: config.service?.host ?? "127.0.0.1", port: config.service?.port ?? 3001 });
|
|
43
|
+
return initial.port ? { url: developmentURL(initial) } : undefined;
|
|
44
|
+
});
|
|
45
|
+
const { config, schemaPath, schema: definition } = await loadHostConfiguration(path);
|
|
46
|
+
startup.signal.throwIfAborted();
|
|
47
|
+
process.send?.({ watch: [dirname(schemaPath), ...(config.functions ? [resolve(dirname(path), config.functions)] : [])] });
|
|
48
|
+
const managed = config.database.server.url === undefined && process.env.DATABASE_URL === undefined;
|
|
49
|
+
if (managed) {
|
|
50
|
+
process.env.DATABASE_URL = await new Promise((resolve, reject) => {
|
|
51
|
+
const finish = (url, error) => {
|
|
52
|
+
process.off("message", receive);
|
|
53
|
+
cancelDatabaseRequest = undefined;
|
|
54
|
+
if (error)
|
|
55
|
+
reject(error);
|
|
56
|
+
else
|
|
57
|
+
resolve(url);
|
|
58
|
+
};
|
|
59
|
+
const receive = (message) => {
|
|
60
|
+
if (!message || typeof message !== "object")
|
|
61
|
+
return;
|
|
62
|
+
if ("database" in message && typeof message.database === "string")
|
|
63
|
+
finish(message.database);
|
|
64
|
+
else if ("databaseError" in message && typeof message.databaseError === "string")
|
|
65
|
+
finish(undefined, new Error(message.databaseError));
|
|
66
|
+
};
|
|
67
|
+
cancelDatabaseRequest = () => finish(undefined, new Error("Development stopped"));
|
|
68
|
+
process.on("message", receive);
|
|
69
|
+
process.send?.({ database: true });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const requested = { host: config.service?.host ?? "127.0.0.1", port: config.service?.port ?? 3001 };
|
|
73
|
+
const initial = remembered("service", requested);
|
|
74
|
+
cliOutput.success("Generated database entrypoints.");
|
|
75
|
+
if (managed && !stopping) {
|
|
76
|
+
const connection = resolveDatabaseConnection(config.database.server, path);
|
|
77
|
+
try {
|
|
78
|
+
await initializeDatabase(definition, connection);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (!(error instanceof DatabaseError) || error.code !== "schema_mismatch" || stopping)
|
|
82
|
+
throw error;
|
|
83
|
+
const plan = await migrateDatabase(definition, connection);
|
|
84
|
+
if (plan.changes.length)
|
|
85
|
+
cliOutput.success("Applied development schema migration.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!stopping) {
|
|
89
|
+
const started = await startCLIListener("Database service", initial, address => startService(path, address), { signal: startup.signal });
|
|
90
|
+
service = started.listener;
|
|
91
|
+
if (stopping)
|
|
92
|
+
await service.close();
|
|
93
|
+
else {
|
|
94
|
+
const url = new URL(service.url);
|
|
95
|
+
const actual = { host: url.hostname.replace(/^\[|\]$/g, ""), port: Number(url.port || 80) };
|
|
96
|
+
if (actual.host !== initial.host || actual.port !== initial.port) {
|
|
97
|
+
await generateProject(path, { url: developmentURL(actual) });
|
|
98
|
+
}
|
|
99
|
+
remember("service", requested, actual);
|
|
100
|
+
cliOutput.listening("Database service", service.url);
|
|
101
|
+
const options = JSON.parse(process.argv[3] ?? "null");
|
|
102
|
+
if (options && !stopping) {
|
|
103
|
+
const requested = { host: "127.0.0.1", port: options.port };
|
|
104
|
+
const started = await startCLIListener("Hulla Studio", remembered("studio", requested), address => startStudio(path, { ...options, port: address.port }), { signal: startup.signal, fixedHost: true });
|
|
105
|
+
studio = started.listener;
|
|
106
|
+
if (stopping)
|
|
107
|
+
await studio.close();
|
|
108
|
+
else {
|
|
109
|
+
remember("studio", requested, { ...started.address, port: Number(new URL(studio.url).port || 80) });
|
|
110
|
+
cliOutput.listening("Hulla Studio", studio.url);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
await (studio ? Promise.race([service.closed, studio.closed]) : service.closed);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error instanceof StartupCancelled) {
|
|
119
|
+
cliOutput.info(error.message);
|
|
120
|
+
process.send?.({ cancelStartup: true });
|
|
121
|
+
}
|
|
122
|
+
else if (error instanceof AddressConflict && process.send) {
|
|
123
|
+
process.send({ startupError: error.message });
|
|
124
|
+
}
|
|
125
|
+
else if (!stopping) {
|
|
126
|
+
cliError.error(error instanceof Error ? error.message : "Development startup failed");
|
|
127
|
+
cliError.info("Waiting for source changes before restarting.");
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
process.off("SIGINT", stop);
|
|
133
|
+
process.off("SIGTERM", stop);
|
|
134
|
+
await studio?.close().catch(() => { });
|
|
135
|
+
await service?.close().catch(() => { });
|
|
136
|
+
if (process.connected)
|
|
137
|
+
process.disconnect?.();
|
|
138
|
+
}
|
package/dist/dev.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { StudioFlags } from "./cli-options.ts";
|
|
2
|
+
/** Development-only supervisor. Each generation gets a fresh module cache. */
|
|
3
|
+
export declare function startDevelopment(configPath: string, studio?: StudioFlags): Promise<{
|
|
4
|
+
close(): Promise<void>;
|
|
5
|
+
closed: Promise<void>;
|
|
6
|
+
}>;
|
package/dist/dev.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { lstatSync, watch } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { runtimeExportConditions } from "./runtime-conditions.js";
|
|
4
|
+
import { cliError, cliOutput } from "./cli-output.js";
|
|
5
|
+
const ignored = new Set([
|
|
6
|
+
"_generated", "node_modules", ".git", ".hulla", "dist", "build", "target", "coverage",
|
|
7
|
+
".next", ".nuxt", ".output", ".svelte-kit", ".astro", ".solid", ".cache",
|
|
8
|
+
".turbo", ".vite", ".vercel", ".netlify",
|
|
9
|
+
]);
|
|
10
|
+
function contains(parent, child) {
|
|
11
|
+
const path = relative(parent, child);
|
|
12
|
+
return path === "" || (!isAbsolute(path) && path !== ".." && !path.startsWith(`..${sep}`));
|
|
13
|
+
}
|
|
14
|
+
function relevant(directory, path, event) {
|
|
15
|
+
const parts = path.split(/[\\/]/);
|
|
16
|
+
if (parts.some(part => ignored.has(part)))
|
|
17
|
+
return false;
|
|
18
|
+
const name = parts.at(-1);
|
|
19
|
+
if (/\.(?:log|tmp|swp|swo|map|tsbuildinfo)$/.test(name) || name === ".DS_Store")
|
|
20
|
+
return false;
|
|
21
|
+
if (/\.(?:[cm]?[jt]sx?|jsonc?|ya?ml|toml)$/.test(name) || name === ".env" || name.startsWith(".env.")
|
|
22
|
+
|| ["bun.lock", "bun.lockb", "yarn.lock"].includes(name))
|
|
23
|
+
return true;
|
|
24
|
+
if (event !== "rename")
|
|
25
|
+
return false;
|
|
26
|
+
// A newly created/moved directory can introduce declarations at any depth.
|
|
27
|
+
// Deleted paths cannot be classified, so conservatively refresh discovery.
|
|
28
|
+
try {
|
|
29
|
+
return lstatSync(resolve(directory, path)).isDirectory();
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Development-only supervisor. Each generation gets a fresh module cache. */
|
|
36
|
+
export async function startDevelopment(configPath, studio) {
|
|
37
|
+
if (typeof Bun === "undefined")
|
|
38
|
+
throw new Error("hulla-db dev requires Bun. Use Node or Bun with hulla-db serve for generated applications.");
|
|
39
|
+
const config = resolve(configPath), root = dirname(config);
|
|
40
|
+
const studioToken = studio ? crypto.getRandomValues(Buffer.alloc(32)).toString("base64url") : undefined;
|
|
41
|
+
const watchers = new Map();
|
|
42
|
+
const addresses = {};
|
|
43
|
+
let child;
|
|
44
|
+
let timer;
|
|
45
|
+
let stopped = false, revision = 0, chain = Promise.resolve();
|
|
46
|
+
const databaseAbort = new AbortController();
|
|
47
|
+
let database;
|
|
48
|
+
let terminalError;
|
|
49
|
+
let finish;
|
|
50
|
+
const closed = new Promise((resolve, reject) => { finish = () => terminalError ? reject(terminalError) : resolve(); });
|
|
51
|
+
async function stopChild() {
|
|
52
|
+
if (!child)
|
|
53
|
+
return;
|
|
54
|
+
const previous = child;
|
|
55
|
+
child = undefined;
|
|
56
|
+
previous.kill("SIGTERM");
|
|
57
|
+
const force = setTimeout(() => previous.kill("SIGKILL"), 5000);
|
|
58
|
+
try {
|
|
59
|
+
await previous.exited;
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
clearTimeout(force);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function schedule() {
|
|
66
|
+
if (stopped)
|
|
67
|
+
return;
|
|
68
|
+
const next = ++revision;
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
timer = setTimeout(() => {
|
|
71
|
+
chain = chain.then(async () => {
|
|
72
|
+
await stopChild();
|
|
73
|
+
if (stopped || next !== revision)
|
|
74
|
+
return;
|
|
75
|
+
cliOutput.info("Source changed; restarting development…");
|
|
76
|
+
launch();
|
|
77
|
+
}).catch(() => { cliError.error("Development restart failed; waiting for the next source change."); });
|
|
78
|
+
}, 100);
|
|
79
|
+
}
|
|
80
|
+
function watchDirectory(directory) {
|
|
81
|
+
const path = resolve(directory);
|
|
82
|
+
if ([...watchers.keys()].some(parent => contains(parent, path)))
|
|
83
|
+
return;
|
|
84
|
+
try {
|
|
85
|
+
const watcher = watch(path, { recursive: true }, (event, filename) => {
|
|
86
|
+
if (filename === null || relevant(path, String(filename), event))
|
|
87
|
+
schedule();
|
|
88
|
+
});
|
|
89
|
+
watcher.on("error", () => {
|
|
90
|
+
watcher.close();
|
|
91
|
+
watchers.delete(path);
|
|
92
|
+
cliError.warn(`Cannot watch ${cliError.file(path)}; restart dev after restoring this directory.`);
|
|
93
|
+
});
|
|
94
|
+
watchers.set(path, watcher);
|
|
95
|
+
for (const [existing, handle] of watchers) {
|
|
96
|
+
if (existing !== path && contains(path, existing)) {
|
|
97
|
+
handle.close();
|
|
98
|
+
watchers.delete(existing);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
cliError.warn(`Cannot watch ${cliError.file(path)}; changes there require restarting dev.`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function launch() {
|
|
107
|
+
const runtimeArguments = runtimeExportConditions().map(condition => `--conditions=${condition}`);
|
|
108
|
+
const workerPath = Bun.fileURLToPath(new URL(import.meta.url.endsWith(".ts") ? "./dev-worker.ts" : "./dev-worker.js", import.meta.url));
|
|
109
|
+
try {
|
|
110
|
+
child = Bun.spawn([process.execPath, ...runtimeArguments, workerPath, config, JSON.stringify(studio ?? null), JSON.stringify(addresses)], {
|
|
111
|
+
stdin: "inherit", stdout: "inherit", stderr: "inherit",
|
|
112
|
+
env: { ...process.env, ...(studioToken ? { HULLADB_STUDIO_TOKEN: studioToken } : {}) },
|
|
113
|
+
ipc(message, sender) {
|
|
114
|
+
if (stopped || child !== sender || !message || typeof message !== "object")
|
|
115
|
+
return;
|
|
116
|
+
if ("cancelStartup" in message && message.cancelStartup === true) {
|
|
117
|
+
void handle.close().catch(() => { });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if ("startupError" in message && typeof message.startupError === "string") {
|
|
121
|
+
terminalError = new Error(message.startupError);
|
|
122
|
+
void handle.close().catch(() => { });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if ("listening" in message && message.listening && typeof message.listening === "object") {
|
|
126
|
+
const choice = message.listening;
|
|
127
|
+
if (choice.kind === "service" || choice.kind === "studio")
|
|
128
|
+
addresses[choice.kind] = choice;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if ("database" in message && message.database === true) {
|
|
132
|
+
database ??= import('./local-postgres.js').then(({ startLocalPostgres }) => startLocalPostgres(config, databaseAbort.signal)).then(local => {
|
|
133
|
+
void local.closed.catch(error => {
|
|
134
|
+
if (!stopped) {
|
|
135
|
+
terminalError = error;
|
|
136
|
+
void handle.close().catch(() => { });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return local;
|
|
140
|
+
}).catch(error => { database = undefined; throw error; });
|
|
141
|
+
const respond = (reply) => {
|
|
142
|
+
if (!stopped && child === sender) {
|
|
143
|
+
try {
|
|
144
|
+
sender.send(reply);
|
|
145
|
+
}
|
|
146
|
+
catch { /* The worker can exit while PostgreSQL starts. */ }
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
void database.then(local => {
|
|
150
|
+
respond({ database: local.url });
|
|
151
|
+
}, error => {
|
|
152
|
+
respond({ databaseError: error instanceof Error ? error.message : "Cannot start local PostgreSQL" });
|
|
153
|
+
});
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!("watch" in message) || !Array.isArray(message.watch))
|
|
157
|
+
return;
|
|
158
|
+
const roots = [root, ...message.watch.filter((path) => typeof path === "string").map(path => resolve(path))];
|
|
159
|
+
const desired = roots.filter((path, index) => !roots.some((parent, other) => other !== index && contains(parent, path) && (parent !== path || other < index)));
|
|
160
|
+
for (const [path, watcher] of watchers) {
|
|
161
|
+
if (!desired.includes(path)) {
|
|
162
|
+
watcher.close();
|
|
163
|
+
watchers.delete(path);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (const path of desired)
|
|
167
|
+
watchDirectory(path);
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
cliError.error("Cannot start development worker; waiting for the next source change.");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
watchDirectory(root);
|
|
176
|
+
if (!watchers.size)
|
|
177
|
+
throw new Error("Cannot watch the project directory");
|
|
178
|
+
launch();
|
|
179
|
+
let closing;
|
|
180
|
+
const handle = {
|
|
181
|
+
closed,
|
|
182
|
+
close() {
|
|
183
|
+
closing ??= (async () => {
|
|
184
|
+
stopped = true;
|
|
185
|
+
revision++;
|
|
186
|
+
clearTimeout(timer);
|
|
187
|
+
for (const watcher of watchers.values())
|
|
188
|
+
watcher.close();
|
|
189
|
+
watchers.clear();
|
|
190
|
+
try {
|
|
191
|
+
await chain;
|
|
192
|
+
await stopChild();
|
|
193
|
+
databaseAbort.abort();
|
|
194
|
+
const local = await database?.catch(() => undefined);
|
|
195
|
+
await local?.close();
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
terminalError ??= error;
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
finish();
|
|
203
|
+
}
|
|
204
|
+
})();
|
|
205
|
+
return closing;
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
return handle;
|
|
209
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { API, SymbolFlags, TypeFlags, type Checker, type Type, type Project, type Snapshot, type Symbol } from "typescript/unstable/async";
|
|
2
|
+
export { SyntaxKind, isExportSpecifier, isImportSpecifier, isExportDeclaration, isImportDeclaration, isImportClause, isVariableDeclaration, isExportAssignment, isCallExpression, isIdentifier, isObjectLiteralExpression, isPropertyAssignment, isArrowFunction, isFunctionExpression, isStringLiteral, isParenthesizedExpression, isAsExpression, isSatisfiesExpression, isNonNullExpression, isBlock, isReturnStatement, isShorthandPropertyAssignment, } from "typescript/unstable/ast";
|
|
3
|
+
export declare function isDateLibrary(path: string): boolean;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { API, SymbolFlags, TypeFlags } from "typescript/unstable/async";
|
|
2
|
+
export { SyntaxKind, isExportSpecifier, isImportSpecifier, isExportDeclaration, isImportDeclaration, isImportClause, isVariableDeclaration, isExportAssignment, isCallExpression, isIdentifier, isObjectLiteralExpression, isPropertyAssignment, isArrowFunction, isFunctionExpression, isStringLiteral, isParenthesizedExpression, isAsExpression, isSatisfiesExpression, isNonNullExpression, isBlock, isReturnStatement, isShorthandPropertyAssignment, } from "typescript/unstable/ast";
|
|
3
|
+
export function isDateLibrary(path) {
|
|
4
|
+
return /\/node_modules\/(?:typescript|@typescript\/typescript-[^/]+)\/lib\/lib\.[^/]+\.d\.ts$/i.test(path);
|
|
5
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { runtimeExportConditions } from "./runtime-conditions.js";
|
|
3
|
+
/** Runs in the Node CLI before Bun is available, so this bootstrap uses Node APIs. */
|
|
4
|
+
export async function runDevelopmentCLI() {
|
|
5
|
+
const { spawn } = await import("node:child_process");
|
|
6
|
+
const conditions = runtimeExportConditions().map(condition => `--conditions=${condition}`);
|
|
7
|
+
const child = spawn("bun", [...conditions, fileURLToPath(new URL(import.meta.url.endsWith(".ts") ? "./cli.ts" : "./cli.js", import.meta.url)), ...process.argv.slice(2)], {
|
|
8
|
+
stdio: "inherit",
|
|
9
|
+
});
|
|
10
|
+
const stop = () => { child.kill("SIGTERM"); };
|
|
11
|
+
process.on("SIGINT", stop);
|
|
12
|
+
process.on("SIGTERM", stop);
|
|
13
|
+
try {
|
|
14
|
+
process.exitCode = await new Promise((resolve, reject) => {
|
|
15
|
+
child.once("error", (error) => reject(error.code === "ENOENT"
|
|
16
|
+
? new Error("This hulla-db command requires Bun on PATH. Install Bun and rerun the command.")
|
|
17
|
+
: error));
|
|
18
|
+
child.once("close", code => resolve(code ?? 1));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
process.off("SIGINT", stop);
|
|
23
|
+
process.off("SIGTERM", stop);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type Project, type Symbol } from "./development-compiler.ts";
|
|
2
|
+
interface SourceSpan {
|
|
3
|
+
readonly file: string;
|
|
4
|
+
readonly start: number;
|
|
5
|
+
readonly end: number;
|
|
6
|
+
}
|
|
7
|
+
export interface DatabaseDeclaration {
|
|
8
|
+
readonly exportName: string;
|
|
9
|
+
readonly call: SourceSpan;
|
|
10
|
+
readonly factory: SourceSpan;
|
|
11
|
+
readonly argument: SourceSpan;
|
|
12
|
+
readonly schemaExport?: {
|
|
13
|
+
readonly file: string;
|
|
14
|
+
readonly name: string;
|
|
15
|
+
};
|
|
16
|
+
readonly factoryExport?: {
|
|
17
|
+
readonly file: string;
|
|
18
|
+
readonly name: string;
|
|
19
|
+
};
|
|
20
|
+
readonly functions?: SourceSpan & {
|
|
21
|
+
readonly shorthand?: true;
|
|
22
|
+
} & ({
|
|
23
|
+
readonly kind: "glob";
|
|
24
|
+
readonly pattern: string;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "import" | "namespace";
|
|
27
|
+
readonly specifier: string;
|
|
28
|
+
readonly importer: string;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export interface FunctionExport {
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly symbol: Symbol;
|
|
34
|
+
readonly original: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** One compiler session shared by discovery, selection and contract inference. */
|
|
37
|
+
export declare class FunctionAnalysis {
|
|
38
|
+
private readonly api;
|
|
39
|
+
private snapshot?;
|
|
40
|
+
private readonly projects;
|
|
41
|
+
private readonly exports;
|
|
42
|
+
private readonly symbols;
|
|
43
|
+
private readonly runtime;
|
|
44
|
+
private readonly declarationFiles;
|
|
45
|
+
private readonly brands;
|
|
46
|
+
private readonly opened;
|
|
47
|
+
private readonly databases;
|
|
48
|
+
open(files: readonly string[]): Promise<void>;
|
|
49
|
+
project(source: string): Promise<Project>;
|
|
50
|
+
private moduleExports;
|
|
51
|
+
private moduleFile;
|
|
52
|
+
private requiredBinding;
|
|
53
|
+
private valueBinding;
|
|
54
|
+
private runtimeExport;
|
|
55
|
+
functions(source: string): Promise<readonly FunctionExport[]>;
|
|
56
|
+
/** Locate configuration through exported aliases without evaluating a loader. */
|
|
57
|
+
databaseDeclarations(source: string): Promise<readonly DatabaseDeclaration[]>;
|
|
58
|
+
/** Namespace barrel exports describe module groups; flat exports form one module. */
|
|
59
|
+
catalogue(source: string, prefix: string): Promise<Readonly<Record<string, string>>>;
|
|
60
|
+
close(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
export {};
|