-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMapper.js
More file actions
58 lines (48 loc) · 1.18 KB
/
FileMapper.js
File metadata and controls
58 lines (48 loc) · 1.18 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
'use strict';
const fs = require('fs');
const walk = require('walk');
class FileMapper {
constructor(options) {
this.options = options;
}
_getRootPathTo(fileOrDir) {
return `${process.env.PWD}/${fileOrDir}`;
}
_mapSingleFile(mapFile, filePath) {
let result = fs.readFileSync(this._getRootPathTo(filePath), 'utf8');
Object.keys(mapFile).forEach(key => {
result = result.replace(key, mapFile[key].newFileName);
});
fs.writeFileSync(this._getRootPathTo(filePath), result);
}
_mapDir(mapFile) {
const walkerOptions = {
followLinks: false,
listeners: {
file: (root, fileStats, next) => {
this._mapSingleFile(mapFile, `${this.options.dirPath}/${fileStats.name}`);
next();
},
},
};
walk.walkSync(this.options.dirPath, walkerOptions);
}
_mapFiles() {
// eslint-disable-next-line
const mapFile = require(this._getRootPathTo(this.options.mapPath));
if (!this.options.dirPath) {
this._mapSingleFile(mapFile, this.options.filePath);
} else {
this._mapDir(mapFile);
}
}
_onDone(compiler) {
compiler.plugin('done', () => {
this._mapFiles();
});
}
apply(compiler) {
this._onDone(compiler);
}
}
module.exports = FileMapper;