-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·113 lines (104 loc) · 2.66 KB
/
index.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
#!/usr/bin/env node
const chokidar = require('chokidar');
const program = require('commander');
const glob = require('globby');
const {
resolve
} = require('path');
const {
createGzip,
createBrotliCompress
} = require('zlib');
const fs = require('fs-extra');
program.option('-w, --watch', 'Watch directories', false);
program.parse(process.argv);
const package = JSON.parse(fs.readFileSync('package.json')).cppConfig
if (!package) {
return console.log('No configuration')
}
let warn = () => {
console.warn('WARN: Brotli is only enabled in node 11.7+')
warn = () => {}
}
const handleFile = async (path, dest, keepPath, br, gz, ignoreWatchDir) => {
let modifiedPath = path.split('/')
if (ignoreWatchDir == true) {
modifiedPath.shift()
modifiedPath = modifiedPath.join('/')
} else if (keepPath == false) {
modifiedPath = modifiedPath.pop()
} else {
modifiedPath = modifiedPath.join('/')
}
const filePath = resolve(dest, modifiedPath)
await fs.createFile(filePath)
const read = fs.createReadStream(path)
if (dest !== '.' && dest !== './') {
read.pipe(fs.createWriteStream(resolve(dest, filePath)));
}
if (br == true) {
if (createBrotliCompress) {
const brotli = createBrotliCompress(package.brConfig || {});
const br = fs.createWriteStream(`${modifiedPath}.br`);
read.pipe(brotli).pipe(br);
} else {
warn()
}
}
if (gz == true) {
const gzip = createGzip(package.gzConfig || {});
const gz = fs.createWriteStream(`${modifiedPath}.gz`);
read.pipe(gzip).pipe(gz);
}
};
if (program.watch && package.watch) {
package.watch.map(({
dirs,
ignore,
dest,
keepPath,
ignoreWatchDir
}) => {
chokidar
.watch(dirs, {
dot: true,
...ignore && {
ignored: ignore.substr(1),
},
persistent: true,
})
.on('add', path => {
handleFile(path, dest, keepPath, false, false, ignoreWatchDir)
})
.on('change', path => {
handleFile(path, dest, keepPath, false, false, ignoreWatchDir)
})
.on('unlink', async path => {
await fs.remove(`${__dirname}/dist/${path}`);
})
.on('error', async error => {
console.error('Error happened', error);
});
});
}
const watch = (package.copy || []).concat(program.watch ? [] : package.watch || [])
watch.concat(watch).map(async (directory) => {
const {
dirs,
dest,
keepPath,
ignore,
gz,
br,
ignoreWatchDir
} = directory
if (ignore) {
dirs.push(ignore)
}
const matches = await glob(dirs, {
dot: true
});
matches.map(async path => {
handleFile(path, dest, keepPath, br, gz, ignoreWatchDir)
});
});