-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyubisaki.js
179 lines (156 loc) · 5.27 KB
/
yubisaki.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
const commander = require('commander')
const path = require('path')
const chalk = require('chalk')
const figlet = require('figlet');
const semver = require('semver')
const { spawnSync } = require('child_process')
const execSync = require('child_process').execSync
const fs = require('fs')
const YAML = require('yamljs')
const ora = require('ora')
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
console.log(
chalk.blue.bold(
figlet.textSync('yubisaki-shell', { horizontalLayout: 'full' })
)
)
const packageJson = require('./package.json')
let action
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<yubisaki action>')
.usage(`${chalk.green('<yubisaki action>')}`)
.action(name => {
action = name
})
.option('-u --user <user>', 'which github account to deploy')
.option('-p --path <path>', 'the path')
.option('-t --page <page>', 'the article page')
.option('-r --remote <repository>', 'the repo address')
.allowUnknownOption()
.on('--help', () => {
console.log(`${chalk.cyan('yubisaki')} ${chalk.green('deploy --user <github user> --path <vuepress docs path> --remote <github repository address>')}`)
console.log()
console.log(`${chalk.cyan('yubisaki')} ${chalk.green('post --path <article path>')}`)
})
.parse(process.argv)
if(typeof action === 'undefined') {
console.log(`${chalk.cyan('yubisaki')} ${chalk.green('your action')}`)
console.log()
console.log('For example:')
console.log(`${chalk.cyan('yubisaki')} ${chalk.green('deploy <user>')}`)
console.log()
console.log(
`Run ${chalk.cyan(`${'yubisaki'} --help`)} to see all options.`
)
process.exit(1)
}
if(action === 'post') {
if(typeof program.path === 'undefined') {
console.log(`${chalk.cyan('please specify the article path')}`)
process.exit(1)
}
if(typeof program.page === 'undefined') {
console.log(`${chalk.cyan('please specify the article page')}`)
process.exit(1)
}
const spinner = ora('')
const filePath = path.resolve(program.path)
const frontmatter = normalizeYaml()
const str = `---\n${frontmatter}---`
const success = mkdirsSync(filePath)
if(success) {
fs.writeFileSync(
path.resolve(program.path, program.page),
str
)
spinner.succeed(`创建 ${path.join(program.path, program.page)} 成功`)
}else {
spinner.fail('创建 article 失败')
process.exit(1)
}
}
function normalizeYaml() {
const date = new Date()
const ymlPath = path.resolve(__dirname, 'frontmatter.yml')
let matterObject = YAML.load(ymlPath)
const result = Object.assign({}, matterObject, {
date: date.Format('yyyy-MM-dd hh:mm:ss').replace(/\-/g, '/')
})
return YAML.stringify(result, 4, 2)
}
function mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
if(action === 'deploy') {
if(typeof program.user === 'undefined') {
console.log(`${chalk.cyan('please specify the github username')}`)
process.exit(1)
}
if(typeof program.path === 'undefined') {
console.log(`${chalk.cyan('please specify the docs path')}`)
process.exit(1)
}
if(typeof program.remote === 'undefined') {
console.log(`${chalk.cyan('please specify the deploy address')}`)
process.exit(1)
}
const distPath = path.resolve(program.path, '.vuepress/dist')
const spinner = ora('')
const out = build(program.path)
if(out) {
spinner.fail('vuepress build failed')
}else {
deploy(distPath, program.remote, program.user)
}
}
function build(path) {
const { stdout, stderr } = spawnSync('vuepress', ['build', path], { stdio: 'inherit' })
return stderr
}
function deploy(path, repo, user) {
try {
const spinner = ora('')
spinner.start()
console.log()
execSync(`git init ${path}`)
execSync(`cd ${path} && git add -A && git commit -m 'deploy'`)
const args = [
'push',
'-f',
`[email protected]:${user}/${repo}.git`,
'master'
]
const { stderr } = spawnSync('git', args, { stdio: 'inherit', cwd: path })
if(!stderr) {
spinner.succeed('deploy successfully')
}else {
spinner.fail('deploy failed')
}
} catch (e) {
console.log(`${chalk.cyan('git deploy failed')}`)
process.exit(1)
}
}