-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·57 lines (46 loc) · 1.18 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
#!/usr/bin/env node
'use strict';
const { resolve } = require('path');
const { readJson } = require('fs-extra');
const { save, saveAll } = require('dev-blog-directory-save');
function main(filepath, options) {
return readJson(filepath).then((docs) => {
if (!docs) {
return;
}
if (Array.isArray(docs)) {
docs = docs.filter((doc) => doc && typeof doc === 'object');
return saveAll(docs, options);
}
return save(docs, options);
});
}
function exit(message) {
if (message) {
console.error('\n' + message);
process.exit(1);
}
process.exit(0);
}
if (require.main === module) {
// Called directly
const argv = require('minimist')(process.argv.slice(2), {
boolean: ['merge']
});
const filename = argv._[0];
const option = { merge: Boolean(argv.merge) };
if (!filename) {
exit('Usage: save-blog-json [--merge] JSONFILE');
}
const filepath = resolve(process.cwd(), filename);
console.log('json file name: ' + filename);
console.log('json full path: ' + filepath);
main(filepath, option)
.then(() => {
console.log('Done!');
})
.catch(exit);
} else {
// Required as a module
module.exports = main;
}