-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathutils.js
More file actions
73 lines (63 loc) · 1.7 KB
/
utils.js
File metadata and controls
73 lines (63 loc) · 1.7 KB
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
/* eslint-disable no-console */
const { relative, resolve } = require('path');
const { stat } = require('../../lib/fs');
const minimatch = require('minimatch');
const pRetry = require('p-retry');
const { promisify } = require('util');
const glob = promisify(require('glob').glob);
const hooks = require('../../lib/lib/webpack/hooks');
const PER = 0.05; // % diff
const LOG = !!process.env.WITH_LOG;
const logger = (lvl, msg) => (lvl === 'error' || LOG) && console[lvl](msg);
// `node-glob` ignore pattern buggy?
const ignores = x => !/node_modules|package-lock|yarn.lock/i.test(x);
function expand(dir, opts) {
dir = resolve(dir);
opts = Object.assign({ dot: true, nodir: true }, opts);
return glob(`${dir}/**/*.*`, opts).then(arr => arr.filter(ignores));
}
async function bytes(str) {
return (await stat(str)).size;
}
async function snapshot(dir) {
let str,
tmp,
out = {};
for (str of await expand(dir)) {
tmp = relative(dir, str);
out[tmp] = await bytes(str);
}
return out;
}
const hasKey = (key, arr) => arr.find(k => minimatch(key, k)) || false;
const isWithin = (val, tar) =>
val == tar || (val > (1 - PER) * tar && val < (1 + PER) * tar);
async function log(fn, msg) {
logger('info', `${msg} - started...`);
try {
let result = await fn();
logger('info', `${msg} - done.`);
return result;
} catch (err) {
logger('error', `${msg} - failed!`);
throw err;
}
}
function waitUntil(action, errorMessage) {
return pRetry(action, { retries: 10, minTimeout: 250 }).catch(err => {
console.log('> waitUntil error', err);
throw new Error(errorMessage);
});
}
const sleep = promisify(setTimeout);
module.exports = {
expand,
bytes,
snapshot,
log,
waitUntil,
sleep,
hasKey,
isWithin,
hooks,
};