forked from etienne-martin/device-detector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-fixtures.js
41 lines (30 loc) · 1.1 KB
/
load-fixtures.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
const path = require("path");
const fs = require("fs");
const YAML = require("js-yaml");
const recursive = require("recursive-readdir");
const loadYaml = (filepath) => {
return YAML.load(fs.readFileSync(filepath, "utf8"), {
schema: YAML.FAILSAFE_SCHEMA
});
};
const ignoreFilter = (file, stats) => {
if (stats.isDirectory()) return false;
return !(stats.isFile() && path.extname(file) === ".yml");
};
const ensureDirectoryExistence = (filePath) => {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) return;
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
};
recursive("./node_modules/device-detector", [ignoreFilter], (err, files) => {
for (const file of files) {
let destination = file.replace(RegExp(".yml$", "i"), ".json");
destination = destination.replace("node_modules/device-detector", "fixtures");
destination = destination.replace("node_modules\\device-detector", "fixtures");
ensureDirectoryExistence(destination);
const yaml = loadYaml(file);
const json = JSON.stringify(yaml);
fs.writeFileSync(destination, json);
}
});