-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (54 loc) · 2.35 KB
/
index.js
File metadata and controls
60 lines (54 loc) · 2.35 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
// noinspection JSCheckFunctionSignatures,JSUnresolvedFunction
import AdmZip from 'adm-zip'
import path from 'path'
import fs from 'fs'
import gitRemoteOriginUrl from 'remote-origin-url'
import parseGithubUrl from 'parse-github-url'
import gitBranch from 'git-branch'
import gitLog from 'gitlog'
const patchPath = process.argv[2]
async function run() {
if (patchPath == null) console.log('Please provide a path.')
else {
const list = []
const files = fs.readdirSync(patchPath).filter(file => file.endsWith('.zip'))
for (const patch of files) {
const zip = new AdmZip(path.join(patchPath, patch))
const metaFile = zip.getEntry('patch.meta')
const gitUrl = parseGithubUrl(gitRemoteOriginUrl.sync())
gitUrl.branch = gitBranch.sync()
const meta = {
url: `https://github.com/${gitUrl.repo}/raw/${gitUrl.branch}/${patchPath}/${patch}`,
author: 'Rboard Theme Patcher',
tags: []
}
meta.size = fs.statSync(path.join(patchPath, patch)).size
await new Promise((res) => {
gitLog.default({
repo: process.cwd(),
file: path.join(patchPath, patch),
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 = patch.replace('_', ' ').replace('.zip', '')
list.push(meta)
console.log(`${meta.name} by ${meta.author} added.`)
}
fs.writeFileSync('patches.json', JSON.stringify(list, null, 2))
}
}
run().then(() => console.log('Done.'))