-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-all.js
executable file
·207 lines (175 loc) · 5.78 KB
/
build-all.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env node
"use strict";
var path = require("path");
var fs = require("fs");
var fsp = require("fs/promises");
var micromatch = require("micromatch");
var recursiveReadDir = require("recursive-readdir-sync");
var terser = require("terser");
const PKG_ROOT_DIR = path.join(__dirname,"..");
const SRC_DIR = path.join(PKG_ROOT_DIR,"src");
const MAIN_COPYRIGHT_HEADER = path.join(SRC_DIR,"copyright-header.txt");
const QRDS_SRC = path.join(SRC_DIR,"qrds.js");
const NODE_MODULES_DIR = path.join(PKG_ROOT_DIR,"node_modules");
const QRCODE_SRC = path.join("external","qrcode.js");
const QRSCANNER_SRC = path.join(NODE_MODULES_DIR,"qr-scanner","qr-scanner.min.js");
const QRSCANNER_WORKER_SRC = path.join(NODE_MODULES_DIR,"qr-scanner","qr-scanner-worker.min.js");
const DIST_DIR = path.join(PKG_ROOT_DIR,"dist");
const DIST_AUTO_DIR = path.join(DIST_DIR,"auto");
const DIST_AUTO_EXTERNAL_DIR = path.join(DIST_AUTO_DIR,"external");
const DIST_AUTO_EXTERNAL_QRCODE = path.join(DIST_AUTO_EXTERNAL_DIR,path.basename(QRCODE_SRC));
const DIST_AUTO_EXTERNAL_QRSCANNER = path.join(DIST_AUTO_EXTERNAL_DIR,path.basename(QRSCANNER_SRC));
const DIST_AUTO_EXTERNAL_QRSCANNER_WORKER = path.join(DIST_AUTO_EXTERNAL_DIR,path.basename(QRSCANNER_WORKER_SRC));
const DIST_BUNDLERS_DIR = path.join(DIST_DIR,"bundlers");
const DIST_BUNDLERS_QRDS_FILE = path.join(DIST_BUNDLERS_DIR,path.basename(QRDS_SRC).replace(/\.js$/,".mjs"));
const DIST_BUNDLERS_QRDS_EXTERNAL_BUNDLE_FILE = path.join(DIST_BUNDLERS_DIR,"qrds-external-bundle.js");
main().catch(console.error);
// **********************
async function main() {
console.log("*** Building JS ***");
// try to make various dist/ directories, if needed
for (let dir of [ DIST_DIR, DIST_AUTO_DIR, DIST_BUNDLERS_DIR, DIST_AUTO_EXTERNAL_DIR, ]) {
if (!(await safeMkdir(dir))) {
throw new Error(`Target directory (${dir}) does not exist and could not be created.`);
}
}
// read package.json
var packageJSON = require(path.join(PKG_ROOT_DIR,"package.json"));
// read version number from package.json
var version = packageJSON.version;
// read main src copyright-header text
var mainCopyrightHeader = await fsp.readFile(MAIN_COPYRIGHT_HEADER,{ encoding: "utf8", });
// render main copyright header with version and year
mainCopyrightHeader = (
mainCopyrightHeader
.replace(/#VERSION#/g,version)
.replace(/#YEAR#/g,(new Date()).getFullYear())
);
// build src/* files in dist/auto/
await buildFiles(
recursiveReadDir(SRC_DIR),
SRC_DIR,
DIST_AUTO_DIR,
prepareFileContents,
/*skipPatterns=*/[ "**/*.txt", "**/*.json", "**/external" ]
);
// build src/qrds.js to bundlers/qrds.mjs
await buildFiles(
[ QRDS_SRC, ],
SRC_DIR,
DIST_BUNDLERS_DIR,
(contents,outputPath,filename = path.basename(outputPath)) => prepareFileContents(
// alter (remove) "external.js" dependencies-import
// since bundlers handle dependencies differently
contents.replace(
/import[^\r\n]*".\/external.js";?/,
"import QRScanner from \"qr-scanner\""
),
outputPath.replace(/\.js$/,".mjs"),
`bundlers/${filename.replace(/\.js$/,".mjs")}`
),
/*skipPatterns=*/[ "**/*.txt", "**/*.json", "**/external" ]
);
// handle dependencies
var [
qrCodeContents,
] = await Promise.all([
fsp.readFile(QRCODE_SRC,{ encoding: "utf8", }),
]);
// build qrds-external-bundle.js
var qrdsExternalBundleContents = [
`/*! ${path.basename(QRCODE_SRC)} */`, await minifyJS(qrCodeContents,/*esModuleFormat=*/false),
].join("\n");
await Promise.all([
// bundlers/qrds-external-bundle.js (for bundlers)
fsp.writeFile(
DIST_BUNDLERS_QRDS_EXTERNAL_BUNDLE_FILE,
qrdsExternalBundleContents,
{ encoding: "utf8", }
),
fsp.writeFile(
DIST_AUTO_EXTERNAL_QRCODE,
qrCodeContents,
{ encoding: "utf8", }
),
fsp.copyFile(
QRSCANNER_SRC,
DIST_AUTO_EXTERNAL_QRSCANNER
),
fsp.copyFile(
QRSCANNER_WORKER_SRC,
DIST_AUTO_EXTERNAL_QRSCANNER_WORKER
),
]);
console.log("Complete.");
// ****************************
async function prepareFileContents(contents,outputPath,filename = path.basename(outputPath)) {
// JS file (to minify)?
if (/\.[mc]?js$/i.test(filename)) {
contents = await minifyJS(contents);
}
// add copyright header
return {
contents: `${
mainCopyrightHeader.replace(/#FILENAME#/g,filename)
}\n${
contents
}`,
outputPath,
};
}
}
async function buildFiles(files,fromBasePath,toDir,processFileContents,skipPatterns) {
for (let fromPath of files) {
// should we skip copying this file?
if (matchesSkipPattern(fromPath,skipPatterns)) {
continue;
}
let relativePath = fromPath.slice(fromBasePath.length);
let outputPath = path.join(toDir,relativePath);
let outputDir = path.dirname(outputPath);
if (!(fs.existsSync(outputDir))) {
if (!(await safeMkdir(outputDir))) {
throw new Error(`While copying src/* to dist/, directory (${outputDir}) could not be created.`);
}
}
let contents = await fsp.readFile(fromPath,{ encoding: "utf8", });
({ contents, outputPath, } = await processFileContents(contents,outputPath));
await fsp.writeFile(outputPath,contents,{ encoding: "utf8", });
}
}
async function minifyJS(contents,esModuleFormat = true) {
let result = await terser.minify(contents,{
mangle: {
keep_fnames: true,
},
compress: {
keep_fnames: true,
},
output: {
comments: /^!/,
},
module: esModuleFormat,
});
if (!(result && result.code)) {
if (result.error) throw result.error;
else throw result;
}
return result.code;
}
function matchesSkipPattern(pathStr,skipPatterns) {
if (skipPatterns && skipPatterns.length > 0) {
return (micromatch(pathStr,skipPatterns).length > 0);
}
}
async function safeMkdir(pathStr) {
if (!fs.existsSync(pathStr)) {
try {
await fsp.mkdir(pathStr,{ recursive: true, mode: 0o755, });
return true;
}
catch (err) {}
return false;
}
return true;
}