-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
95 lines (90 loc) · 4.5 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
const chalk = require('chalk')
const convict = require('convict')
const lib = require('./lib')
const fs = require('fs')
const yaml = require('yaml')
const beautyError = require('beauty-error')
const commandLineUsage = require('command-line-usage')
let cfg
let schema
try {
const schemaFile = fs.readFileSync('./action.yml', 'utf8')
schema = yaml.parse(schemaFile)
cfg = convict(schema.inputs)
cfg.validate()
} catch (e) {
console.error(beautyError(e))
process.exitCode = 1
}
if (cfg.get('api-key') === '' || cfg.get('help')) {
const header = `░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░██████╗██╗████████╗███████╗██╗░░░░░███████╗░█████╗░███████╗
░██╔════╝██║╚══██╔══╝██╔════╝██║░░░░░██╔════╝██╔══██╗██╔════╝
░╚█████╗░██║░░░██║░░░█████╗░░██║░░░░░█████╗░░███████║█████╗░░
░░╚═══██╗██║░░░██║░░░██╔══╝░░██║░░░░░██╔══╝░░██╔══██║██╔══╝░░
░██████╔╝██║░░░██║░░░███████╗███████╗███████╗██║░░██║██║░░░░░
░╚═════╝░╚═╝░░░╚═╝░░░╚══════╝╚══════╝╚══════╝╚═╝░░╚═╝╚═╝░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░██╗░░░██╗██████╗░██████╗░░█████╗░████████╗███████╗██████╗░░
░░██║░░░██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗░
░░██║░░░██║██████╔╝██║░░██║███████║░░░██║░░░█████╗░░██████╔╝░
░░██║░░░██║██╔═══╝░██║░░██║██╔══██║░░░██║░░░██╔══╝░░██╔══██╗░
░░╚██████╔╝██║░░░░░██████╔╝██║░░██║░░░██║░░░███████╗██║░░██║░
░░░╚═════╝░╚═╝░░░░░╚═════╝░╚═╝░░╚═╝░░░╚═╝░░░╚══════╝╚═╝░░╚═╝░`
const sections = [
{
header: chalk.bgYellow(chalk.dim(chalk.greenBright(header))),
raw: true
},
{
header: 'siteleaf updater',
content: schema.description
},
{
header: 'Synopsis',
content: [
'$ siteleaf-updater --api-key "secret-key" --api-secret "secret" --site "siteid"'
]
},
{
header: 'Options',
optionList: Object.entries(schema.inputs)
.map(([key, val]) => {
const opt = {
name: key,
description: String(val.description),
type: String,
typeLabel: 'string'
}
if (val.format === 'Boolean') {
opt.type = Boolean
opt.typeLabel = ''
}
switch (key) {
case 'file': { opt.typeLabel = 'file' }
}
return opt
})
}
]
const usage = commandLineUsage(sections)
console.log(usage)
if (!cfg.get('help')) {
process.exitCode = 1
}
} else {
lib(cfg.get('api-key'), cfg.get('api-secret'), cfg.get('site'), cfg.get('page'),
cfg.get('file'), cfg.get('publish'))
.then(name => {
if (cfg.get('publish')) {
console.info(`Page "${name}" was successfully updated and published`)
} else {
console.info(`Page "${name}" was successfully updated`)
}
})
.catch(err => {
console.error(beautyError(err))
process.exitCode = 1
})
}