forked from sequelize/sequelize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
99 lines (85 loc) · 2.5 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
'use strict';
const glob = require('fast-glob');
const { promisify } = require('util');
const { build } = require('esbuild');
const fs = require('fs');
const copyFiles = promisify(require('copyfiles'));
const path = require('path');
const exec = promisify(require('child_process').exec);
const stat = promisify(fs.stat);
// if this script is moved, this will need to be adjusted
const rootDir = __dirname;
const outdir = path.join(rootDir, 'lib');
const typesDir = path.join(rootDir, 'types');
async function rmDir(dirName) {
try {
await stat(dirName);
const rm = promisify(fs.rm);
await rm(dirName, { recursive: true });
} catch {
/* no-op */
}
}
async function main() {
console.info('Compiling sequelize...');
const [sourceFiles] = await Promise.all([
// Find all .js and .ts files from /src
glob('./src/**/*.{mjs,cjs,js,mts,cts,ts}', { onlyFiles: true, absolute: false }),
// Delete /lib for a full rebuild.
rmDir(outdir),
// Delete /types for a full rebuild.
rmDir(typesDir),
]);
const filesToCompile = [];
const filesToCopyToLib = [];
const declarationFiles = [];
for (const file of sourceFiles) {
// mjs files cannot be built as they would be compiled to commonjs
if (file.endsWith('.mjs')) {
filesToCopyToLib.push(file);
} else if (file.endsWith('.d.ts')) {
declarationFiles.push(file);
} else {
filesToCompile.push(file);
}
}
// copy .d.ts files prior to generating them from the .ts files
// so the .ts files in lib/ will take priority..
await copyFiles(
// The last path in the list is the output directory
[...declarationFiles, typesDir],
{ up: 1 },
);
if (filesToCopyToLib.length > 0) {
await copyFiles(
// The last path in the list is the output directory
filesToCopyToLib.concat(outdir),
{ up: 1 },
);
}
await Promise.all([
build({
// Adds source mapping
sourcemap: true,
// The compiled code should be usable in node v14.17
target: 'node14.17',
// The source code's format is commonjs.
format: 'cjs',
outdir,
entryPoints: filesToCompile
.map(file => path.resolve(file)),
}),
exec('tsc', {
env: {
// binaries installed from modules have symlinks in
// <pkg root>/node_modules/.bin.
PATH: `${process.env.PATH || ''}:${path.join(
rootDir,
'node_modules/.bin',
)}`,
},
cwd: rootDir,
}),
]);
}
main().catch(console.error).finally(process.exit);