-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (117 loc) · 3.57 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
const merge = require('lodash.merge');
const fs = require('fs');
const path = require('path');
module.exports = class MergeThemeJson {
constructor(baseFile, targetDirArray, output) {
this.baseFile = baseFile;
this.targetDirArray = targetDirArray;
this.output = output;
this.jsonFiles = [];
this.directories = [];
this.resultObj = {};
}
// get file and directory names
getJsonFiles(dir) {
if (fs.existsSync(dir)) {
const filenames = fs.readdirSync(dir);
for (let i = 0; i < filenames.length; i++) {
const filename = filenames[i];
const fullPath = path.join(dir, filename);
const isJsonFile = filename && fs.statSync(fullPath).isFile() && filename.match(/^_.*\.(json)$/);
// case of file
if(isJsonFile){
this.jsonFiles.push(fullPath);
// case of directory
}else if(fs.statSync(fullPath).isDirectory()) {
this.directories.push(fullPath);
}
}
} else {
console.error('This directory is not found. %j', dir);
}
}
// delete propaties prefixed with a "//".
deleteComment(sourceObj) {
for (const key in sourceObj) {
if (key.match(/^\/\//)) {
delete sourceObj[key];
}
const sourceValue = sourceObj[key];
if (typeof sourceValue === "object") {
this.deleteComment(sourceValue);
}
}
}
// delete {} and []
deleteEmptyPropaty(obj) {
for (const key in obj) {
const strValue = JSON.stringify(obj[key]);
if (strValue === '{}' || strValue === '[]') {
delete obj[key];
}
if (typeof obj[key] === "object") {
this.deleteEmptyPropaty(obj[key]);
}
}
}
// delete null object
deleteArrayNull(obj) {
for (const key in obj) {
if (Array.isArray(obj[key])) {
obj[key] = obj[key].filter(v => v);
}
if (typeof obj[key] === "object") {
this.deleteArrayNull(obj[key]);
}
}
}
// merge json objects
mergeJson(jsonFiles) {
if (fs.existsSync(this.baseFile)) {
const baseObj = JSON.parse(fs.readFileSync(this.baseFile, 'utf8'));
for (let i = 0; i < jsonFiles.length; i++) {
const jsonFile = jsonFiles[i];
const sourceObj = JSON.parse(fs.readFileSync(jsonFile, 'utf8'));
this.deleteComment(sourceObj);
this.resultObj = merge(baseObj, sourceObj);
}
} else {
console.error('This file is not found. %j', this.baseFile);
}
}
// export json
exportJson(resultObj) {
const result = JSON.stringify(resultObj, null, 2);
if(!this.output.match(/.*\.(json)$/)) {
this.output = this.output + '.json';
}
fs.writeFileSync(this.output, result);
}
// use all functions to generate merged json
run() {
let isTargetDir;
for (let i = 0; i < this.targetDirArray.length; i++) {
const targetDir = this.targetDirArray[i];
this.getJsonFiles(targetDir);
if (fs.existsSync(targetDir)) {
isTargetDir = true;
}
}
if (this.directories.length) {
for (let i = 0; i < this.directories.length; i++) {
const childDir = this.directories[i];
this.getJsonFiles(childDir);
}
}
if (isTargetDir === true) {
this.mergeJson(this.jsonFiles);
// delete {} and []
this.deleteEmptyPropaty(this.resultObj);
// delete generated null by the aforementioned function
this.deleteArrayNull(this.resultObj);
// delete generated {} and [] by the aforementioned function
this.deleteEmptyPropaty(this.resultObj);
this.exportJson(this.resultObj);
}
}
}