-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.ts
171 lines (143 loc) · 4.7 KB
/
build.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { readFile, writeFile, copyFile, mkdir, existsSync } from "fs";
import { promisify } from "util";
import { exec as execCommand } from "child_process";
import { ncp } from "ncp";
import terser from "terser";
import chokidar from "chokidar";
import sass from "sass";
import autoprefixer from "autoprefixer";
import postcss from "postcss";
import * as rollup from "rollup";
import rollupConfig, { getConfig } from "./config/rollup";
import * as pkg from "./package.json";
const version = `/* simple-tree-component v${pkg.version},, @license MIT */`;
// eslint-disable-next-line @typescript-eslint/naming-convention
const DEV_MODE = process.argv.indexOf("--dev") > -1;
const paths = {
style: "./src/style/index.scss",
};
const watchers: chokidar.FSWatcher[] = [];
function logErr(e: Error | string) {
console.error(e);
}
async function readFileAsync(path: string): Promise<string> {
try {
const buf = await promisify(readFile)(path);
return buf.toString();
} catch (e) {
logErr(e);
return e.toString();
}
}
async function uglify(src: string) {
const minified = await terser.minify(src, {
output: {
preamble: version,
comments: false,
},
});
return minified.code as string;
}
async function buildBundleJs() {
const bundle = await rollup.rollup(rollupConfig);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return bundle.write(rollupConfig.output as rollup.OutputOptions);
}
async function buildScripts() {
try {
await buildBundleJs();
const transpiled = await readFileAsync("./dist/simple-tree-component.js");
promisify(writeFile)("./dist/simple-tree-component.min.js", await uglify(transpiled));
} catch (e) {
logErr(e);
}
}
async function transpileStyle(src: string, compress = false) {
const sassResult = await sass.compileAsync(src, { style: compress ? "compressed" : "expanded" });
const postCssResult = await postcss([autoprefixer]).process(sassResult.css);
return postCssResult.css;
}
async function buildStyle() {
try {
await Promise.all([
promisify(writeFile)("./dist/simple-tree-component.css", await transpileStyle(paths.style)),
promisify(writeFile)("./dist/simple-tree-component.min.css", await transpileStyle(paths.style, true)),
]);
} catch (e) {
logErr(e);
}
}
async function ensureDistFolder() {
try {
if (!existsSync("./dist")) {
await promisify(mkdir)("./dist");
}
} catch (e) {
logErr(e);
}
}
async function copySass() {
await promisify(ncp)("./src/style", "./dist/scss");
}
function setupWatchers() {
watch("./src/style/*.scss", () => {
buildStyle();
});
watch("./src", (path: string) => {
execCommand(`npm run format -- ${path}`, {
cwd: __dirname,
});
});
watch("./index.template.html", async () => {
await promisify(copyFile)("./index.template.html", "./index.html");
});
}
function watch(path: string, cb: (path: string) => void = (s: string) => s) {
watchers.push(
chokidar
.watch(path, {
// awaitWriteFinish: {
// stabilityThreshold: 500,
// },
//usePolling: true,
})
.on("change", cb)
.on("error", logErr)
);
}
async function start() {
if (DEV_MODE) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(rollupConfig.output as rollup.OutputOptions).sourcemap = true;
await promisify(copyFile)("./index.template.html", "./index.html");
const write = (s: string) => process.stdout.write(`rollup: ${s}`);
const watcher = rollup.watch([getConfig({ dev: true })]);
watcher.on("event", logEvent);
function exit() {
watcher.close();
watchers.forEach((w) => w.close());
}
interface RollupWatchEvent {
code: string;
input?: string | string[] | { [key: string]: string };
output?: readonly string[];
}
function logEvent(e: RollupWatchEvent) {
write(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[e.code, e.input && `${e.input} -> ${e.output!}`, "\n"].filter((x) => x).join(" ")
);
}
//catches ctrl+c event
process.on("SIGINT", exit);
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exit);
process.on("SIGUSR2", exit);
setupWatchers();
}
ensureDistFolder();
buildScripts();
buildStyle();
copySass();
}
start();