This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepo-builder.js
executable file
·90 lines (78 loc) · 2.77 KB
/
repo-builder.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
const fs = require('fs/promises')
const fsO = require('fs')
const path = require('path')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const TMP_PACKAGE_STORE = '/work/tmp'
const REPO_PATH = '/work/repo'
const REPO_NAME = process.env.REPO_NAME
let workingLock = false
const waitingQueue = []
/**
* @param {string} pkgFileName
*/
const addPackage = async (pkgFileName) => {
if (workingLock) {
waitingQueue.push(pkgFileName)
return
}
workingLock = true
try {
// 从文件名中获取架构名称
// [----1名称版本----]-[---2架构---] .pkg .tar .zst/.gz/.xz?
const regexExec = /^([A-Za-z0-9\-._:]+)-([a-z0-9._]+)\.pkg\.tar(\.[a-z0-9]+)?$/.exec(pkgFileName)
const archName = regexExec[2]
let arches = [archName]
if (archName === 'any') {
arches = ['x86_64', 'i686', 'aarch64']
}
for (const arch of arches) {
// 创建架构目录,如果不存在
const archPath = path.join(REPO_PATH, arch)
if (!fsO.existsSync(archPath)) {
await fs.mkdir(archPath)
}
// 执行添加命令
if (fsO.existsSync(path.join(archPath, `${REPO_NAME}.db.tar.gz.lck`)))
await fs.unlink(path.join(archPath, `${REPO_NAME}.db.tar.gz.lck`))
await exec(`repo-add -R ${path.join(archPath, `${REPO_NAME}.db.tar.gz`)} ${path.join(TMP_PACKAGE_STORE, pkgFileName)}`)
}
if (archName === 'any') {
for (const arch of arches) {
await fs.copyFile(path.join(TMP_PACKAGE_STORE, pkgFileName), path.join(REPO_PATH, arch, pkgFileName))
}
await fs.unlink(path.join(TMP_PACKAGE_STORE, pkgFileName))
}
else {
// 将文件移动到架构目录
const dest = path.join(REPO_PATH, archName, pkgFileName)
await fs.rename(path.join(TMP_PACKAGE_STORE, pkgFileName), dest)
}
}
catch (e) {
console.log(e)
}
finally {
workingLock = false
if (waitingQueue.length > 0) {
await addPackage(waitingQueue.shift())
}
}
}
const getQueue = () => {
return waitingQueue
}
/**
* @param {string} pkgFileName
*/
const packageExists = (pkgFileName) => {
// 从文件名中获取架构名称
// [----1名称版本----]-[---2架构---] .pkg .tar .zst/.gz/.xz?
const regexExec = /^([A-Za-z0-9\-._:]+)-([a-z0-9._]+)\.pkg\.tar(\.[a-z0-9]+)?$/.exec(pkgFileName)
let archName = regexExec[2]
if (archName === 'any') {
archName = 'x86_64'
}
return fsO.existsSync(path.join(REPO_PATH, archName, pkgFileName))
}
module.exports = {addPackage, getQueue, packageExists}