-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (75 loc) · 3.36 KB
/
index.js
File metadata and controls
87 lines (75 loc) · 3.36 KB
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
// noinspection JSCheckFunctionSignatures,JSUnresolvedFunction
const protobuf = require('protobufjs')
const AdmZip = require('adm-zip')
const crypto = require('crypto')
const path = require('path')
const fs = require('fs')
const gitRemoteOriginUrl = require('remote-origin-url')
const parseGithubUrl = require('parse-github-url')
const gitBranch = require('git-branch')
const gitLog = require('gitlog').default
const packPath = process.argv[2]
async function run() {
if (path == null) console.log("Please provide a path.")
else {
const list = []
const files = fs.readdirSync(packPath).filter(file => file.endsWith(".zip") || file.endsWith(".pack"))
for (const pack of files) {
const zip = new AdmZip(path.join(packPath, pack))
const metaFile = zip.getEntry('pack.meta')
const gitUrl = parseGithubUrl(gitRemoteOriginUrl.sync())
gitUrl.branch = gitBranch.sync()
const fileBuffer = fs.readFileSync(path.join(packPath, pack))
const hashSum = crypto.createHash('sha256')
hashSum.update(fileBuffer)
const meta = {
url: `${packPath}/${pack}`,
hash: hashSum.digest('hex'),
author: 'Rboard Script',
tags: []
}
meta.themes = zip.getEntries().filter(entry => entry.name.endsWith(".zip")).map(entry => entry.name.replace(".zip", ""))
meta.size = fs.statSync(path.join(packPath, pack)).size
await new Promise((res) => {
gitLog({
repo: process.cwd(),
file: path.join(packPath, pack),
fields: ["hash", "authorName", "authorDate"]
}, (error, commits) => {
const commit = commits[0]
if (commit) {
meta.date = new Date(commit.authorDate).getTime()
meta.author = commit.authorName
}
res()
})
})
if (metaFile != null) {
const tmp = metaFile.getData().toString()
tmp.split(new RegExp('(\r\n|\n)')).forEach(metaEntry => {
if (metaEntry.includes('=')) {
meta[metaEntry.split('=')[0]] = (metaEntry.includes(',') || metaEntry.startsWith("tags")) ? metaEntry.split('=')[1].split(',') : metaEntry.split('=')[1]
}
})
} else {
meta.name = pack.replace('_', ' ').replace(new RegExp('\.(zip|pack)'), '')
}
list.push(meta)
console.log(`${meta.name} by ${meta.author} added.`)
}
fs.writeFileSync('list.json', JSON.stringify(list, null, 2))
await new Promise((res, rej) => protobuf.load('proto/list.proto', (err, root) => {
if (err) return rej(err)
const ObjectList = root.lookupType('rboard.ObjectList')
const errMsg = ObjectList.verify({ objects: list })
if (errMsg) return rej(errMsg)
const message = ObjectList.fromObject({ objects: list })
const buffer = ObjectList.encode(message).finish()
fs.writeFile('proto/list.pb', buffer, err => {
if (err) return rej(err)
res()
})
}))
}
}
run().then(() => console.log('Done.'))