forked from outline/outline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·91 lines (79 loc) · 2.43 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
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
const { exec } = require("child_process");
const { readdirSync, existsSync } = require("fs");
const getDirectories = (source) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
/**
* Executes a shell command and return it as a Promise.
* @param cmd {string}
* @return {Promise<string>}
*/
function execAsync(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout ? stdout : stderr);
}
});
});
}
async function build() {
// Clean previous build
console.log("Clean previous build…");
await Promise.all([
execAsync("rm -rf ./build/server"),
execAsync("rm -rf ./build/plugins"),
]);
const d = getDirectories("./plugins");
// Compile server and shared
console.log("Compiling…");
await Promise.all([
execAsync(
"yarn babel --extensions .ts,.tsx --quiet -d ./build/server ./server"
),
execAsync(
"yarn babel --extensions .ts,.tsx --quiet -d ./build/shared ./shared"
),
...d.map(async (plugin) => {
const hasServer = existsSync(`./plugins/${plugin}/server`);
if (hasServer) {
await execAsync(
`yarn babel --extensions .ts,.tsx --quiet -d "./build/plugins/${plugin}/server" "./plugins/${plugin}/server"`
);
}
const hasShared = existsSync(`./plugins/${plugin}/shared`);
if (hasShared) {
await execAsync(
`yarn babel --extensions .ts,.tsx --quiet -d "./build/plugins/${plugin}/shared" "./plugins/${plugin}/shared"`
);
}
}),
]);
// Copy static files
console.log("Copying static files…");
await Promise.all([
execAsync(
"cp ./server/collaboration/Procfile ./build/server/collaboration/Procfile"
),
execAsync(
"cp ./server/static/error.dev.html ./build/server/error.dev.html"
),
execAsync(
"cp ./server/static/error.prod.html ./build/server/error.prod.html"
),
execAsync("cp package.json ./build"),
...d.map(async (plugin) =>
execAsync(
`mkdir -p ./build/plugins/${plugin} && cp ./plugins/${plugin}/plugin.json ./build/plugins/${plugin}/plugin.json 2>/dev/null || :`
)
),
]);
console.log("Done!");
}
void build();