-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-article.js
63 lines (45 loc) · 1.4 KB
/
create-article.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
const util = require('node:util')
const path = require('node:path')
const fs = require('node:fs')
const readline = require('node:readline')
const { runCommand } = require('./src/core/run-command')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const question = util.promisify(rl.question).bind(rl)
async function main() {
const slug = await question('Enter article slug (url)\n')
if (!slug) {
return
}
const categories = (await question('Enter article categories separated by comma\n') || '')
.split(',')
.map(s => s.trim())
const gitUser = (() => {
try {
return runCommand('git config user.name').trim()
} catch (error) {
return ''
}
})()
const authors = (await question(`Enter authors names separated by comma (default: ${gitUser})\n`) || gitUser)
.split(',')
.map(s => s.trim())
const articleFolder = path.join(process.cwd(), 'src', 'data', 'articles', slug)
fs.mkdirSync(articleFolder, {
recursive: true
})
fs.writeFileSync(path.join(articleFolder, 'index.md'), '')
let data = ''
if (categories.length > 0) {
data += `categories:\n${categories.map(c => ` - ${c}`).join('\n')}`
}
data += '\n'
if (authors.length > 0) {
data += `authors:\n${authors.map(c => ` - ${c}`).join('\n')}`
}
fs.writeFileSync(path.join(articleFolder, 'index.data.yml'), data)
rl.close()
}
main()