forked from asfktz/autodll-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompileIfNeeded.js
95 lines (82 loc) · 2.35 KB
/
compileIfNeeded.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
import path from 'path';
import crypto from 'crypto';
import isEmpty from 'lodash/isEmpty';
import fs from './utils/fs';
import { mkdirp } from './utils/index.js';
import { cacheDir } from './paths';
import createLogger from './createLogger';
import del from 'del';
export const HASH_FILENAME = 'lastHash';
const isCacheValid = newHash => {
return mkdirp(cacheDir)
.then(() =>
fs.readFileAsync(path.resolve(cacheDir, HASH_FILENAME), 'utf-8')
)
.then(lastHash => {
return lastHash === newHash;
})
.catch(() => {
return false;
});
};
const cleanup = () => del(path.join(cacheDir, '**/*'));
const storeHash = hash => () => {
return fs.writeFileAsync(path.resolve(cacheDir, HASH_FILENAME), hash);
};
export const compile = (settings, getCompiler) => () => {
// skip compiling if there is nothing to build
if (isEmpty(settings.entry)) return;
return new Promise((resolve, reject) => {
getCompiler().run((err, stats) => {
if (err) {
return reject(err);
}
resolve(stats);
});
});
};
const getContents = watchPath => {
try {
if (fs.existsSync(watchPath)) {
if (fs.lstatSync(watchPath).isDirectory()) {
if (watchPath.startsWith(cacheDir)) {
return '';
}
return fs
.readdirSync(watchPath)
.map(p => getContents(path.join(watchPath, p)))
.join('');
} else {
return fs.readFileSync(watchPath, 'utf-8');
}
}
} catch (e) {
//Failed to read file, fallback to string
return '';
}
};
export const getHash = settings => {
const hash = crypto.createHash('md5');
const settingsJSON = JSON.stringify(settings);
hash.update(settingsJSON);
if (Array.isArray(settings.watch)) {
hash.update(settings.watch.map(getContents).join(''));
}
return hash.digest('hex');
};
const compileIfNeeded = (settings, getCompiler) => {
const log = createLogger(settings.debug);
const currentHash = getHash(settings);
return isCacheValid(currentHash)
.then(log.tap(isValid => `is valid cache? ${isValid}`))
.then(isValid => {
if (isValid) return;
return Promise.resolve()
.then(log.tap('cleanup'))
.then(cleanup)
.then(log.tap('compile'))
.then(compile(settings, getCompiler))
.then(storeHash(currentHash));
});
};
export default compileIfNeeded;