-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
116 lines (101 loc) · 2.89 KB
/
build.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import esbuild from 'esbuild';
import { generateIndexFiles } from './src/utils/generateTestIndices.js';
import { polyfillNode } from 'esbuild-plugin-polyfill-node';
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';
const isWatchMode = process.argv.includes('--watch');
function generateImportsPlugin({ pattern }) {
return {
name: 'generate-imports',
setup(build) {
build.onStart(async () => {
await generateIndexFiles(pattern);
});
}
};
}
async function dereferenceSchemas() {
const schemaFiles = [];
function findSchemaFiles(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
findSchemaFiles(fullPath);
} else if (file.endsWith('.schema.json')) {
schemaFiles.push(fullPath);
}
}
}
findSchemaFiles(path.resolve('./dist'));
// Run dereferencing script on each schema file
for (const schemaPath of schemaFiles) {
await new Promise((resolve) => {
exec(`node ./scripts/derefJsonSchemas.js ${schemaPath} ${schemaPath}`, (error, stdout, stderr) => {
if (stderr) {
console.error(`Error processing ${schemaPath}:`, stderr);
} else {
// //console.log(`Dereferenced ${schemaPath}:`, stdout);
}
resolve();
});
});
}
}
const plugins = [
generateImportsPlugin({
pattern: 'src/nips/**/*/tests/*.ts',
})
];
const browserConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
tsconfig: 'tsconfig.json',
outfile: 'dist/web.js',
platform: 'browser',
plugins: [
polyfillNode(),
...plugins
]
};
const serverConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
tsconfig: './tsconfig.json',
outfile: 'dist/server.js',
format: 'esm',
platform: 'node',
plugins
};
async function build() {
try {
const browserContext = await esbuild.context(browserConfig);
const serverContext = await esbuild.context(serverConfig);
if (isWatchMode) {
await Promise.all([
browserContext.watch(),
serverContext.watch()
]);
//console.log("Watching for changes in src...");
watchSrcDirectory();
} else {
await Promise.all([browserContext.rebuild(), serverContext.rebuild()]);
//console.log("Build complete for both web and server targets.");
await dereferenceSchemas();
}
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}
function watchSrcDirectory() {
fs.watch('./src', { recursive: true }, (eventType, filename) => {
if (filename && (filename.endsWith('.ts') || filename.endsWith('.json'))) {
//console.log(`Source change detected in ${filename}, rebuilding and running dereferenceSchemas...`);
build().then(() => dereferenceSchemas());
}
});
}
build();