-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
executable file
·72 lines (66 loc) · 1.83 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
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
const fs = require('fs')
const merge = require('lodash.merge')
const path = require('path')
const program = require('commander')
const config = require('./config')
const translateMarkdown = require('./index')
program
.command('set')
.description('set config for cli')
.option('-K --key [key]', 'TRANSLATOR_TEXT_KEY')
.option('-R --region [region]', 'TRANSLATOR_REGION')
.action(function ({ key, region }) {
let newConfig = merge(config, {
key,
region,
})
fs.writeFileSync(
path.resolve(__dirname, './config.json'),
JSON.stringify(newConfig)
)
})
program
.command('get <key>')
.description('get value of typical key from config')
.action(function (key) {
console.log(config[key] || '')
})
program
.command('translate')
.description('translate markdown file')
.option('-S, --src [src]', 'src file')
.option('-D, --dest [dest]', 'dest file')
.option('-F, --from [from]', 'src lang')
.option('-R, --region [region]', 'Azure region')
.option('-T, --to [to]', 'dest lang')
.option('-K, --key [key]', 'TRANSLATOR_TEXT_KEY')
.action(function ({ src, dest, from, region, to, key }) {
const srcPath = path.resolve(process.cwd(), src)
const destPath = path.resolve(process.cwd(), dest)
const subscriptionKey = key || config.key
const serviceRegion = region || config.region
translateMarkdown({
src: srcPath,
from,
to,
subscriptionKey,
region: serviceRegion,
})
.then((data) => {
const writeStream = fs.createWriteStream(destPath)
writeStream.write(data, (err) => {
if (err) {
throw err
}
process.exit(0)
})
})
.catch((err) => {
throw err
})
})
program.command('*').action(() => {
program.help()
})
program.parse(process.argv)