-
Notifications
You must be signed in to change notification settings - Fork 5
/
command.js
executable file
·49 lines (39 loc) · 1.37 KB
/
command.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
#!/usr/bin/env node
"use strict";
const yargs = require('yargs');
const getlog = require('./lib/getlog');
const convlog = require('./lib/convlog');
const argv = yargs
.usage('Usage: $0 [--title=title] [--xml] <logid>')
.option('title', { alias: 't', description: 'Title (optional)'})
.option('xml', { alias: 'x', boolean: true, description: 'No conversion' })
.demandCommand(1)
.argv;
const multi = argv._.length > 1;
let paipu = [];
function convlogs() {
if (! argv._.length) {
process.stdout.write(
argv.xml ? paipu.join('\n')
: multi ? JSON.stringify(paipu)
: JSON.stringify(paipu[0]));
process.exit(0);
}
let arg = argv._.shift();
let [ , id, title ] = (''+arg).match(/^(.*?)(:.*)?$/);
title = title ? title
: argv.title && multi ? `:${argv.title}(${paipu.length + 1})`
: argv.title ? `:${argv.title}`
: id;
getlog(id)
.then(xml=>{
paipu.push(argv.xml ? xml : convlog(xml, title));
setTimeout(convlogs, 0);
})
.catch((e)=>{
if (e == 404) console.error(`${id}: not found.`);
else console.error(`${id}: ${e.message}`);
process.exit(-1);
});
}
convlogs();