-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGenerateResultsJsonPlugin.js
32 lines (30 loc) · 1.19 KB
/
GenerateResultsJsonPlugin.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
const fs = require('fs');
const path = require('path');
class GenerateResultsJsonPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap('GenerateResultsJsonPlugin', (compilation) => {
compilation.hooks.processAssets.tapAsync(
{
name: 'GenerateResultsJsonPlugin',
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
(assets, callback) => {
const resultsDir = path.resolve(__dirname, 'public/result');
const results = fs.readdirSync(resultsDir).filter(file => fs.statSync(path.join(resultsDir, file)).isDirectory());
const newJsonContent = JSON.stringify({ results }, null, 2);
const jsonFilePath = path.resolve(__dirname, 'src/assets/results.json');
if (fs.existsSync(jsonFilePath)) {
const existingJsonContent = fs.readFileSync(jsonFilePath, 'utf-8');
if (existingJsonContent !== newJsonContent) {
fs.writeFileSync(jsonFilePath, newJsonContent);
}
} else {
fs.writeFileSync(jsonFilePath, newJsonContent);
}
callback();
}
);
});
}
}
module.exports = GenerateResultsJsonPlugin;