-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·59 lines (50 loc) · 1.38 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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const { resolve } = require('path');
const read = require('node-read-yaml');
const { FAILSAFE_SCHEMA } = require('node-read-yaml');
const { saveAll } = require('dev-blog-directory-save');
const newFilename = 'new.yml';
const templateFilename = '.new.yml';
function main(filepath, reset, options) {
return read(filepath, { multi: true, schema: FAILSAFE_SCHEMA })
.then((docs) => docs.filter((doc) => doc && typeof doc === 'object'))
.then((docs) => saveAll(docs, options))
.then(() => {
if (reset === true) {
fs.copyFileSync(templateFilename, filepath);
}
});
}
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']
});
let filename = argv._[0];
const options = { merge: Boolean(argv.merge) };
let reset = false;
if (!filename) {
filename = newFilename;
reset = true;
}
const filepath = resolve(process.cwd(), filename);
console.log('yaml file name: ' + filename);
console.log('yaml full path: ' + filepath);
main(filepath, reset, options)
.then(() => {
console.log('Done!');
})
.catch(exit);
} else {
// Required as a module
module.exports = main;
}