-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathwatchAssets.js
70 lines (61 loc) · 1.92 KB
/
watchAssets.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
'use strict';
const path = require('path');
const _ = require('lodash');
const chokidar = require('chokidar');
const logger = require('./log');
let copyFile = require('./copyFile'); // eslint-disable-line prefer-const
function onWatchTripped(patternlab, p, assetBase, basePath, dir, copyOptions) {
const subPath = p.replace(assetBase, '');
_.each(patternlab.uikits, (uikit) => {
const destination = path.resolve(
basePath,
uikit.outputDir,
dir.public + '/' + subPath
);
copyFile(p, destination, copyOptions);
});
}
const watchAssets = (
patternlab,
basePath,
dir,
key,
copyOptions,
watchOnce
) => {
const assetBase = path.resolve(basePath, dir.source);
const assetsToIgnore = patternlab.config.transformedAssetTypes
? patternlab.config.transformedAssetTypes.join('|')
: '';
logger.debug(`Pattern Lab is watching ${assetBase} for changes`);
if (patternlab.watchers[key]) {
patternlab.watchers[key].close();
}
const assetWatcher = chokidar.watch(assetBase, {
// *ignored* combines file types that the wrapper is watching, passed to pl config
// regex string escapes backslashes for JS
// second part of regex is holdover from existing ignore regex for '/index.html' and other
// files meant to be ignored, not based on file type
ignored: new RegExp(
`(?:(?:.*\\.(?:${assetsToIgnore})$)|(?:(^|[\\/\\\\])\\..))`,
'i'
),
// ignored: /(^|[\/\\])\../, //old version
ignoreInitial: false,
awaitWriteFinish: {
stabilityThreshold: 200,
pollInterval: 100,
},
persistent: !watchOnce,
});
//watch for changes and copy
assetWatcher
.on('add', (p) => {
onWatchTripped(patternlab, p, assetBase, basePath, dir, copyOptions);
})
.on('change', (p) => {
onWatchTripped(patternlab, p, assetBase, basePath, dir, copyOptions);
});
patternlab.watchers[key] = assetWatcher;
};
module.exports = watchAssets;