forked from andreasbm/weightless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-build.js
108 lines (89 loc) · 2.17 KB
/
post-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
99
100
101
102
103
104
105
106
107
108
const glob = require("glob");
const path = require("path");
const {copySync, readFile, outputFileSync, remove} = require("fs-extra");
const replaceExt = require("replace-ext");
const outLib = "dist";
/**
* Makes the dist folder ready.
* @returns {Promise<void>}
*/
async function postBuild () {
await rewrite(outLib);
copyFile("./package.json", `./${outLib}/package.json`);
copyFile("./README.md", `./${outLib}/README.md`);
// Copy the SASS files
copyFile("./src/lib/style/base", `./${outLib}/style/base`);
copyFile("./src/lib/style/base.scss", `./${outLib}/style/base.scss`);
}
/**
* Copies a file from a src to a dest.
* @param src
* @param dest
*/
function copyFile (src, dest) {
copySync(path.resolve(__dirname, src), path.resolve(__dirname, dest));
}
/**
* Returns whether the path has a .d.ts extention.
* @param path
* @returns {boolean}
*/
function isDeclaration (path) {
return path.endsWith(".d.ts");
}
/**
* Returns whether the path has a scss extention.
* @param path
* @returns {boolean}
*/
function isSCSS (path) {
return path.endsWith(".scss");
}
/**
* Rewrites files ending with .ts or .scss to .js instead.
* @param path
*/
function rewrite (path) {
return new Promise(res => {
glob(`${path}/**/*.{ts,scss}`, {}, (err, files) => {
// Check if an error occurred.
if (err) {
throw err;
}
// Rewrite each file
for (const file of files) {
// Don't rewrite declaration files
if (isDeclaration(file)) {
continue;
}
rewriteFile(file);
replaceExt(file, ".js");
}
res();
});
});
}
/**
* Rewrites a file.
* @param path
*/
function rewriteFile (path) {
readFile(path, (err, buffer) => {
// If the file could not be read, abort!
if (err != null) {
throw err;
}
// Convert the buffer into a string
let content = buffer.toString("utf8");
// Replace the imports
content = content.replace(/\.ts';/gm, ".js';");
content = content.replace(/\.scss';/gm, ".scss.js';");
// Write new file and remove old
const ext = isSCSS(path) ? `.scss.js` : `.js`;
outputFileSync(replaceExt(path, ext), content);
remove(path);
});
}
postBuild().then(_ => {
console.log(">> Postbuild completed");
});