@protobase/cli 0.47.1 → 0.49.0
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/dist/build/config-bundle.js +6 -1
- package/dist/build/entry-code.js +30 -17
- package/dist/cli/package.js +1 -1
- package/dist/config/build-project.js +70 -0
- package/dist/config/called-config.js +11 -0
- package/dist/config/config-defaults.js +30 -0
- package/dist/config/env-values.js +20 -0
- package/dist/config/lazy-imports.js +18 -0
- package/dist/config/merge-config.js +72 -0
- package/dist/config/project-files.js +48 -0
- package/dist/config/project-parts.js +201 -0
- package/dist/config/read-config.js +31 -0
- package/dist/config/source-config.js +37 -0
- package/dist/dev/api-child.js +2 -1
- package/dist/dev/api-process.d.ts +1 -0
- package/dist/dev/api-process.js +5 -2
- package/dist/dev/auth-drift-lines.js +8 -0
- package/dist/dev/create-db.js +2 -2
- package/dist/dev/entry.d.ts +5 -0
- package/dist/dev/entry.js +17 -12
- package/dist/dev/project.js +23 -47
- package/dist/dev/run.js +3 -2
- package/dist/main.js +3 -3
- package/dist/migration/load-project.js +34 -34
- package/dist/migration/parts.js +2 -2
- package/dist/migration/summary.js +2 -2
- package/dist/mobile/api-origin.js +4 -4
- package/dist/mobile/load-project.js +13 -12
- package/dist/mobile/register.js +1 -1
- package/dist/mobile/run.js +1 -1
- package/dist/organizations/register.js +1 -1
- package/dist/project/error-reporter.js +3 -3
- package/dist/project/import-project-file.js +4 -2
- package/dist/project/load-bundle-project.js +16 -25
- package/dist/project/load-data-config.js +2 -2
- package/dist/project/pg-db.js +10 -4
- package/dist/project/placeholder-secret.js +2 -2
- package/dist/project/project-admin.js +12 -11
- package/dist/scaffold/plan.js +7 -3
- package/dist/scaffold/protobase-tables.js +8 -13
- package/dist/scaffold/run.js +1 -1
- package/dist/serve/create-admin.js +9 -4
- package/dist/serve/host-module-ids.js +1 -0
- package/dist/serve/host-modules.js +3 -1
- package/dist/serve/lifecycle.js +19 -14
- package/dist/serve/load-bundle.js +41 -11
- package/dist/serve/mobile-release.js +1 -1
- package/dist/serve/serve-bundle.js +3 -3
- package/dist/supabase/auth-config.js +33 -28
- package/dist/supabase/config-jobs.js +28 -27
- package/dist/supabase/config-object.js +25 -0
- package/dist/supabase/cron-job-file.js +13 -14
- package/dist/supabase/cron-jobs-write.js +6 -12
- package/dist/supabase/cron-plan.js +3 -51
- package/dist/supabase/project-files.js +4 -9
- package/dist/supabase/report.js +3 -3
- package/dist/users/load-auth.js +16 -15
- package/dist/worker/start-worker.js +18 -15
- package/package.json +8 -6
- package/skills/protobase/SKILL.md +2 -1
- package/skills/protobase/references/features/notifications.md +1 -1
- package/skills/protobase/references/features/sign-in-and-accounts.md +1 -0
- package/skills/protobase/references/features/start-a-project.md +3 -0
- package/skills/protobase/references/getting-started.md +2 -2
- package/skills/protobase/references/guides/api-functions.md +4 -4
- package/skills/protobase/references/guides/deploy.md +8 -8
- package/skills/protobase/references/guides/existing-database.md +11 -31
- package/skills/protobase/references/guides/first-plugin.md +1 -1
- package/skills/protobase/references/guides/mobile-app.md +5 -3
- package/skills/protobase/references/guides/plugin-build.md +1 -1
- package/skills/protobase/references/guides/push-notifications.md +6 -7
- package/skills/protobase/references/guides/remote-resources.md +22 -31
- package/skills/protobase/references/reference/access.md +3 -3
- package/skills/protobase/references/reference/ai.md +1 -9
- package/skills/protobase/references/reference/api.md +15 -15
- package/skills/protobase/references/reference/app-identity.md +9 -10
- package/skills/protobase/references/reference/assistant.md +6 -6
- package/skills/protobase/references/reference/auth.md +126 -96
- package/skills/protobase/references/reference/bottom-bar.md +1 -1
- package/skills/protobase/references/reference/cli.md +36 -86
- package/skills/protobase/references/reference/comments.md +0 -8
- package/skills/protobase/references/reference/config.md +152 -0
- package/skills/protobase/references/reference/data-config.md +2 -2
- package/skills/protobase/references/reference/email.md +36 -37
- package/skills/protobase/references/reference/events.md +1 -1
- package/skills/protobase/references/reference/files.md +18 -23
- package/skills/protobase/references/reference/forms.md +5 -17
- package/skills/protobase/references/reference/history.md +2 -3
- package/skills/protobase/references/reference/installable-app.md +1 -1
- package/skills/protobase/references/reference/jobs.md +53 -56
- package/skills/protobase/references/reference/languages.md +4 -2
- package/skills/protobase/references/reference/mcp.md +11 -11
- package/skills/protobase/references/reference/notifications.md +26 -24
- package/skills/protobase/references/reference/plugins.md +11 -14
- package/skills/protobase/references/reference/remote-resources.md +18 -24
- package/skills/protobase/references/reference/supabase-realtime.md +1 -1
- package/skills/protobase/references/reference/supabase-rest.md +1 -1
- package/skills/protobase/references/reference/supabase-storage.md +1 -1
- package/skills/protobase/references/reference/ui-config.md +15 -17
- package/skills/protobase/references/reference/versioning.md +1 -1
- package/skills/protobase/references/reference/webhooks.md +6 -7
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { findPackageDir, packageName } from "./packages/package-dir.js";
|
|
2
|
+
import { loadSourceConfig } from "../config/source-config.js";
|
|
2
3
|
import { layoutCheck } from "./layout-check.js";
|
|
3
4
|
import { extensionImports } from "../project/extensions.js";
|
|
4
5
|
import { viteBundle } from "./vite-bundle.js";
|
|
@@ -66,6 +67,10 @@ const servedImports = (native) => {
|
|
|
66
67
|
};
|
|
67
68
|
};
|
|
68
69
|
const buildConfigBundle = async ({ projectDir, outFile, bun, extensions = [], components = /* @__PURE__ */ new Set() }) => {
|
|
70
|
+
const config = await loadSourceConfig(projectDir, {
|
|
71
|
+
command: "build",
|
|
72
|
+
extensions
|
|
73
|
+
});
|
|
69
74
|
const file = path.join(projectDir, "protobase-bundle-entry.js");
|
|
70
75
|
const native = /* @__PURE__ */ new Map();
|
|
71
76
|
const plugins = [
|
|
@@ -73,7 +78,7 @@ const buildConfigBundle = async ({ projectDir, outFile, bun, extensions = [], co
|
|
|
73
78
|
collectComponents(components),
|
|
74
79
|
extensionImports(projectDir, extensions),
|
|
75
80
|
servedImports(native),
|
|
76
|
-
entry(file, bundleEntryCode(projectDir, extensions))
|
|
81
|
+
entry(file, bundleEntryCode(projectDir, extensions, config.files?.images ? [config.files.images] : []))
|
|
77
82
|
];
|
|
78
83
|
await viteBundle({
|
|
79
84
|
root: projectDir,
|
package/dist/build/entry-code.js
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
|
+
import { uiDirs } from "../project/convention-files.js";
|
|
1
2
|
import { functionFiles } from "../project/functions-folder.js";
|
|
3
|
+
import { conventionModules } from "../config/project-files.js";
|
|
4
|
+
import { configFileOf } from "../config/read-config.js";
|
|
2
5
|
import { moduleFile } from "../module-file.js";
|
|
3
6
|
import { exportOrder } from "./export-order.js";
|
|
4
|
-
import { uiDirs } from "../project/convention-files.js";
|
|
5
7
|
import { existsSync } from "node:fs";
|
|
6
8
|
import path from "node:path";
|
|
7
9
|
//#region src/build/entry-code.ts
|
|
8
10
|
const conventionModule = moduleFile("../project/convention", import.meta.url);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const configDir = path.join(
|
|
11
|
+
const json = (value) => JSON.stringify(value);
|
|
12
|
+
const filesCode = (dir) => {
|
|
13
|
+
const configDir = path.join(dir, "config");
|
|
12
14
|
const index = path.join(configDir, "index.ts");
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
return `{ ${existsSync(index) ? `resources: async () => conventionConfig(await import(${json(index)}), [${uiDirs(configDir).map((ui) => `[${json(ui)}, await import(${json(path.join(configDir, ui, "ui.ts"))})]`).join(", ")}], ${json(exportOrder(index))}), ` : ""}functions: { ${functionFiles(dir).map(({ name, file }) => `${json(name)}: () => import(${json(file)})`).join(", ")} }, modules: { ${conventionModules(dir).map((file) => `${json(file)}: () => import(${json(path.join(dir, file))})`).join(", ")} } }`;
|
|
16
|
+
};
|
|
17
|
+
const configCode = (file, name) => {
|
|
18
|
+
if (!file) return [`const ${name} = {}`];
|
|
19
|
+
if (file.endsWith(".json")) return [`import ${name} from ${json(file)}`];
|
|
20
|
+
return [`import * as ${name}Module from ${json(file)}`, `const ${name} = ${name}Module.default`];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The bundle's config module (see `BundleModule`): the project's config and the extensions' as they are, the code
|
|
24
|
+
* their conventions find as references to its files, and the packages the config names. The serve runtime reads the
|
|
25
|
+
* configs again with its own environment, and merges them as `protobase dev` does.
|
|
26
|
+
*/
|
|
27
|
+
const bundleEntryCode = (projectDir, extensions = [], packages = []) => {
|
|
28
|
+
const file = configFileOf(projectDir);
|
|
29
|
+
if (!file && !existsSync(path.join(projectDir, "config", "index.ts"))) throw new Error(`No protobase.config.ts, protobase.config.json or config/index.ts in ${projectDir}`);
|
|
30
|
+
const configs = extensions.flatMap((extension) => extension.config ? [extension] : []);
|
|
31
|
+
return `${[
|
|
32
|
+
`import { conventionConfig } from ${json(conventionModule)}`,
|
|
33
|
+
...configCode(file, "config"),
|
|
34
|
+
`export const own = { config, label: ${json(file ? path.basename(file) : "config/index.ts")}, files: ${filesCode(projectDir)} }`,
|
|
35
|
+
...configs.flatMap((extension, i) => configCode(extension.config, `extension${i}`)),
|
|
36
|
+
`export const extensions = [${configs.map((extension, i) => `{ config: extension${i}, label: ${json(path.basename(extension.dir))}, files: ${filesCode(extension.dir)} }`).join(", ")}]`,
|
|
37
|
+
`export const packages = { ${packages.map((name) => `${json(name)}: () => import(${json(name)})`).join(", ")} }`
|
|
38
|
+
].join("\n")}\n`;
|
|
26
39
|
};
|
|
27
40
|
//#endregion
|
|
28
41
|
export { bundleEntryCode };
|
package/dist/cli/package.js
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { projectFiles, projectFunctions, projectJobs, projectNotifications, projectOptions, projectResources, projectWebhooks } from "./project-parts.js";
|
|
2
|
+
import { buildAppAuth, buildAuthenticator } from "@protobase/server/internal";
|
|
3
|
+
//#region src/config/build-project.ts
|
|
4
|
+
/**
|
|
5
|
+
* The app's sign-in, built from `auth` with the app's URL, secret and name, the hooks of its auth/hooks.ts and the
|
|
6
|
+
* mailer of its auth/mailer.ts when it has them; none without `auth`.
|
|
7
|
+
*/
|
|
8
|
+
const projectAuth = async (config, { db, pool }) => {
|
|
9
|
+
if (!config.auth) return void 0;
|
|
10
|
+
if (!pool) throw new Error("auth needs the app's pool, which its store shares");
|
|
11
|
+
const { url, secret, name } = config;
|
|
12
|
+
if (!url) throw new Error("Sign-in needs the app's public URL: set PROTOBASE_URL, such as https://admin.example.com, or url in protobase.config.ts");
|
|
13
|
+
if (!secret) throw new Error("Sign-in needs the app's secret: set PROTOBASE_SECRET to the output of `openssl rand -base64 32`, or secret in protobase.config.ts");
|
|
14
|
+
const [hooks, mailer] = await Promise.all(["auth/hooks.ts", "auth/mailer.ts"].map((file) => config.authFiles?.module(file)?.()));
|
|
15
|
+
return buildAppAuth({
|
|
16
|
+
...config.auth || {},
|
|
17
|
+
baseURL: url,
|
|
18
|
+
secret,
|
|
19
|
+
...name && { appName: name }
|
|
20
|
+
}, {
|
|
21
|
+
pool,
|
|
22
|
+
db,
|
|
23
|
+
...hooks && { hooks },
|
|
24
|
+
...mailer && { mailer: mailer.default }
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* How the API verifies tokens: the default export of the app's auth/authenticate.ts when it has one, such as a
|
|
29
|
+
* `jwtAuthenticator` for tokens of another system; otherwise the app's sign-in with the `authenticate` settings.
|
|
30
|
+
*/
|
|
31
|
+
const projectAuthenticate = async (config, auth) => {
|
|
32
|
+
const own = config.sources.map(({ files }) => files.module("auth/authenticate.ts")).findLast(Boolean);
|
|
33
|
+
if (own) return (await own()).default;
|
|
34
|
+
return buildAuthenticator(config.authenticate, auth);
|
|
35
|
+
};
|
|
36
|
+
/** The app, built from its merged config over its database: every reference loaded, auth and the authenticator made. */
|
|
37
|
+
const buildProject = async (config, database) => {
|
|
38
|
+
const [resources, functions, files, options, auth, webhooks, notifications] = await Promise.all([
|
|
39
|
+
projectResources(config),
|
|
40
|
+
projectFunctions(config),
|
|
41
|
+
projectFiles(config),
|
|
42
|
+
projectOptions(config),
|
|
43
|
+
projectAuth(config, database),
|
|
44
|
+
projectWebhooks(config),
|
|
45
|
+
projectNotifications(config)
|
|
46
|
+
]);
|
|
47
|
+
const jobs = projectJobs(config);
|
|
48
|
+
const identity = config.themeColor || config.backgroundColor ? {
|
|
49
|
+
...config.themeColor && { themeColor: config.themeColor },
|
|
50
|
+
...config.backgroundColor && { backgroundColor: config.backgroundColor }
|
|
51
|
+
} : void 0;
|
|
52
|
+
return {
|
|
53
|
+
config: resources,
|
|
54
|
+
authenticate: await projectAuthenticate(config, auth),
|
|
55
|
+
jobs,
|
|
56
|
+
...auth && { auth },
|
|
57
|
+
...functions && { functions },
|
|
58
|
+
...files && { files },
|
|
59
|
+
...config.events && { events: config.events },
|
|
60
|
+
...config.history && { history: config.history },
|
|
61
|
+
...webhooks && { webhooks },
|
|
62
|
+
...notifications && { notifications },
|
|
63
|
+
...config.mobile && { mobile: config.mobile },
|
|
64
|
+
...config.secret && { secret: config.secret },
|
|
65
|
+
...identity && { app: identity },
|
|
66
|
+
options
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
|
70
|
+
export { buildProject, projectAuth, projectAuthenticate };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { resolveEnvValues } from "./env-values.js";
|
|
2
|
+
//#region src/config/called-config.ts
|
|
3
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
/** The config `input` describes for `configEnv`: itself, or what its function returns, with its `$env` values read. */
|
|
5
|
+
const calledConfig = async (input, configEnv, label) => {
|
|
6
|
+
const config = typeof input === "function" ? await input(configEnv) : input;
|
|
7
|
+
if (!isObject(config)) throw new Error(`${label} must describe a config: an object, or in TypeScript a function that returns one`);
|
|
8
|
+
return resolveEnvValues(config, configEnv.env, label);
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { calledConfig };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/config/config-defaults.ts
|
|
2
|
+
const listOf = (text) => text?.split(",").map((part) => part.trim()).filter(Boolean);
|
|
3
|
+
/**
|
|
4
|
+
* The merged config with Protobase's defaults filled in where it leaves a key out:
|
|
5
|
+
*
|
|
6
|
+
* - `url`: `PROTOBASE_URL`, or in `protobase dev` the dev server's own address;
|
|
7
|
+
* - `secret`: `PROTOBASE_SECRET`;
|
|
8
|
+
* - `auth`: on, with its own defaults, unless it is `false` or `authenticate.jwksUrl` trusts an auth server elsewhere;
|
|
9
|
+
* - `auth.trustedOrigins`: `PROTOBASE_TRUSTED_ORIGINS`, comma separated.
|
|
10
|
+
*
|
|
11
|
+
* The other keys' defaults are the API's own, applied where they are used (`apiPath` `/api/v1`, `timezone` UTC, ...).
|
|
12
|
+
*/
|
|
13
|
+
const withDefaults = (config, command, env) => {
|
|
14
|
+
const url = config.url ?? (env.PROTOBASE_URL?.trim() || void 0) ?? (command === "dev" ? env.PROTOBASE_DEV_URL : void 0);
|
|
15
|
+
const secret = config.secret ?? (env.PROTOBASE_SECRET?.trim() || void 0);
|
|
16
|
+
const authOn = config.auth !== false && (config.auth !== void 0 || !config.authenticate?.jwksUrl);
|
|
17
|
+
const trustedOrigins = listOf(env.PROTOBASE_TRUSTED_ORIGINS);
|
|
18
|
+
const { auth: _, ...rest } = config;
|
|
19
|
+
return {
|
|
20
|
+
...rest,
|
|
21
|
+
...url && { url },
|
|
22
|
+
...secret && { secret },
|
|
23
|
+
...authOn && { auth: {
|
|
24
|
+
...trustedOrigins && { trustedOrigins },
|
|
25
|
+
...config.auth || {}
|
|
26
|
+
} }
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
export { withDefaults };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/config/env-values.ts
|
|
2
|
+
const isEnvValue = (value) => typeof value === "object" && value !== null && typeof value.$env === "string";
|
|
3
|
+
const envValue = ({ $env, default: fallback, split }, env) => {
|
|
4
|
+
const text = env[$env]?.trim() || void 0;
|
|
5
|
+
if (text === void 0) return fallback;
|
|
6
|
+
return split === void 0 ? text : text.split(split).map((part) => part.trim()).filter(Boolean);
|
|
7
|
+
};
|
|
8
|
+
/** `value` with every `$env` value in it read from `env`; one whose variable is unset and has no default is left out. */
|
|
9
|
+
const resolveEnvValues = (value, env, label) => {
|
|
10
|
+
if (isEnvValue(value)) return envValue(value, env);
|
|
11
|
+
if (Array.isArray(value)) return value.map((item) => resolveEnvValues(item, env, label));
|
|
12
|
+
if (typeof value !== "object" || value === null || Object.getPrototypeOf(value) !== Object.prototype) return value;
|
|
13
|
+
return Object.fromEntries(Object.entries(value).flatMap(([key, item]) => {
|
|
14
|
+
if (key === "$env") throw new Error(`${label}: "$env" names a variable by a string, not ${JSON.stringify(item)}`);
|
|
15
|
+
const resolved = resolveEnvValues(item, env, label);
|
|
16
|
+
return resolved === void 0 ? [] : [[key, resolved]];
|
|
17
|
+
}));
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { resolveEnvValues };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/config/lazy-imports.ts
|
|
2
|
+
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
3
|
+
const literalOf = (node) => node?.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
4
|
+
/** The paths of the config's `import()` calls, `(computed)` for one it computes. */
|
|
5
|
+
const lazyImports = (program) => {
|
|
6
|
+
const found = [];
|
|
7
|
+
const walk = (node) => {
|
|
8
|
+
if (node.type === "ImportExpression") found.push(literalOf(node.source) ?? "(computed)");
|
|
9
|
+
for (const [key, value] of Object.entries(node)) {
|
|
10
|
+
if (key === "parent") continue;
|
|
11
|
+
for (const child of Array.isArray(value) ? value : [value]) if (isNode(child)) walk(child);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
walk(program);
|
|
15
|
+
return found;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { lazyImports };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/config/merge-config.ts
|
|
2
|
+
const lastSet = (objects) => Object.fromEntries(objects.flatMap((object) => Object.entries(object)).filter(([, value]) => value !== void 0));
|
|
3
|
+
/** A name defined by two different values in two configs is an error naming both. */
|
|
4
|
+
const checkUnique = (kind, entries) => {
|
|
5
|
+
const seen = /* @__PURE__ */ new Map();
|
|
6
|
+
for (const { name, value, label } of entries) {
|
|
7
|
+
const first = seen.get(name);
|
|
8
|
+
if (first && first.value !== value) throw new Error(`${kind} "${name}" is defined twice, ${first.label === label ? `in ${label}` : `by ${first.label} and by ${label}`}; rename one of them`);
|
|
9
|
+
if (!first) seen.set(name, {
|
|
10
|
+
value,
|
|
11
|
+
label
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const mergedFiles = (sources) => {
|
|
16
|
+
const all = sources.flatMap(({ config, label }) => config.files ? [{
|
|
17
|
+
files: config.files,
|
|
18
|
+
label
|
|
19
|
+
}] : []);
|
|
20
|
+
if (all.length === 0) return void 0;
|
|
21
|
+
const providers = all.flatMap(({ files, label }) => Object.entries(files.providers ?? {}).map(([name, value]) => ({
|
|
22
|
+
name,
|
|
23
|
+
value,
|
|
24
|
+
label
|
|
25
|
+
})));
|
|
26
|
+
checkUnique("The file provider", providers);
|
|
27
|
+
const trustedHosts = [...new Set(all.flatMap(({ files }) => files.trustedHosts ?? []))];
|
|
28
|
+
const { providers: _, trustedHosts: __, ...rest } = lastSet(all.map(({ files }) => files));
|
|
29
|
+
return {
|
|
30
|
+
...rest,
|
|
31
|
+
...providers.length > 0 && { providers: Object.fromEntries(providers.map(({ name, value }) => [name, value])) },
|
|
32
|
+
...trustedHosts.length > 0 && { trustedHosts }
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* One config from several, the app's own last. Jobs and file providers are named things: one name declared by two
|
|
37
|
+
* configs is an error naming both. Write hooks run in the same order. Every other value is the last config's that sets
|
|
38
|
+
* it, so the app's own wins.
|
|
39
|
+
*/
|
|
40
|
+
const mergeConfig = (...sources) => {
|
|
41
|
+
const data = lastSet(sources.map(({ config: { jobs: _, writeHooks: __, files: ___, ...rest } }) => rest));
|
|
42
|
+
const jobs = sources.flatMap(({ config, files, label }) => Object.entries(config.jobs ?? {}).map(([name, declaration]) => ({
|
|
43
|
+
name,
|
|
44
|
+
value: declaration,
|
|
45
|
+
files,
|
|
46
|
+
label
|
|
47
|
+
})));
|
|
48
|
+
checkUnique("The job", jobs);
|
|
49
|
+
const files = mergedFiles(sources);
|
|
50
|
+
const authFiles = (sources.findLast(({ config }) => config.auth) ?? sources.at(-1))?.files;
|
|
51
|
+
return {
|
|
52
|
+
...data,
|
|
53
|
+
...files && { files },
|
|
54
|
+
...authFiles && { authFiles },
|
|
55
|
+
sources: sources.map(({ files, label }) => ({
|
|
56
|
+
files,
|
|
57
|
+
label
|
|
58
|
+
})),
|
|
59
|
+
jobs: Object.fromEntries(jobs.map(({ name, value, files, label }) => [name, {
|
|
60
|
+
declaration: value,
|
|
61
|
+
files,
|
|
62
|
+
label
|
|
63
|
+
}])),
|
|
64
|
+
writeHooks: sources.flatMap(({ config, files, label }) => (config.writeHooks ?? []).map((name) => ({
|
|
65
|
+
name,
|
|
66
|
+
files,
|
|
67
|
+
label
|
|
68
|
+
})))
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
//#endregion
|
|
72
|
+
export { checkUnique, mergeConfig };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { conventionConfig } from "../project/convention.js";
|
|
2
|
+
import { uiDirs } from "../project/convention-files.js";
|
|
3
|
+
import { functionFiles } from "../project/functions-folder.js";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
//#region src/config/project-files.ts
|
|
9
|
+
/** The files the conventions name besides resources and functions, which a bundle carries for its serve runtime. */
|
|
10
|
+
const conventionModules = (projectDir) => {
|
|
11
|
+
const inFolder = (folder) => existsSync(path.join(projectDir, folder)) ? readdirSync(path.join(projectDir, folder)).filter((name) => name.endsWith(".ts") && !/\.(test|d)\.ts$/.test(name)).map((name) => `${folder}/${name}`) : [];
|
|
12
|
+
const files = [
|
|
13
|
+
"auth/hooks.ts",
|
|
14
|
+
"auth/authenticate.ts",
|
|
15
|
+
"auth/mailer.ts",
|
|
16
|
+
"config/roles.ts",
|
|
17
|
+
"assistant/tools.ts",
|
|
18
|
+
"audit/queue.ts",
|
|
19
|
+
"webhooks/owner-roles.ts",
|
|
20
|
+
"notifications/store.ts",
|
|
21
|
+
"notifications/history-store.ts",
|
|
22
|
+
"notifications/senders.ts"
|
|
23
|
+
];
|
|
24
|
+
return [
|
|
25
|
+
...inFolder("jobs"),
|
|
26
|
+
...inFolder("hooks"),
|
|
27
|
+
...inFolder("files"),
|
|
28
|
+
...files.filter((file) => existsSync(path.join(projectDir, file)))
|
|
29
|
+
].sort();
|
|
30
|
+
};
|
|
31
|
+
/** The code in the project's folder `projectDir`, loaded with `importModules`. */
|
|
32
|
+
const sourceFiles = (projectDir, importModules) => {
|
|
33
|
+
const configDir = path.join(projectDir, "config");
|
|
34
|
+
const index = path.join(configDir, "index.ts");
|
|
35
|
+
const one = (file) => async () => (await importModules([file]))[0];
|
|
36
|
+
return {
|
|
37
|
+
...existsSync(index) && { resources: async () => {
|
|
38
|
+
const dirs = uiDirs(configDir);
|
|
39
|
+
const [indexExports, ...ui] = await importModules([index, ...dirs.map((dir) => path.join(configDir, dir, "ui.ts"))]);
|
|
40
|
+
return conventionConfig(indexExports, dirs.map((dir, i) => [dir, ui[i]]));
|
|
41
|
+
} },
|
|
42
|
+
functions: Object.fromEntries(functionFiles(projectDir).map(({ name, file }) => [name, one(file)])),
|
|
43
|
+
module: (file) => existsSync(path.join(projectDir, file)) ? one(path.join(projectDir, file)) : void 0,
|
|
44
|
+
package: (name) => import(pathToFileURL(createRequire(path.join(projectDir, "package.json")).resolve(name)).href)
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
export { conventionModules, sourceFiles };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { checkUnique } from "./merge-config.js";
|
|
2
|
+
import { checkNotificationsOptions, declaredJob, fileProviderOf, isSubscription, webhooksModule } from "@protobase/server/internal";
|
|
3
|
+
import { isPageDefinition } from "@protobase/layout";
|
|
4
|
+
//#region src/config/project-parts.ts
|
|
5
|
+
/** The config modules' exports as one module: every value once, under a key no other config uses. */
|
|
6
|
+
const mergedModule = (modules) => {
|
|
7
|
+
const exports = modules.flatMap(({ module, label }) => Object.entries(module).map(([key, value]) => ({
|
|
8
|
+
key,
|
|
9
|
+
value,
|
|
10
|
+
label
|
|
11
|
+
})));
|
|
12
|
+
const values = exports.map((entry) => ({
|
|
13
|
+
...entry,
|
|
14
|
+
value: entry.value
|
|
15
|
+
}));
|
|
16
|
+
checkUnique("The resource", values.filter(({ value }) => typeof value?.recordSchema === "function" && typeof value.toModel === "function").map(({ value, label }) => ({
|
|
17
|
+
name: String(value.toModel().name),
|
|
18
|
+
value,
|
|
19
|
+
label
|
|
20
|
+
})));
|
|
21
|
+
checkUnique("The page", exports.filter(({ value }) => isPageDefinition(value)).map(({ value, label }) => ({
|
|
22
|
+
name: value.name,
|
|
23
|
+
value,
|
|
24
|
+
label
|
|
25
|
+
})));
|
|
26
|
+
checkUnique("The subscription", exports.filter(({ value }) => isSubscription(value)).map(({ value, label }) => ({
|
|
27
|
+
name: value.name,
|
|
28
|
+
value,
|
|
29
|
+
label
|
|
30
|
+
})));
|
|
31
|
+
const menus = values.filter(({ value }) => typeof value?.toUserMenuModel === "function");
|
|
32
|
+
if (new Set(menus.map(({ value }) => value)).size > 1) throw new Error(`Only one config may export a user menu; ${menus.map(({ label }) => label).join(" and ")} each do`);
|
|
33
|
+
const merged = {};
|
|
34
|
+
const own = modules.at(-1)?.label;
|
|
35
|
+
for (const { key, value, label } of exports) {
|
|
36
|
+
if (Object.values(merged).includes(value)) continue;
|
|
37
|
+
merged[label === own ? key : `${label}:${key}`] = value;
|
|
38
|
+
}
|
|
39
|
+
return merged;
|
|
40
|
+
};
|
|
41
|
+
/** The resources, views, pages and subscriptions of every config's config/ folder, after the webhook pages of `webhooks`. */
|
|
42
|
+
const projectResources = async (config) => {
|
|
43
|
+
const modules = await Promise.all(config.sources.flatMap(({ files, label }) => files.resources ? [files.resources().then((module) => ({
|
|
44
|
+
module,
|
|
45
|
+
label
|
|
46
|
+
}))] : []));
|
|
47
|
+
const webhooks = config.webhooks ? [{
|
|
48
|
+
module: webhooksModule(config.webhooks),
|
|
49
|
+
label: "protobase-webhooks"
|
|
50
|
+
}] : [];
|
|
51
|
+
return mergedModule([...webhooks, ...modules]);
|
|
52
|
+
};
|
|
53
|
+
/** The declared jobs, each run by the default export of its jobs/<name>.ts, loaded on its first run; schedules in the app's `timezone` unless a job names its own. */
|
|
54
|
+
const projectJobs = (config) => Object.entries(config.jobs).map(([name, { declaration, files, label }]) => {
|
|
55
|
+
const load = files.module(`jobs/${name}.ts`);
|
|
56
|
+
if (!load) throw new Error(`The job "${name}" of ${label} has no jobs/${name}.ts: write what it does there, as its default export`);
|
|
57
|
+
const timezone = declaration.timezone ?? config.timezone;
|
|
58
|
+
return declaredJob(name, {
|
|
59
|
+
...declaration,
|
|
60
|
+
...timezone && { timezone }
|
|
61
|
+
}, async () => (await load()).default);
|
|
62
|
+
});
|
|
63
|
+
/** The API functions of every config's functions/ folder by name, each its file's default export. */
|
|
64
|
+
const projectFunctions = async (config) => {
|
|
65
|
+
const all = config.sources.flatMap(({ files, label }) => Object.entries(files.functions).map(([name, load]) => ({
|
|
66
|
+
name,
|
|
67
|
+
value: load,
|
|
68
|
+
label
|
|
69
|
+
})));
|
|
70
|
+
if (all.length === 0) return void 0;
|
|
71
|
+
checkUnique("The function", all);
|
|
72
|
+
return Object.fromEntries(await Promise.all(all.map(async ({ name, value }) => [name, (await value()).default])));
|
|
73
|
+
};
|
|
74
|
+
/** The write hooks by name, each the default export of hooks/<name>.ts, loaded on its first write. */
|
|
75
|
+
const projectWriteHooks = (config) => config.writeHooks.map(({ name, files, label }) => {
|
|
76
|
+
const load = files.module(`hooks/${name}.ts`);
|
|
77
|
+
if (!load) throw new Error(`The write hook "${name}" of ${label} has no hooks/${name}.ts: write it there, as its default export`);
|
|
78
|
+
let hook;
|
|
79
|
+
return async (event, trx) => (await (hook ??= load().then((module) => module.default)))(event, trx);
|
|
80
|
+
});
|
|
81
|
+
/** The module at `file` of the last config's folder that has one, such as auth/mailer.ts; `undefined` when none has it. */
|
|
82
|
+
const lastModule = async (config, file) => {
|
|
83
|
+
const load = config.sources.map(({ files }) => files.module(file)).findLast(Boolean);
|
|
84
|
+
return load && await load();
|
|
85
|
+
};
|
|
86
|
+
/** The default export of the last config folder's `file`; `undefined` when none has it. */
|
|
87
|
+
const lastDefault = async (config, file) => (await lastModule(config, file))?.default;
|
|
88
|
+
/** The roles: the `roles` of the last config/roles.ts among the configs' folders. */
|
|
89
|
+
const projectRoles = async (config) => {
|
|
90
|
+
const module = await lastModule(config, "config/roles.ts");
|
|
91
|
+
return module && (module.roles ?? module.default);
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* The assistant's own tools: the default export of every config folder's assistant/tools.ts, the extensions' first, each
|
|
95
|
+
* name once; none while the assistant is off.
|
|
96
|
+
*/
|
|
97
|
+
const projectAssistantTools = async (config) => {
|
|
98
|
+
if (config.assistant === false) return [];
|
|
99
|
+
const found = config.sources.flatMap(({ files, label }) => {
|
|
100
|
+
const load = files.module("assistant/tools.ts");
|
|
101
|
+
return load ? [load().then((module) => module.default.map((tool) => ({
|
|
102
|
+
tool,
|
|
103
|
+
label
|
|
104
|
+
})))] : [];
|
|
105
|
+
});
|
|
106
|
+
const tools = (await Promise.all(found)).flat();
|
|
107
|
+
checkUnique("The assistant tool", tools.map(({ tool, label }) => ({
|
|
108
|
+
name: tool.name,
|
|
109
|
+
value: tool,
|
|
110
|
+
label
|
|
111
|
+
})));
|
|
112
|
+
return tools.map(({ tool }) => tool);
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The API's options, from the config's top-level keys, with what the conventions find: the roles, write hooks and
|
|
116
|
+
* assistant tools, the audit queue of audit/queue.ts and the error hook of hooks/error.ts.
|
|
117
|
+
*/
|
|
118
|
+
const projectOptions = async (config) => {
|
|
119
|
+
const { apiPath, mcpEndpoint, runtimeUpdates, remoteResources, locale, apiKeys, statementTimeoutMs, scanGuard, defaultAccess, rowPermissions, errorReporting, assistant, cors, maps, plugins } = config;
|
|
120
|
+
const settings = {
|
|
121
|
+
apiKeys,
|
|
122
|
+
statementTimeoutMs,
|
|
123
|
+
scanGuard,
|
|
124
|
+
defaultAccess,
|
|
125
|
+
rowPermissions,
|
|
126
|
+
errorReporting,
|
|
127
|
+
assistant,
|
|
128
|
+
cors,
|
|
129
|
+
maps,
|
|
130
|
+
plugins,
|
|
131
|
+
locale,
|
|
132
|
+
basePath: apiPath,
|
|
133
|
+
mcp: mcpEndpoint,
|
|
134
|
+
runtime: runtimeUpdates,
|
|
135
|
+
sources: remoteResources
|
|
136
|
+
};
|
|
137
|
+
const [roles, tools, audit, onUnhandledError] = await Promise.all([
|
|
138
|
+
projectRoles(config),
|
|
139
|
+
projectAssistantTools(config),
|
|
140
|
+
lastDefault(config, "audit/queue.ts"),
|
|
141
|
+
lastDefault(config, "hooks/error.ts")
|
|
142
|
+
]);
|
|
143
|
+
const writeHooks = projectWriteHooks(config);
|
|
144
|
+
const options = Object.fromEntries(Object.entries(settings).filter(([, value]) => value !== void 0));
|
|
145
|
+
return {
|
|
146
|
+
...options,
|
|
147
|
+
...roles && { roles },
|
|
148
|
+
...writeHooks.length > 0 && { writeHooks },
|
|
149
|
+
...tools.length > 0 && { assistant: {
|
|
150
|
+
...options.assistant || {},
|
|
151
|
+
tools
|
|
152
|
+
} },
|
|
153
|
+
...audit && { audit },
|
|
154
|
+
...onUnhandledError && { onUnhandledError }
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
const customProvider = async (config, name) => {
|
|
158
|
+
const provider = await lastDefault(config, `files/${name}.ts`);
|
|
159
|
+
if (!provider) throw new Error(`The file provider "${name}" is custom but no config's folder has files/${name}.ts: export the provider there, as its default export`);
|
|
160
|
+
return provider;
|
|
161
|
+
};
|
|
162
|
+
/** Where file fields keep their bytes: the providers made from their settings or found in files/<name>.ts, and the image library by its name. */
|
|
163
|
+
const projectFiles = async (config) => {
|
|
164
|
+
if (!config.files) return void 0;
|
|
165
|
+
const { providers, images, ...files } = config.files;
|
|
166
|
+
const made = providers && Object.fromEntries(await Promise.all(Object.entries(providers).map(async ([name, settings]) => [name, settings.kind === "custom" ? await customProvider(config, name) : fileProviderOf(name, settings)])));
|
|
167
|
+
return {
|
|
168
|
+
...files,
|
|
169
|
+
...made && { providers: made },
|
|
170
|
+
...images && { sharp: (await config.sources.at(-1).files.package(images)).default }
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
/** The webhooks options, with the owner roles of webhooks/owner-roles.ts when a config's folder has it. */
|
|
174
|
+
const projectWebhooks = async (config) => {
|
|
175
|
+
if (!config.webhooks) return void 0;
|
|
176
|
+
const ownerRoles = await lastDefault(config, "webhooks/owner-roles.ts");
|
|
177
|
+
return {
|
|
178
|
+
...config.webhooks,
|
|
179
|
+
...ownerRoles && { ownerRoles }
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* The notifications options, checked, with what the conventions find in notifications/: the device store of store.ts,
|
|
184
|
+
* the history store of history-store.ts and the push senders of senders.ts, each its default export.
|
|
185
|
+
*/
|
|
186
|
+
const projectNotifications = async (config) => {
|
|
187
|
+
if (!config.notifications) return void 0;
|
|
188
|
+
const [store, historyStore, senders] = await Promise.all([
|
|
189
|
+
lastDefault(config, "notifications/store.ts"),
|
|
190
|
+
lastDefault(config, "notifications/history-store.ts"),
|
|
191
|
+
lastDefault(config, "notifications/senders.ts")
|
|
192
|
+
]);
|
|
193
|
+
return checkNotificationsOptions({
|
|
194
|
+
...config.notifications,
|
|
195
|
+
...store && { store },
|
|
196
|
+
...historyStore && { historyStore },
|
|
197
|
+
...senders && { senders }
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
//#endregion
|
|
201
|
+
export { lastModule, mergedModule, projectAssistantTools, projectFiles, projectFunctions, projectJobs, projectNotifications, projectOptions, projectResources, projectRoles, projectWebhooks, projectWriteHooks };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { calledConfig } from "./called-config.js";
|
|
2
|
+
import { lazyImports } from "./lazy-imports.js";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
//#region src/config/read-config.ts
|
|
7
|
+
const configFiles = ["protobase.config.ts", "protobase.config.json"];
|
|
8
|
+
/** The project's one config file: protobase.config.ts or protobase.config.json, never both; `undefined` without one. */
|
|
9
|
+
const configFileOf = (dir) => {
|
|
10
|
+
const found = configFiles.map((name) => path.join(dir, name)).filter((file) => existsSync(file));
|
|
11
|
+
if (found.length > 1) throw new Error(`${dir} has both protobase.config.ts and protobase.config.json; a project has one config file, so remove one of them`);
|
|
12
|
+
return found[0];
|
|
13
|
+
};
|
|
14
|
+
const parser = async () => import("rolldown/utils");
|
|
15
|
+
const typescriptInput = async (file, importModules) => {
|
|
16
|
+
const { parseSync } = await parser();
|
|
17
|
+
const parsed = parseSync(file, await readFile(file, "utf8"));
|
|
18
|
+
if (parsed.errors.length > 0) throw new Error(`${file}: ${parsed.errors[0].message}`);
|
|
19
|
+
const lazy = lazyImports(parsed.program);
|
|
20
|
+
if (lazy.length > 0) throw new Error(`${path.basename(file)} imports ${lazy.map((name) => `"${name}"`).join(", ")} lazily; a config is one object, so import a value it holds statically, and leave code to the files the framework finds (jobs/<name>.ts, hooks/<name>.ts, auth/hooks.ts)`);
|
|
21
|
+
const [module] = await importModules([file]);
|
|
22
|
+
if (module.default === void 0) throw new Error(`${file} has no default export: export default defineConfig({ ... })`);
|
|
23
|
+
return module.default;
|
|
24
|
+
};
|
|
25
|
+
/** The config of a config file: a JSON object, or a TypeScript module's default export, called with `configEnv`. */
|
|
26
|
+
const readConfigFile = async (file, configEnv, importModules) => {
|
|
27
|
+
const input = file.endsWith(".json") ? JSON.parse(await readFile(file, "utf8")) : await typescriptInput(file, importModules);
|
|
28
|
+
return calledConfig(input, configEnv, path.basename(file));
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { configFileOf, configFiles, readConfigFile };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { mergeConfig } from "./merge-config.js";
|
|
2
|
+
import { importProjectFiles } from "../project/import-project-file.js";
|
|
3
|
+
import { withDefaults } from "./config-defaults.js";
|
|
4
|
+
import { sourceFiles } from "./project-files.js";
|
|
5
|
+
import { configFileOf, readConfigFile } from "./read-config.js";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
//#region src/config/source-config.ts
|
|
8
|
+
/**
|
|
9
|
+
* The config of the project in `dir`: protobase.config.ts or protobase.config.json (or none), merged after the
|
|
10
|
+
* extensions' configs, with the code each folder's conventions find. Only the config files run here; that code loads
|
|
11
|
+
* when a command asks for it.
|
|
12
|
+
*/
|
|
13
|
+
const loadSourceConfig = async (dir, { command, extensions = [], importModules = importProjectFiles }) => {
|
|
14
|
+
const configEnv = {
|
|
15
|
+
command,
|
|
16
|
+
env: process.env,
|
|
17
|
+
root: dir
|
|
18
|
+
};
|
|
19
|
+
const extended = [];
|
|
20
|
+
for (const extension of extensions) {
|
|
21
|
+
if (!extension.config) continue;
|
|
22
|
+
extended.push({
|
|
23
|
+
config: await readConfigFile(extension.config, configEnv, importModules),
|
|
24
|
+
files: sourceFiles(extension.dir, importModules),
|
|
25
|
+
label: path.basename(extension.dir)
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const file = configFileOf(dir);
|
|
29
|
+
const own = file ? await readConfigFile(file, configEnv, importModules) : {};
|
|
30
|
+
return withDefaults(mergeConfig(...extended, {
|
|
31
|
+
config: own,
|
|
32
|
+
files: sourceFiles(dir, importModules),
|
|
33
|
+
label: file ? path.basename(file) : "config/index.ts"
|
|
34
|
+
}), command, process.env);
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { loadSourceConfig };
|
package/dist/dev/api-child.js
CHANGED