Repository navigation
Expand file tree
/
Copy pathextensionModule.js
More file actions
99 lines (90 loc) · 3.26 KB
/
Copy pathextensionModule.js
File metadata and controls
99 lines (90 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** @typedef {import('harperdb').Scope} Scope */
import { createRegenerationScheduler } from './utils/createRegenerationScheduler.js';
import { setLogger } from './utils/logger.js';
import { hasClientOutputs, regenerateAll } from './utils/regenerateAll.js';
export const suppressHandleApplicationWarning = true;
const INITIAL_DELAY_MS = 5000;
/**
* @param {Scope} scope
*/
export async function handleApplication(scope) {
setLogger(scope.logger);
if (!process.env.DEV_MODE) {
scope.logger.trace?.('@harperfast/schema-codegen skipping execution outside of dev mode');
return;
}
/**
* @param {string} key
* @returns {any}
*/
const option = (key) => scope.options.get([key]);
const watchConfig = option('watch');
const shouldWatch = watchConfig === true || watchConfig === undefined;
const globalTypes = /** @type {string} */ (option('globalTypes'));
const schemaTypes = /** @type {string} */ (option('schemaTypes'));
const jsdoc = /** @type {string | undefined} */ (option('jsdoc'));
const client = {
schemaIR: /** @type {string | undefined} */ (option('schemaIR')),
syncProfiles: /** @type {string | undefined} */ (option('syncProfiles')),
swift: /** @type {string | undefined} */ (option('swift')),
swiftModule: /** @type {string | undefined} */ (option('swiftModule')),
kotlin: /** @type {string | undefined} */ (option('kotlin')),
kotlinPackage: /** @type {string | undefined} */ (option('kotlinPackage')),
};
const options = {
module: /** @type {string | undefined} */ (option('module')),
includeDatabases: /** @type {string[] | undefined} */ (option('includeDatabases')),
excludeDatabases: /** @type {string[] | undefined} */ (option('excludeDatabases')),
baseDirectory: /** @type {any} */ (scope).directory,
};
const closing = new AbortController();
const scheduler = createRegenerationScheduler(
() =>
regenerateAll(globalTypes, schemaTypes, jsdoc, {
...options,
client: {
...client,
types: /** @type {any} */ (scope).resources?.allTypes,
signal: closing.signal,
},
}),
{
onError: (error) =>
scope.logger.error?.(
`@harperfast/schema-codegen failed to regenerate: ${/** @type {Error} */ (error)?.stack ?? error}`,
),
},
);
const schedule = () => scheduler.schedule();
let started = false;
const initialTimer = setTimeout(() => {
started = true;
scheduler.schedule();
if (shouldWatch) {
scope.databaseEvents.on('updateTable', schedule);
scope.databaseEvents.on('dropTable', schedule);
scope.databaseEvents.on('dropDatabase', schedule);
}
}, INITIAL_DELAY_MS);
if (shouldWatch && client.syncProfiles && hasClientOutputs(client)) {
try {
/** @type {any} */ (scope).handleEntry({ files: client.syncProfiles }, () => {
if (started) scheduler.schedule();
});
} catch (error) {
scope.logger.warn?.(
`@harperfast/schema-codegen cannot watch ${client.syncProfiles}; edits apply on the next schema change: ${/** @type {Error} */ (error)?.message}`,
);
}
}
scope.on('close', scopeClosed);
function scopeClosed() {
clearTimeout(initialTimer);
closing.abort();
scheduler.close();
scope.databaseEvents.off('updateTable', schedule);
scope.databaseEvents.off('dropTable', schedule);
scope.databaseEvents.off('dropDatabase', schedule);
scope.off('close', scopeClosed);
}
}