zitejs 0.10.0 → 0.10.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.
Files changed (32) hide show
  1. package/dist/cjs/check/index.js +10 -4
  2. package/dist/cjs/start/build/entries.d.ts +5 -0
  3. package/dist/cjs/start/build/entries.js +16 -8
  4. package/dist/cjs/start/build/roleIds.d.ts +11 -0
  5. package/dist/cjs/start/build/roleIds.js +59 -0
  6. package/dist/cjs/start/describe/schema.d.ts +574 -0
  7. package/dist/cjs/start/describe/schema.js +178 -0
  8. package/dist/cjs/start/protocol.d.ts +7 -0
  9. package/dist/cjs/start/protocol.js +27 -3
  10. package/dist/cjs/start/serve/authHookNames.d.ts +3 -0
  11. package/dist/cjs/start/serve/authHookNames.js +13 -0
  12. package/dist/esm/check/index.js +10 -4
  13. package/dist/esm/start/build/entries.d.ts +5 -0
  14. package/dist/esm/start/build/entries.js +15 -8
  15. package/dist/esm/start/build/roleIds.d.ts +11 -0
  16. package/dist/esm/start/build/roleIds.js +54 -0
  17. package/dist/esm/start/describe/schema.d.ts +574 -0
  18. package/dist/esm/start/describe/schema.js +175 -0
  19. package/dist/esm/start/describe/schema.test-d.d.ts +1 -0
  20. package/dist/esm/start/describe/schema.test-d.js +23 -0
  21. package/dist/esm/start/index.d.ts +1 -1
  22. package/dist/esm/start/protocol.d.ts +7 -0
  23. package/dist/esm/start/protocol.js +17 -2
  24. package/dist/esm/start/roles.d.ts +10 -2
  25. package/dist/esm/start/serve/authHookNames.d.ts +3 -0
  26. package/dist/esm/start/serve/authHookNames.js +9 -0
  27. package/dist/esm/start/serve/authHooks.d.ts +2 -4
  28. package/dist/esm/start/serve/authHooks.js +2 -8
  29. package/dist/esm/start/version.d.ts +1 -1
  30. package/dist/esm/start/version.js +1 -1
  31. package/package.json +4 -2
  32. package/tsconfig/start.json +40 -0
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.describeOutputSchema = exports.authHookNameSchema = exports.describedWorkflowSchema = exports.describedTriggerSchema = exports.describedScheduleSchema = exports.describedPageSchema = exports.describedRouteSchema = exports.describedFunctionSchema = exports.describedAccessSchema = void 0;
4
+ // Describe's answer as zod, for the platform to check a build's description with before it reads one: the shape of
5
+ // `Description`, nothing of the platform's limits. Imports only zod and the auth hook names, so
6
+ // zitejs/start/protocol stays light; schema.test-d.ts holds it to the type.
7
+ const zod_1 = require("zod");
8
+ const authHookNames_js_1 = require("../serve/authHookNames.js");
9
+ exports.describedAccessSchema = zod_1.z.object({
10
+ kind: zod_1.z.enum(['signedIn', 'anyone', 'webhook', 'none']),
11
+ roles: zod_1.z.array(zod_1.z.string()),
12
+ paused: zod_1.z.literal(true).optional(),
13
+ });
14
+ exports.describedFunctionSchema = zod_1.z.object({
15
+ id: zod_1.z.string(),
16
+ name: zod_1.z.string().optional(),
17
+ file: zod_1.z.string().optional(),
18
+ method: zod_1.z.string().optional(),
19
+ description: zod_1.z.string().optional(),
20
+ access: exports.describedAccessSchema,
21
+ /** The validator's JSON Schema, `{ unrepresentable: true }` or `{ opaque: true }`. */
22
+ input: zod_1.z.unknown().optional(),
23
+ problems: zod_1.z.array(zod_1.z.string()),
24
+ });
25
+ exports.describedRouteSchema = zod_1.z.object({
26
+ path: zod_1.z.string(),
27
+ file: zod_1.z.string().optional(),
28
+ description: zod_1.z.string().optional(),
29
+ methods: zod_1.z.record(zod_1.z.string(), exports.describedAccessSchema),
30
+ problems: zod_1.z.array(zod_1.z.string()),
31
+ });
32
+ exports.describedPageSchema = zod_1.z.object({
33
+ id: zod_1.z.string(),
34
+ path: zod_1.z.string(),
35
+ file: zod_1.z.string().optional(),
36
+ });
37
+ const dayOfWeek = zod_1.z.enum(['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']);
38
+ const activeWindow = zod_1.z.object({
39
+ hoursRange: zod_1.z.object({ from: zod_1.z.string(), to: zod_1.z.string() }).optional(),
40
+ daysOfWeek: zod_1.z.array(dayOfWeek).optional(),
41
+ });
42
+ /** `ZiteSchedule`, as an automation declares it. */
43
+ exports.describedScheduleSchema = zod_1.z.discriminatedUnion('scheduleType', [
44
+ zod_1.z.object({
45
+ scheduleType: zod_1.z.literal('recurring'),
46
+ schedule: zod_1.z.discriminatedUnion('frequency', [
47
+ zod_1.z.object({
48
+ frequency: zod_1.z.literal('minutely'),
49
+ interval: zod_1.z.number(),
50
+ activeWindow: activeWindow.optional(),
51
+ }),
52
+ zod_1.z.object({
53
+ frequency: zod_1.z.literal('hourly'),
54
+ interval: zod_1.z.number(),
55
+ minute: zod_1.z.number().optional(),
56
+ activeWindow: activeWindow.optional(),
57
+ }),
58
+ zod_1.z.object({
59
+ frequency: zod_1.z.literal('daily'),
60
+ interval: zod_1.z.number(),
61
+ times: zod_1.z.array(zod_1.z.string()),
62
+ }),
63
+ zod_1.z.object({
64
+ frequency: zod_1.z.literal('weekly'),
65
+ interval: zod_1.z.number(),
66
+ daysOfWeek: zod_1.z.array(dayOfWeek),
67
+ times: zod_1.z.array(zod_1.z.string()),
68
+ }),
69
+ zod_1.z.object({
70
+ frequency: zod_1.z.literal('monthly'),
71
+ interval: zod_1.z.literal(1),
72
+ monthlyDay: zod_1.z.discriminatedUnion('type', [
73
+ zod_1.z.object({
74
+ type: zod_1.z.literal('dayOfMonth'),
75
+ day: zod_1.z.union([zod_1.z.number(), zod_1.z.literal('last')]),
76
+ }),
77
+ zod_1.z.object({
78
+ type: zod_1.z.literal('weekdayOccurrence'),
79
+ weekday: dayOfWeek,
80
+ occurrence: zod_1.z.union([zod_1.z.literal([1, 2, 3, 4]), zod_1.z.literal('last')]),
81
+ }),
82
+ ]),
83
+ times: zod_1.z.array(zod_1.z.string()),
84
+ }),
85
+ ]),
86
+ endPolicy: zod_1.z
87
+ .discriminatedUnion('type', [
88
+ zod_1.z.object({ type: zod_1.z.literal('never') }),
89
+ zod_1.z.object({ type: zod_1.z.literal('onDate'), endAt: zod_1.z.string() }),
90
+ zod_1.z.object({
91
+ type: zod_1.z.literal('afterOccurrences'),
92
+ occurrences: zod_1.z.number(),
93
+ }),
94
+ ])
95
+ .optional(),
96
+ overlapPolicy: zod_1.z.enum(['skip', 'allow']).optional(),
97
+ timezone: zod_1.z.string(),
98
+ paused: zod_1.z.boolean().optional(),
99
+ }),
100
+ zod_1.z.object({
101
+ scheduleType: zod_1.z.literal('oneTime'),
102
+ fireAt: zod_1.z.string(),
103
+ timezone: zod_1.z.string(),
104
+ paused: zod_1.z.boolean().optional(),
105
+ }),
106
+ ]);
107
+ const absent = zod_1.z.never().optional();
108
+ const shown = { label: zod_1.z.string().optional(), paused: zod_1.z.boolean().optional() };
109
+ /**
110
+ * `ZiteTrigger`, as an automation declares it, without a provider trigger's `subscribe` and `unsubscribe`. Database
111
+ * and form triggers are in it: describe reports one it refuses.
112
+ */
113
+ exports.describedTriggerSchema = zod_1.z.union([
114
+ zod_1.z.object({
115
+ integration: zod_1.z.string(),
116
+ ...shown,
117
+ every: absent,
118
+ events: absent,
119
+ table: absent,
120
+ form: absent,
121
+ }),
122
+ zod_1.z.object({
123
+ integration: zod_1.z.string(),
124
+ events: zod_1.z.array(zod_1.z.string()),
125
+ ...shown,
126
+ every: absent,
127
+ table: absent,
128
+ form: absent,
129
+ }),
130
+ zod_1.z.object({
131
+ every: zod_1.z.string(),
132
+ integration: zod_1.z.string().optional(),
133
+ ...shown,
134
+ events: absent,
135
+ table: absent,
136
+ form: absent,
137
+ }),
138
+ zod_1.z.object({
139
+ table: zod_1.z.string(),
140
+ on: zod_1.z.array(zod_1.z.enum(['created', 'updated', 'deleted'])),
141
+ fields: zod_1.z.array(zod_1.z.string()).optional(),
142
+ ...shown,
143
+ integration: absent,
144
+ every: absent,
145
+ events: absent,
146
+ form: absent,
147
+ }),
148
+ zod_1.z.object({
149
+ form: zod_1.z.string(),
150
+ on: zod_1.z.array(zod_1.z.literal('submitted')),
151
+ ...shown,
152
+ integration: absent,
153
+ every: absent,
154
+ events: absent,
155
+ table: absent,
156
+ }),
157
+ ]);
158
+ exports.describedWorkflowSchema = zod_1.z.object({
159
+ id: zod_1.z.string(),
160
+ file: zod_1.z.string(),
161
+ description: zod_1.z.string().optional(),
162
+ schedule: exports.describedScheduleSchema.optional(),
163
+ trigger: exports.describedTriggerSchema.optional(),
164
+ registration: zod_1.z
165
+ .object({ unsubscribe: zod_1.z.boolean(), hash: zod_1.z.string(), module: zod_1.z.string() })
166
+ .optional(),
167
+ problems: zod_1.z.array(zod_1.z.string()),
168
+ });
169
+ exports.authHookNameSchema = zod_1.z.enum(authHookNames_js_1.AUTH_HOOK_NAMES);
170
+ /** What `zitejs describe` and a build's describe invoke answer. */
171
+ exports.describeOutputSchema = zod_1.z.object({
172
+ functions: zod_1.z.array(exports.describedFunctionSchema),
173
+ routes: zod_1.z.array(exports.describedRouteSchema),
174
+ pages: zod_1.z.array(exports.describedPageSchema),
175
+ workflows: zod_1.z.array(exports.describedWorkflowSchema),
176
+ authHooks: zod_1.z.array(exports.authHookNameSchema),
177
+ problems: zod_1.z.array(zod_1.z.string()),
178
+ });
@@ -1,4 +1,5 @@
1
1
  export { isReservedIntegrationId, RESERVED_INTEGRATION_IDS, } from '../internal/reservedIds.js';
2
+ export { authHookNameSchema, describedAccessSchema, describedFunctionSchema, describedPageSchema, describedRouteSchema, describedScheduleSchema, describedTriggerSchema, describedWorkflowSchema, describeOutputSchema, } from './describe/schema.js';
2
3
  export type { AppContext, ExpiringToken, HostConnection, HostMode, HostPage, HostProps, PlatformBinding, SiteHead, WebManifest, } from './types.js';
3
4
  /** What the host dispatches a request to do beside serving it; without props, only `warm` and `describe`. */
4
5
  export declare const INVOKE_KINDS: readonly ["webhook", "describe", "workflow", "register", "warm", "authHook", "function"];
@@ -17,3 +18,9 @@ export declare const GATEWAY_CONNECTION_HEADER = "x-zite-gateway-connection";
17
18
  /** On a demo's refused answer, beside its 403, so the platform's runtime shows the demo notice. */
18
19
  export declare const ERROR_CODE_HEADER = "x-zite-error-code";
19
20
  export declare const DEMO_READ_ONLY_CODE = "DEMO_READ_ONLY";
21
+ /**
22
+ * What a Start app installs beside zitejs, exactly: the TanStack peers zitejs pins, and React Query with its core,
23
+ * pinned together so only one query-core is ever installed. tests/startAppDependencies.test.ts holds it to
24
+ * package.json.
25
+ */
26
+ export declare const START_APP_DEPENDENCIES: Readonly<Record<string, string>>;
@@ -1,11 +1,22 @@
1
1
  "use strict";
2
- // What a Start build and the platform's host say to each other on the wire. Import-free but for the reserved ids,
3
- // themselves import-free, so the host takes these without loading anything of zitejs.
2
+ // What a Start build and the platform's host say to each other on the wire. Import-free but for the reserved ids and
3
+ // describe's output schema, which imports only zod and the auth hook names, so the host takes these without loading
4
+ // anything of zitejs.
4
5
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.DEMO_READ_ONLY_CODE = exports.ERROR_CODE_HEADER = exports.GATEWAY_CONNECTION_HEADER = exports.PREVIEW_CLAIM_HEADER = exports.RUNTIME_VERSION_HEADER = exports.INVOKE_PATH = exports.INVOKE_HEADER = exports.isInvokeKind = exports.INVOKE_KINDS = exports.RESERVED_INTEGRATION_IDS = exports.isReservedIntegrationId = void 0;
6
+ exports.START_APP_DEPENDENCIES = exports.DEMO_READ_ONLY_CODE = exports.ERROR_CODE_HEADER = exports.GATEWAY_CONNECTION_HEADER = exports.PREVIEW_CLAIM_HEADER = exports.RUNTIME_VERSION_HEADER = exports.INVOKE_PATH = exports.INVOKE_HEADER = exports.isInvokeKind = exports.INVOKE_KINDS = exports.describeOutputSchema = exports.describedWorkflowSchema = exports.describedTriggerSchema = exports.describedScheduleSchema = exports.describedRouteSchema = exports.describedPageSchema = exports.describedFunctionSchema = exports.describedAccessSchema = exports.authHookNameSchema = exports.RESERVED_INTEGRATION_IDS = exports.isReservedIntegrationId = void 0;
6
7
  var reservedIds_js_1 = require("../internal/reservedIds.js");
7
8
  Object.defineProperty(exports, "isReservedIntegrationId", { enumerable: true, get: function () { return reservedIds_js_1.isReservedIntegrationId; } });
8
9
  Object.defineProperty(exports, "RESERVED_INTEGRATION_IDS", { enumerable: true, get: function () { return reservedIds_js_1.RESERVED_INTEGRATION_IDS; } });
10
+ var schema_js_1 = require("./describe/schema.js");
11
+ Object.defineProperty(exports, "authHookNameSchema", { enumerable: true, get: function () { return schema_js_1.authHookNameSchema; } });
12
+ Object.defineProperty(exports, "describedAccessSchema", { enumerable: true, get: function () { return schema_js_1.describedAccessSchema; } });
13
+ Object.defineProperty(exports, "describedFunctionSchema", { enumerable: true, get: function () { return schema_js_1.describedFunctionSchema; } });
14
+ Object.defineProperty(exports, "describedPageSchema", { enumerable: true, get: function () { return schema_js_1.describedPageSchema; } });
15
+ Object.defineProperty(exports, "describedRouteSchema", { enumerable: true, get: function () { return schema_js_1.describedRouteSchema; } });
16
+ Object.defineProperty(exports, "describedScheduleSchema", { enumerable: true, get: function () { return schema_js_1.describedScheduleSchema; } });
17
+ Object.defineProperty(exports, "describedTriggerSchema", { enumerable: true, get: function () { return schema_js_1.describedTriggerSchema; } });
18
+ Object.defineProperty(exports, "describedWorkflowSchema", { enumerable: true, get: function () { return schema_js_1.describedWorkflowSchema; } });
19
+ Object.defineProperty(exports, "describeOutputSchema", { enumerable: true, get: function () { return schema_js_1.describeOutputSchema; } });
9
20
  /** What the host dispatches a request to do beside serving it; without props, only `warm` and `describe`. */
10
21
  exports.INVOKE_KINDS = [
11
22
  'webhook',
@@ -31,3 +42,16 @@ exports.GATEWAY_CONNECTION_HEADER = 'x-zite-gateway-connection';
31
42
  /** On a demo's refused answer, beside its 403, so the platform's runtime shows the demo notice. */
32
43
  exports.ERROR_CODE_HEADER = 'x-zite-error-code';
33
44
  exports.DEMO_READ_ONLY_CODE = 'DEMO_READ_ONLY';
45
+ /**
46
+ * What a Start app installs beside zitejs, exactly: the TanStack peers zitejs pins, and React Query with its core,
47
+ * pinned together so only one query-core is ever installed. tests/startAppDependencies.test.ts holds it to
48
+ * package.json.
49
+ */
50
+ exports.START_APP_DEPENDENCIES = {
51
+ '@tanstack/query-core': '5.104.0',
52
+ '@tanstack/react-query': '5.104.0',
53
+ '@tanstack/react-router': '1.170.45',
54
+ '@tanstack/react-router-ssr-query': '1.167.3',
55
+ '@tanstack/react-start': '1.169.3',
56
+ '@tanstack/start-storage-context': '1.167.40',
57
+ };
@@ -0,0 +1,3 @@
1
+ export declare const AUTH_HOOK_NAMES: readonly ["beforeSignUp", "beforeSignIn", "afterSignUp", "afterSignIn", "verifySession"];
2
+ export type AuthHookName = (typeof AUTH_HOOK_NAMES)[number];
3
+ export declare const isAuthHookName: (name: string) => name is AuthHookName;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ // Import-free, so describe's output schema in zitejs/start/protocol can name them.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isAuthHookName = exports.AUTH_HOOK_NAMES = void 0;
5
+ exports.AUTH_HOOK_NAMES = [
6
+ 'beforeSignUp',
7
+ 'beforeSignIn',
8
+ 'afterSignUp',
9
+ 'afterSignIn',
10
+ 'verifySession',
11
+ ];
12
+ const isAuthHookName = (name) => exports.AUTH_HOOK_NAMES.includes(name);
13
+ exports.isAuthHookName = isAuthHookName;
@@ -130,14 +130,20 @@ export function runTool(file, args, cwd) {
130
130
  }
131
131
  const isMissingModule = (err) => err?.code === 'MODULE_NOT_FOUND';
132
132
  /**
133
- * A Start app, in about a second: its route tree regenerated as `vite build`
134
- * writes it, then a tsgo typecheck, which reads that tree. Everything else is
135
- * the platform build's and its describe's to refuse.
133
+ * A Start app, in about a second: its `.zite/` entries and role ids, and its
134
+ * route tree, written as `vite build` writes them, then a tsgo typecheck, which
135
+ * reads them. Everything else is the platform build's and its describe's to
136
+ * refuse.
136
137
  */
137
138
  async function checkStartApp(reporter, app, appPath) {
138
- const { regenerateRouteTree } = await import('./start/routeTree.js');
139
+ const [{ regenerateRouteTree }, { writeStartFiles }] = await Promise.all([
140
+ import('./start/routeTree.js'),
141
+ import('../start/build/entries.js'),
142
+ ]);
139
143
  let routeTree = { ok: true };
140
144
  try {
145
+ // First: the route tree's footer types the start instance only when .zite/start.ts is there.
146
+ writeStartFiles(appPath);
141
147
  await regenerateRouteTree(appPath);
142
148
  }
143
149
  catch (err) {
@@ -15,6 +15,11 @@ export declare const hasAppStart: (appDir: string) => boolean;
15
15
  export declare const writeIfChanged: (appDir: string, file: string, content: string) => boolean;
16
16
  export declare const isStartAppDir: (appPath: string) => boolean;
17
17
  export declare const workspaceHasStartApps: () => boolean;
18
+ /**
19
+ * The `.zite/` files a Start app's typecheck reads, its entries as `vite build` writes them: `check`, `generate` and
20
+ * `dev` all write them. Returns the ones that changed.
21
+ */
22
+ export declare function writeStartFiles(appDir: string): string[];
18
23
  export declare function regenerateStartEntries(app: SourceDir): void;
19
24
  /**
20
25
  * App modules that mention createAutomation or createProviderTrigger: what the workflow registry imports. Describe
@@ -1,6 +1,7 @@
1
1
  import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, } from 'node:fs';
2
2
  import { dirname, join, relative } from 'node:path';
3
3
  import { listSourceDirs } from '../../sourceRoots.js';
4
+ import { renderRoleIds, START_ROLES_FILE, workspaceRoleIds, } from './roleIds.js';
4
5
  export const START_SERVER_ENTRY = '.zite/server.ts';
5
6
  export const START_INSTANCE_ENTRY = '.zite/start.ts';
6
7
  const APP_START_FILES = ['src/start.ts', 'src/start.tsx'];
@@ -64,17 +65,23 @@ export const isStartAppDir = (appPath) => {
64
65
  };
65
66
  const isStartApp = (app) => isStartAppDir(app.path);
66
67
  export const workspaceHasStartApps = () => listSourceDirs().some(isStartApp);
68
+ /**
69
+ * The `.zite/` files a Start app's typecheck reads, its entries as `vite build` writes them: `check`, `generate` and
70
+ * `dev` all write them. Returns the ones that changed.
71
+ */
72
+ export function writeStartFiles(appDir) {
73
+ const files = [
74
+ [START_SERVER_ENTRY, renderStartServerEntry()],
75
+ [START_INSTANCE_ENTRY, renderStartInstance(hasAppStart(appDir))],
76
+ [START_ROLES_FILE, renderRoleIds(workspaceRoleIds(appDir))],
77
+ ];
78
+ return files.flatMap(([file, content]) => writeIfChanged(appDir, file, content) ? [file] : []);
79
+ }
67
80
  export function regenerateStartEntries(app) {
68
81
  if (!isStartApp(app))
69
82
  return;
70
- const entries = [
71
- [START_SERVER_ENTRY, renderStartServerEntry()],
72
- [START_INSTANCE_ENTRY, renderStartInstance(hasAppStart(app.path))],
73
- ];
74
- for (const [file, content] of entries) {
75
- if (writeIfChanged(app.path, file, content))
76
- console.log(`Wrote ${join(app.path, file)}`);
77
- }
83
+ for (const file of writeStartFiles(app.path))
84
+ console.log(`Wrote ${join(app.path, file)}`);
78
85
  }
79
86
  const SOURCE_FILE = /\.[mc]?[jt]sx?$/;
80
87
  const NOT_APP_CODE = /(\.(test|spec)\.[^.]+|\.d\.ts|routeTree\.gen\.ts)$/;
@@ -0,0 +1,11 @@
1
+ export declare const START_ROLES_FILE = ".zite/roles.d.ts";
2
+ /** The roles every workspace has without defining them, as the platform names them. */
3
+ export declare const BUILTIN_ROLE_IDS: readonly ["builtin:all-team-members", "builtin:all-users"];
4
+ /**
5
+ * The built-ins, then the roles the nearest `zite.permissions.json` defines. None at all when there's no readable
6
+ * file, so role ids stay plain strings: a missing or broken file would otherwise type every role the app names as a
7
+ * typo, and the nearest id that compiles is a built-in that admits everyone.
8
+ */
9
+ export declare const workspaceRoleIds: (appDir: string) => string[];
10
+ /** `.zite/roles.d.ts`, which types `role()` and `hasRole` to these ids. Its `export {}` makes it an augmentation. */
11
+ export declare const renderRoleIds: (ids: readonly string[]) => string;
@@ -0,0 +1,54 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { dirname, join, resolve } from 'node:path';
3
+ export const START_ROLES_FILE = '.zite/roles.d.ts';
4
+ /** The roles every workspace has without defining them, as the platform names them. */
5
+ export const BUILTIN_ROLE_IDS = [
6
+ 'builtin:all-team-members',
7
+ 'builtin:all-users',
8
+ ];
9
+ const PERMISSIONS_FILE = 'zite.permissions.json';
10
+ // The workspace root's in a monorepo, above apps/<dir>/.
11
+ const findPermissionsFile = (appDir) => {
12
+ for (let dir = resolve(appDir);; dir = dirname(dir)) {
13
+ const file = join(dir, PERMISSIONS_FILE);
14
+ if (existsSync(file))
15
+ return file;
16
+ if (dirname(dir) === dir)
17
+ return undefined;
18
+ }
19
+ };
20
+ // Undefined for a file that isn't a permissions file: its roles can't be known.
21
+ const definedRoleIds = (file) => {
22
+ try {
23
+ const { roles } = (JSON.parse(readFileSync(file, 'utf-8')) ?? {});
24
+ if (!Array.isArray(roles))
25
+ return undefined;
26
+ return roles.flatMap(role => {
27
+ const id = role?.id;
28
+ return typeof id === 'string' && id ? [id] : [];
29
+ });
30
+ }
31
+ catch {
32
+ return undefined;
33
+ }
34
+ };
35
+ /**
36
+ * The built-ins, then the roles the nearest `zite.permissions.json` defines. None at all when there's no readable
37
+ * file, so role ids stay plain strings: a missing or broken file would otherwise type every role the app names as a
38
+ * typo, and the nearest id that compiles is a built-in that admits everyone.
39
+ */
40
+ export const workspaceRoleIds = (appDir) => {
41
+ const file = findPermissionsFile(appDir);
42
+ const defined = file ? definedRoleIds(file) : undefined;
43
+ return defined ? [...new Set([...BUILTIN_ROLE_IDS, ...defined])] : [];
44
+ };
45
+ /** `.zite/roles.d.ts`, which types `role()` and `hasRole` to these ids. Its `export {}` makes it an augmentation. */
46
+ export const renderRoleIds = (ids) => `// Generated by zitejs from the built-in roles and zite.permissions.json. The platform owns this file.
47
+ export {};
48
+
49
+ declare module 'zitejs/start' {
50
+ interface ZiteRoles {
51
+ ${ids.map(id => ` ${JSON.stringify(id)}: true;`).join('\n')}
52
+ }
53
+ }
54
+ `;