-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.js
75 lines (59 loc) · 2.03 KB
/
install.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
var fs = require('fs');
var path = require('path');
// Process arguments
if (process.argv.length < 3) {
console.log('Usage: install <path to theme>');
process.exit(0);
}
var _themePath = path.resolve(process.argv[2]);
// Helper functions
function corePath() {
var params = [__dirname].concat(Array.from(arguments));
return path.join.apply(null, params);
}
function themePath() {
var params = [_themePath].concat(Array.from(arguments));
return path.join.apply(null, params);
}
function loadJSON(path) {
var contents = fs.readFileSync(path, 'utf-8');
return JSON.parse(contents);
}
function saveJSON(path, data) {
var contents = JSON.stringify(data, null, ' ');
fs.writeFileSync(path, contents, 'utf-8');
}
function merge(source, destination, fn) {
var sourceData = loadJSON(source);
var destinationData = loadJSON(destination);
var processedData = fn(sourceData, destinationData);
saveJSON(destination, processedData);
}
// Install language files
merge(corePath('lang/en.json'), themePath('lang/en.json'), (coreLang, themeLang) => {
return Object.assign({}, themeLang, coreLang);
});
// Install package.json
merge(corePath('config/package.json'), themePath('package.json'), (corePackage, themePackage) => {
Object.keys(corePackage.devDependencies).forEach(key => {
themePackage.devDependencies[key] = corePackage.devDependencies[key];
});
return themePackage;
});
// Install config.json
merge(corePath('config/config.json'), themePath('config.json'), (coreConfig, themeConfig) => {
Object.keys(coreConfig.settings).forEach(key => {
themeConfig.settings[key] = coreConfig.settings[key];
});
return themeConfig;
});
// schema.json
merge(corePath('config/schema.json'), themePath('schema.json'), (coreSchema, themeSchema) => {
var insert = coreSchema.insert || [];
var remove = coreSchema.remove || [];
// Remove schema sections
themeSchema = themeSchema.filter(schema => remove.indexOf(schema.name) === -1);
// Insert schema sections
themeSchema = themeSchema.concat(insert);
return themeSchema;
});