-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
112 lines (98 loc) · 2.72 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('zx/globals')
const { readFileSync, createWriteStream, writeFileSync } = require('fs')
const { join } = require('path')
const yargs = require('yargs')
const workDir = join(process.cwd(), './dist')
const tmpDir = join(process.cwd(), './tmp')
/**
* @type any
*/
const argv = yargs.argv
const endpoint = 'https://api.github.com'
const token =
argv.GH_TOKEN ||
argv.ghToken ||
process.env.GH_TOKEN ||
(() => {
try {
return readFileSync('./gh_token', { encoding: 'utf-8' }).split('\n')[0]
} catch {
return null
}
})()
async function fetchAsset() {
const owner = argv.owner || 'Innei'
const repo = argv.repo || 'mx-actions'
/**
* @type {number}
*/
const workflowId = await (async () => {
const res = await fetch(
endpoint + `/repos/${owner}/${repo}/actions/workflows`,
)
const data = await res.json()
const workflow = data.workflows.find(
(w) => w.name === '[Kami] Build & Deploy',
)
return workflow?.id
})()
const runId = await (async () => {
const search = new URLSearchParams()
search.append('per_page', '1')
const res = await fetch(
endpoint +
`/repos/${owner}/${repo}/actions/workflows/${workflowId}/runs` +
'?' +
search.toString(),
)
const { workflow_runs } = await res.json()
const runId = workflow_runs[0].id
return runId
})()
const artifactUrl = await (async function () {
const res = await fetch(
endpoint + `/repos/${owner}/${repo}/actions/runs/${runId}/artifacts`,
)
const data = await res.json()
const { artifacts, total_count } = data
return artifacts[0].archive_download_url
})()
$.verbose = false
const fileRes = await fetch(artifactUrl, {
headers: {
Authorization: 'token ' + token,
},
})
$.verbose = true
const buffer = await fileRes.buffer()
writeFileSync(join(tmpDir, 'kami.zip'), buffer)
}
async function pull() {
cd(join(process.cwd(), './run'))
if (fs.existsSync(join(process.cwd(), 'run/kami'))) {
await $`cd kami && git pull`
} else {
await $`git clone https://github.com/mx-space/kami.git --depth 1`
}
await $`cd kami && pnpm i`
}
async function bootsharp() {
await $`mkdir -p dist`
await $`mkdir -p run`
await $`mkdir -p tmp`
await pull()
await fetchAsset()
cd(tmpDir)
await $`rm -rf ${join(process.cwd(), 'run/kami/.next')}`
await $`unzip kami.zip -d ../run/kami/.next`
cd(process.cwd())
cd('./run/kami')
await $`rm .env && touch .env`
// 写入 ENV
await $`echo "NETEASE_PHONE=${process.env.NETEASE_PHONE}" >> .env`
await $`echo "NETEASE_PASSWORD=${process.env.NETEASE_PASSWORD}" >> .env`
await $`pm2 reload ecosystem.config.js`
cd(process.cwd())
await $`rm -rf tmp`
}
bootsharp()