-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (63 loc) · 1.8 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
68
69
70
71
72
73
74
75
const path = require("path");
const { cloneDeep } = require("lodash");
const webpack = require("webpack");
const Ajv = require("ajv");
const schema = require("./schema.json");
const baseConfig = require("./webpack.config");
const ajv = new Ajv({ allErrors: true });
/**
* Throw an error if the radar data is invalid
* @private
* @param {object} data The data for the radar
*/
function validate(data) {
const valid = ajv.validate(schema, data);
if (!valid) {
throw new Error(`Config did not match JSON schema: ${ajv.errorsText()}`);
}
}
/**
* A promise-generating form of Webpack
* @private
* @param {object} config The webpack config
* @return {Promise<void>}
*/
const build = (config) =>
new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err || stats.hasErrors()) {
reject(err || stats.toString({ colors: true }));
} else {
resolve();
}
});
});
/**
* @typedef RadarOptions
* @property {('development'|'production')} [mode="production"] The webpack mode
*/
/**
* Generates a static Thoughtworks Radar.
* @param {object} data The data for the radar
* @param {string} outputArg The relative or absolute path to the output directory
* @param {RadarOptions} [options] The radar options
* @return {Promise<string>} A promise resolving to the output directory path
*/
async function techRadarGenerator(
data,
outputArg,
{ mode = "production" } = {}
) {
validate(data);
const outputPath = path.resolve(outputArg);
const config = cloneDeep(baseConfig);
config.mode = mode;
config.output.path = outputPath;
const valLoader = config.module.rules.find(
(el) => el.loader === require.resolve("val-loader")
);
valLoader.options.data = data;
await build(config);
return outputPath;
}
module.exports = techRadarGenerator;