-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·63 lines (52 loc) · 1.86 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const program = require('commander');
const debug = require('debug')('githubIssue2Blog');
const pkg = require('./package.json')
const issue2Hexo = require('./');
const defaultTemplateFile = path.join(__dirname, './template.md');
program
.version(pkg.version)
.description(pkg.description)
.command('init [filname]', 'init your custom template file')
.action((action, arg) => {
if (action === 'init') {
const file = path.resolve(arg);
fs.writeFileSync(file, fs.readFileSync(defaultTemplateFile));
console.log(`Write template file to ${file}`);
process.exit(0);
}
if (action === 'help') {
console.log(`Example: github-issue-to-hexo init your-template.md`);
}
process.exit(0);
})
.option('-u, --user [user]', 'User name')
.option('-r, --repo [repo]', 'Repo name')
.option('--since [since]', 'Issues updated at or after this time are returned. Direct pass to moment')
.option('--allowOtherUsers', 'Allow converting issues created by other users', Boolean, false)
.option('-d, --dir [dir]', 'Output dir, default: "source/_posts/"', String, 'source/_posts/')
.option('-t, --template [filePath]', 'Template file path')
.parse(process.argv);
debug(`Args: user ${program.user}, repo ${program.repo}`);
issue2Hexo.getIssues(program)
.then((posts) => {
const dir = path.resolve(program.dir);
let templateFile = defaultTemplateFile;
if (program.template) {
templateFile = path.resolve(program.template);
}
console.log(`Using template ${templateFile}`);
debug(`Write to dir ${dir}`);
return issue2Hexo.writeFiles(dir, issue2Hexo.renderMDFiles(templateFile, posts));
})
.then(() => {
console.log('Done');
process.exit(0);
})
.catch(e => {
console.log(e.stack);
process.exit(1);
})