-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.js
101 lines (86 loc) · 3.11 KB
/
write.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
// write.js
/*
* command --url=="https://my.url.com" --name=="keyForFileName" --dir=="directory --body=="keyForBodyText" --remove=="key1,key2,key3" ==title==true
* args
* {
* url: 'json url/path',
* name: 'JSON key for filename', - key to identify what value should be used for the filename (filename is 'cleaned' for spaces, punctuation, etc.)
* body: 'key for body text' - key to identify content that should make up the body (the content below the frontmatter)
* dir: 'directory to make'
* remove: 'key1,key2,key3' - remove specific keys from the object before writing to frontmatter (remove cruft)
* title: true - set frontmatter title from name argument
* }
*
* Note: args use '==' due to potential for urls to contain a single "="
*/
const fs = require('fs');
const yaml = require('js-yaml');
const axios = require('axios')
function getArgs () {
const args = {};
process.argv
.slice(2, process.argv.length)
.forEach( arg => {
// long arg
if (arg.slice(0,2) === '--') {
const longArg = arg.split('==');
const longArgFlag = longArg[0].slice(2,longArg[0].length);
const longArgValue = longArg.length > 1 ? longArg[1] : true;
args[longArgFlag] = longArgValue;
}
// flags
else if (arg[0] === '-') {
const flags = arg.slice(1,arg.length).split('');
flags.forEach(flag => {
args[flag] = true;
});
}
});
return args;
}
const args = getArgs();
// clean name
function cleanName(name) {
if(name){
let newname = name.replace(/[^a-zA-Z0-9]+/g,'-').replace(/[-]+/g,'-').toLowerCase();
let newername = newname.replace(/\-$/, "");
return newername;
} else {
return false;
}
}
// make dir
fs.mkdirSync(args.dir, { recursive: true })
// fetch JSON
async function fetchJSON(url) {
const response = await axios.get(url)
let results = response.data;
const removeArray = (args.remove).split(',');
// if api returns object instead of array of objects when there is only a single result we have to create an array of the single object to use `results.forEach()`
if(results.length == undefined){
let tempArray = [];
tempArray.push(results);
results = tempArray;
}
results.forEach(result => {
let name = result[args.name];
let cleaned = cleanName(name);
let body = result[args.body];
// remove args.remove keys from object
removeArray.forEach(key =>
delete result[key]
);
// create yaml/frontmatter
let yamlStr = yaml.dump(result);
let frontMatter;
if(args.title == "true"){
frontMatter = '---\n' + 'title: ' + name + '\n' + yamlStr + '\n---\n' + body;
} else {
frontMatter = '---\n' + yamlStr + '\n---\n' + body;
}
fs.writeFileSync(args.dir + '/' + cleaned + '.md', frontMatter, 'utf8');
console.log(cleaned + ".md created");
});
}
// time to make the YAML
fetchJSON(args.url);