forked from microsoft/azure-devops-extension-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpackage.js
80 lines (71 loc) · 2.53 KB
/
buildpackage.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
/**
* This is solely a build script, intended to prep the azure-devops-extension-api npm package for publishing.
*/
const { execSync } = require("child_process");
const fs = require("fs");
const glob = require("glob");
const path = require("path");
const copy = require("recursive-copy");
const shell = require("shelljs");
const UglifyES = require("uglify-es");
(async function() {
// Clean bin directory
console.log("# Cleaning bin. Running shelljs rm -rf ./bin");
shell.rm("-rf", "./bin");
// Compile typescript
console.log("# Compiling TypeScript. Executing `node_modules\\.bin\\tsc -p ./tsconfig.json`.");
try {
execSync("node_modules\\.bin\\tsc -p ./tsconfig.json", {
stdio: [0, 1, 2],
shell: true,
cwd: __dirname,
});
} catch (error) {
console.log("ERROR: Failed to build TypeScript.");
process.exit(1);
}
// Copy ts files to bin
console.log("# Copy declare files to bin.");
try {
await copy(path.join(__dirname, "src"), path.join(__dirname, "bin"), {
filter: f => {
return f.endsWith(".d.ts");
},
});
} catch (e) {
console.log("Copy failed. " + error);
}
// Uglify JavaScript
console.log("# Minifying JS using the UglifyES API, replacing un-minified files.");
let count = 0;
const files = await new Promise((resolve, reject) => {
glob("./bin/**/*.js", (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
for (const file of files) {
if (file.includes("node_modules/")) {
continue;
}
fs.writeFileSync(
file.substr(0, file.length - 2) + "min.js",
UglifyES.minify(fs.readFileSync(file, "utf-8"), { compress: true, mangle: true }).code,
"utf-8",
);
count++;
}
console.log(`-- Minified ${count} files.`);
// Copy package.json, LICENSE, README.md to bin
console.log("# Copying package.json, LICENSE, and README.md to bin.");
try {
await copy(path.join(__dirname, "package.json"), path.join(__dirname, "bin", "package.json"));
await copy(path.join(__dirname, "LICENSE"), path.join(__dirname, "bin", "LICENSE"));
await copy(path.join(__dirname, "README.md"), path.join(__dirname, "bin", "README.md"));
} catch (error) {
console.log("ERROR: Failed to copy package.json, LICENSE, or README.md - " + error);
}
})();