-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
67 lines (53 loc) · 1.61 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
'use strict';
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const Applause = require('applause');
function getFileList(directory, filePatterns) {
// expand file globs to absolute paths
const filePaths = filePatterns.reduce((list, filePattern) => {
const expandedGlob = glob.sync(filePattern, { cwd: directory });
return [...list, ...expandedGlob];
}, []);
// return a unique list
return [...new Set(filePaths)];
}
function replaceFileContent(directory, filePath, applause) {
const fullFilePath = path.join(directory, filePath);
try {
const contents = fs.readFileSync(fullFilePath, 'utf8');
const { content: result } = applause.replace(contents);
if (result) {
fs.writeFileSync(fullFilePath, result);
}
} catch (err) {
console.error(err);
}
}
module.exports = {
name: require('./package').name,
included() {
this._super.included.apply(this, arguments);
const options = this.app.options.replace || {};
this.app.options.replace = {
files: ['index.html', '**/*.js'],
patterns: [],
enabled: true,
...options,
};
},
postBuild({ directory }) {
const options = this.app.options.replace;
if (!options.enabled) {
return;
}
// duplicate options and remove non-applause entries
const applauseOptions = { ...options };
delete applauseOptions.enabled;
delete applauseOptions.files;
const applause = Applause.create(applauseOptions);
getFileList(directory, options.files).forEach((filePath) => {
replaceFileContent(directory, filePath, applause);
});
},
};