-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_build.js
106 lines (90 loc) · 2.71 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* eslint no-console:0, consistent-return:0 */
const path = require('path');
const fs = require('fs-promise');
const webpack = require('webpack');
const glob = require('glob');
const webpackConfig = require('./webpack.config');
const bundler = webpack(webpackConfig);
// ===========================================================================
// CONFIG
// ===========================================================================
const PATHS = webpackConfig.data.PATHS;
// ===========================================================================
// RUN
// ===========================================================================
(async() => {
try {
await clean();
await copyAssets();
await build();
console.log('Done.');
}
catch (err) {
console.error(err.toString());
}
})();
// ===========================================================================
// TASKS
// ===========================================================================
/**
* Empty DIST directory
*/
function clean() {
console.log('Cleaning DIST directory.');
return fs.emptyDirSync(PATHS.build());
}
/**
* Async, copy all non-(js|css) assets to DIST
*/
function copyAssets() {
console.log('Copying assets.');
/**
* Copy in parallel, resolve when all are complete.
*/
return new Promise((resolve, reject) => {
const completedStack = []; // track completed copies
// get files
glob('src/**/*.!(scss|js)', (err, files) => {
if (err) {return reject(err);}
// for each file
for (const file of files) {
// copy to DIST, update completed stack
const fileDest = getDest(file);
fs.copy(file, fileDest, setComplete(files, fileDest));
}
});
/**
* Resolve promise when all files are copied
*/
function setComplete(files, fileDest) {
return (err, file) => {
if (err) {return reject(err);}
// add current file to completed stack
completedStack.push([ file, fileDest ]);
// when completed stack matches initial list
if (completedStack.length === files.length) {
// resolve promise
return resolve(completedStack);
}
};
}
});
}
/**
* Async, run webpack
*/
function build() {
console.log('Running webpack build.');
return new Promise((resolve, reject) => {
bundler.run((err, stats) => (err ? reject(err) : resolve(stats)));
});
}
// ===========================================================================
// UTILS
// ===========================================================================
/**
* Map file locations from source to dist.
*/
function getDest(file) {
return PATHS.build(file.replace(`sass${path.sep}`, '').replace(`src${path.sep}`, ''));
}