-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionModule.js
More file actions
60 lines (49 loc) · 1.74 KB
/
extensionModule.js
File metadata and controls
60 lines (49 loc) · 1.74 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
/** @typedef {import('harperdb').Scope} Scope */
import { setTimeout as delay } from 'node:timers/promises';
import { setLogger } from './utils/logger.js';
import { regenerateAll } from './utils/regenerateAll.js';
export const suppressHandleApplicationWarning = true;
/**
* @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;
}
const watchConfig = scope.options.get(['watch']);
const shouldWatch = watchConfig === true || watchConfig === undefined;
const globalTypes = /** @type {string} */ (scope.options.get(['globalTypes']));
const schemaTypes = /** @type {string} */ (scope.options.get(['schemaTypes']));
const jsdoc = /** @type {string | undefined} */ (scope.options.get(['jsdoc']));
if (shouldWatch) {
scope.on('close', scopeClosed);
}
// Do not await this.
delay(500).then(() => {
// Initial generation
regenerateAll(globalTypes, schemaTypes, jsdoc);
if (shouldWatch) {
// Watch for schema/database changes via events
scope.databaseEvents.on('updateTable', updateTable);
scope.databaseEvents.on('dropTable', dropTable);
scope.databaseEvents.on('dropDatabase', dropDatabase);
}
});
function updateTable() {
regenerateAll(globalTypes, schemaTypes, jsdoc);
}
function dropTable() {
regenerateAll(globalTypes, schemaTypes, jsdoc);
}
function dropDatabase() {
regenerateAll(globalTypes, schemaTypes, jsdoc);
}
function scopeClosed() {
scope.databaseEvents.off('updateTable', updateTable);
scope.databaseEvents.off('dropTable', dropTable);
scope.databaseEvents.off('dropDatabase', dropDatabase);
scope.off('close', scopeClosed);
}
}