-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (201 loc) · 5.8 KB
/
index.js
File metadata and controls
208 lines (201 loc) · 5.8 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const argv = require('yargs')
.usage('Usage: node index.js --inputDir <path> --outputDir <path> --verbose')
.demand(['i', 'o'])
.alias('i', 'inputDir')
.describe('i', 'The directory path to source XML files from')
.alias('o', 'outputDir')
.describe('o', 'The directory path to merge XML files to')
.alias('v', 'verbose')
.describe('v', 'Whether to show verbose output')
.help('h')
.alias('h', 'help')
.epilog('Copyright Charles Hulcher 2016')
.argv;
const merge = require('lodash.mergewith');
const fs = require('fs-extra');
const path = require('path');
const pathExists = require('path-exists');
const through2 = require('through2');
const convert = require('xml-js');
// logs stuff
function log(...args) {
if (argv.v) {
console.log(...args);
}
}
// gets the JS representation of an XML tree from a file
function getXMLasObj(filePath) {
return new Promise((resolve, reject) => {
pathExists(filePath).then(exists => {
if (exists) {
fs.readFile(filePath, (err, data) => {
if (err) {
reject(err);
}
else {
resolve(JSON.parse(convert.xml2json(data, { compact: true })));
}
});
}
else {
resolve({});
}
});
});
}
// takes in array of files to merge XML contents
function processFiles(inputFiles = [], outputFiles = [], inputDir = '.', outputDir = './out') {
return new Promise((resolve, reject) => {
if (inputFiles.length) {
const proms = inputFiles.map(file => {
return new Promise((res, rej) => {
log(`${outputFiles.indexOf(file) > -1 ? 'Merging' : 'Copying'} ${file}`);
const inputFilePath = path.join(inputDir, file);
const outputFilePath = path.join(outputDir, file);
Promise.all([getXMLasObj(inputFilePath), getXMLasObj(outputFilePath)])
.then(results => {
const [
inputObj,
outputObj,
] = results;
const mergedObj = merge({}, outputObj, inputObj, (dest, src, key) => {
return key === '_comment' ? `${src}\n${dest || ''}` : undefined;
});
const xmlString = convert.json2xml(JSON.stringify(mergedObj), { compact: true, spaces: 2 });
// if (outputFilePath.indexOf('values/dimens.xml') > -1) {
// // console.log(JSON.stringify(mergedObj, null, 2));
// log(xmlString);
//
// // console.log(JSON.stringify(inputObj, null, 2));
// }
// res();
fs.outputFile(outputFilePath, xmlString, err => {
if (err) {
rej(err);
}
else {
res();
}
});
}, rej);
});
});
Promise.all(proms).then(resolve, reject);
}
else {
reject('No input files to process.');
}
});
}
function getXMLFilesInDir(dir) {
return new Promise((resolve, reject) => {
pathExists(dir).then(exists => {
if (exists) {
const items = [];
const onlyXMLFilter = through2.obj(function filter(item, enc, next) { // no arrow!!
if (item.path.indexOf('.xml') === item.path.length - 4) {
this.push(item);
}
next();
});
fs.walk(dir)
.pipe(onlyXMLFilter)
.on('data', item => {
items.push(item.path.slice(item.path.indexOf(dir) + dir.length));
})
.on('end', () => {
resolve(items);
});
}
else {
reject(`Cannot find ${dir} from ${process.cwd()}`);
}
});
});
}
function startMerge() {
const {
inputDir,
outputDir,
} = argv;
if (inputDir && outputDir) {
Promise.all([getXMLFilesInDir(inputDir), getXMLFilesInDir(outputDir)])
.then((results) => {
const [
inputXmlFiles,
outputXmlFiles,
] = results;
processFiles(inputXmlFiles, outputXmlFiles, inputDir, outputDir)
.then(() => {
log('Operation complete.');
}, log);
}, log);
}
}
function getAllFilesInDir(dir) {
return new Promise((resolve, reject) => {
pathExists(dir).then(exists => {
if (exists) {
const items = [];
const noDirs = through2.obj(function filter(item, enc, next) { // no arrow!!
if (!item.stats.isDirectory()) {
this.push(item);
}
next();
});
fs.walk(dir)
.pipe(noDirs)
.on('data', item => {
items.push(item.path.slice(item.path.indexOf(dir) + dir.length));
})
.on('end', () => {
resolve(items);
});
}
else {
reject(`Cannot find ${dir} from ${process.cwd()}`);
}
});
});
}
function list() {
const {
inputDir,
outputDir,
} = argv;
if (inputDir && outputDir) {
Promise.all([getAllFilesInDir(inputDir)]) // , getAllFilesInDir(outputDir)])
.then((results) => {
const [
inputFiles,
// outputFiles,
] = results;
const proms = inputFiles.map(file => {
return new Promise((resolve, reject) => {
pathExists(path.join(outputDir, file)).then(exists => {
if (!exists) {
fs.copy(path.join(inputDir, file), path.join(outputDir, file), (err) => {
if (err) {
reject(err);
}
else {
console.log(`Copied ${file}`);
resolve();
}
});
}
else {
resolve();
}
});
});
});
Promise.all(proms).then(() => {
console.log('COmplete');
}, (err) => {
console.error(err);
});
});
}
}
list();