-
Notifications
You must be signed in to change notification settings - Fork 23
/
insert.js
82 lines (75 loc) · 2.04 KB
/
insert.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
const { promises: { writeFile, readFile } } = require('fs')
const { spawn } = require('child_process')
readFile('pending.txt')
.then(String)
.then(pending => pending
.split('\n')
.map(line => line.trim())
.filter(Boolean)
.map(line => {
if (!line.includes(':') && line !== 'w') {
return `a:${line}`
} else {
return line
}
})
.map(line => line.split(':')
.map(param => param.trim())
.filter(Boolean))
.map(([cmd, ...params]) => [cmd, params.join(':')
.split(' ')
.map(param => param.trim())
.filter(Boolean),
])
.reduce(async (p, [cmd, params]) => {
let { vtb = {}, filename } = await p || {}
if (cmd === 'w') {
await writeFile(`vtbs/${filename}`, JSON.stringify(vtb, undefined, 2))
console.log(filename)
return undefined
}
if (cmd === 'n') {
if (params.length === 1) {
params.unshift('cn')
}
const [language, name] = params
if (!filename) {
filename = `${name}.json`
}
if (!vtb.name) {
vtb.name = {}
}
vtb.name[language] = name
}
if (cmd === 'g') {
const group = params.join(' ')
vtb.group = group
}
if (cmd === 't') {
const [type] = params
vtb.type = type
}
if (cmd === 'a') {
const [platform, ...ids] = params
ids.forEach(id => {
if (!vtb.accounts) {
vtb.accounts = {}
}
if (!vtb.accounts[platform]) {
vtb.accounts[platform] = id
} else {
if (typeof vtb.accounts[platform] === 'string') {
vtb.accounts[platform] = [vtb.accounts[platform]]
}
vtb.accounts[platform].push(id)
}
})
}
if (cmd === 'c') {
const [username] = params
await spawn('all-contributors', ['add', username, 'content'])
console.log(username)
}
return { vtb, filename }
}, Promise.resolve())
)