@flowdular/sdk 0.2.0 → 0.2.2
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFile, readdir } from 'node:fs/promises';
|
|
2
|
-
import { join } from 'node:path';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
3
|
import { parse as parseYaml } from 'yaml';
|
|
4
4
|
import { defineCliExtension } from '@flowdular/sdk/cli-protocol';
|
|
5
5
|
import { AUTH_SCOPES, OWNER_SCOPES, PLATFORM_SCOPES } from '../acl/scopes.ts';
|
|
@@ -17,25 +17,31 @@ import {
|
|
|
17
17
|
|
|
18
18
|
/* Scopes are declared once, in the module specification. Reading them from
|
|
19
19
|
there keeps this command free of module code execution. */
|
|
20
|
-
async function declaredScopes(
|
|
20
|
+
export async function declaredScopes(
|
|
21
21
|
workspaceRoot: string,
|
|
22
22
|
moduleId: string,
|
|
23
|
+
moduleRoot = join(workspaceRoot, 'modules/auth'),
|
|
23
24
|
): Promise<{ readonly directory: string; readonly scopes: readonly string[] }> {
|
|
24
|
-
for (const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
for (const root of new Set([
|
|
26
|
+
join(workspaceRoot, 'modules'),
|
|
27
|
+
dirname(moduleRoot),
|
|
28
|
+
])) {
|
|
29
|
+
for (const entry of await readdir(root)) {
|
|
30
|
+
const specPath = join(root, entry, 'spec/module.yaml');
|
|
31
|
+
let spec: { id?: string; permissions?: { id?: string }[] };
|
|
32
|
+
try {
|
|
33
|
+
spec = parseYaml(await readFile(specPath, 'utf8')) as typeof spec;
|
|
34
|
+
} catch {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (spec.id !== moduleId) continue;
|
|
38
|
+
return {
|
|
39
|
+
directory: entry,
|
|
40
|
+
scopes: (spec.permissions ?? [])
|
|
41
|
+
.map((permission) => permission.id)
|
|
42
|
+
.filter((id): id is string => typeof id === 'string'),
|
|
43
|
+
};
|
|
31
44
|
}
|
|
32
|
-
if (spec.id !== moduleId) continue;
|
|
33
|
-
return {
|
|
34
|
-
directory: entry,
|
|
35
|
-
scopes: (spec.permissions ?? [])
|
|
36
|
-
.map((permission) => permission.id)
|
|
37
|
-
.filter((id): id is string => typeof id === 'string'),
|
|
38
|
-
};
|
|
39
45
|
}
|
|
40
46
|
throw new Error(`No enabled module declares the id "${moduleId}".`);
|
|
41
47
|
}
|
|
@@ -84,6 +90,7 @@ export const cliExtension = defineCliExtension({
|
|
|
84
90
|
const declared = await declaredScopes(
|
|
85
91
|
context.workspaceRoot,
|
|
86
92
|
moduleId.trim(),
|
|
93
|
+
context.moduleRoot,
|
|
87
94
|
);
|
|
88
95
|
const local = localDatabaseProvider(context.workspaceRoot);
|
|
89
96
|
const databases = local.create();
|
package/package.json
CHANGED
|
@@ -81,22 +81,40 @@ export function createPgliteCluster(
|
|
|
81
81
|
const close = (): Promise<void> => {
|
|
82
82
|
closePromise ??= (async () => {
|
|
83
83
|
if (!databasePromise) return;
|
|
84
|
-
|
|
84
|
+
// A failed initialization has no database to close; its caller already
|
|
85
|
+
// received the original error. Do not replace it during disposal.
|
|
86
|
+
const instance = await databasePromise.catch(() => undefined);
|
|
85
87
|
databasePromise = undefined;
|
|
86
|
-
await instance
|
|
88
|
+
await instance?.close();
|
|
87
89
|
})();
|
|
88
90
|
return closePromise;
|
|
89
91
|
};
|
|
90
92
|
|
|
91
93
|
const database = async (): Promise<PGlite> => {
|
|
92
94
|
databasePromise ??= (async () => {
|
|
95
|
+
if (options.dataDirectory) {
|
|
96
|
+
const { mkdir } = await import('node:fs/promises');
|
|
97
|
+
await mkdir(options.dataDirectory, { recursive: true, mode: 0o700 });
|
|
98
|
+
}
|
|
93
99
|
const created = options.dataDirectory
|
|
94
100
|
? await PGlite.create({
|
|
95
101
|
dataDir: options.dataDirectory,
|
|
96
102
|
parsers: SERVER_PARSERS,
|
|
97
103
|
})
|
|
98
104
|
: await PGlite.create({ parsers: SERVER_PARSERS });
|
|
99
|
-
|
|
105
|
+
try {
|
|
106
|
+
if (options.bootstrap) await created.exec(options.bootstrap);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
try {
|
|
109
|
+
await created.close();
|
|
110
|
+
} catch (closeError) {
|
|
111
|
+
throw new AggregateError(
|
|
112
|
+
[error, closeError],
|
|
113
|
+
'PGlite bootstrap failed and cleanup also failed.',
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
100
118
|
return created;
|
|
101
119
|
})();
|
|
102
120
|
return databasePromise;
|