-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
132 lines (114 loc) · 3.85 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Imports
const fs = require('fs-extra');
const path = require('path');
const yaml = require('js-yaml');
const rmdir = require('rmdir-recursive').sync;
const validator = require('./lib/schema');
const postProcessor = require('./lib/post-processor');
// Constants
const outputDir = 'out/';
const baseDir = 'presets/';
/**
* Process yaml file and translate into JSON. Output in output/ directory.
* @param f the input yaml file
* @return {"name": {"en": "The Preset Name"}, "description": {"en": "Your preset description"}, "file": "preset_file.json", "image": "image_url"}
*/
function processYaml(f) {
// Convert Yaml to JSON
var parsed = yaml.safeLoad(fs.readFileSync(baseDir + f));
var presetName = path.basename(f, path.extname(f));
var filename = presetName + '.json';
var validation = validator.validate(parsed, presetName);
if (!validation.valid) {
console.error("File " + f + " is invalid. Will be ignored in parsing.");
console.error(validation.errors);
process.exit(1);
} else {
console.log("File " + f + " is valid. Will be parsed as preset.");
postProcessor.postProcessCleanup(presetName, parsed);
}
fs.writeFileSync(outputDir + filename, JSON.stringify(parsed));
return {"name": parsed.name, "description": parsed.description, "version": parsed.version, "image": parsed.image, "file": filename};
};
/**
* Process json file. Output in output/ directory.
* @param f the input json file
* @return {"name": {"en": "The Preset Name"}, "description": {"en": "Your preset description"}, "file": "preset_file.json"}
*/
function processJson(f) {
var parsed = JSON.parse(fs.readFileSync(baseDir + f));
var presetName = path.basename(f, path.extname(f));
var filename = path.basename(f);
var filenameYml = presetName + '.yml';
var validation = validator.validate(parsed, presetName);
if (!validation.valid) {
console.error("File " + f + " is invalid. Will be ignored in parsing.");
console.error(validation.errors);
process.exit(1);
} else {
console.log("File " + f + " is valid. Will be parsed as preset.");
postProcessor.postProcessCleanup(parsed);
}
// Write to output directory
fs.writeFileSync(outputDir + filename, JSON.stringify(parsed));
// Convert JSON to Yaml
fs.writeFileSync(baseDir + filenameYml, yaml.safeDump(parsed));
// Remove Old JSON File
fs.unlinkSync(baseDir + filename);
return {"name": parsed.name, "description": parsed.description, "image": parsed.image, "file": filename};
};
/**
* Main processing function factory
* @param presets the presets object
* @returns {Function} the function which will process all files
*/
function getFileProcessor(presets) {
return (f) => {
try {
// Only process files
var s = fs.statSync(baseDir + f);
if (!s.isFile()) {
return;
}
const ext = path.extname(f);
const basename = path.basename(f);
const presetName = path.basename(f, ext);
let p;
// Process yaml or json files
switch (ext) {
case '.yml':
case '.yaml':
p = processYaml(f);
presets[p.file] = p;
break;
case '.json':
p = processJson(f);
validator.validate(p, presetName);
presets[p.file] = p;
break;
}
} catch (err) {
console.error(err);
process.exit(-1);
}
}
};
function exec() {
// Create output directory
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const dir = fs.readdirSync(baseDir);
const output = {
revision: process.argv[2],
lastUpdate: new Date().toISOString(),
presets: {}
};
// Parse directory and process each file
dir.forEach(getFileProcessor(output.presets));
// Copy all resources to output
fs.copySync('resources', outputDir + 'resources');
fs.writeFileSync(outputDir + 'presets.json', JSON.stringify(output));
};
// Execute code
exec();