-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaction.mjs
More file actions
77 lines (71 loc) · 2.27 KB
/
action.mjs
File metadata and controls
77 lines (71 loc) · 2.27 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
// @ts-check
import path from 'node:path'
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
function withRuntimeNodePath() {
const runtimeBinPath = path.dirname(process.execPath)
const pathDelimiter = path.delimiter
const currentPath = process.env['PATH'] || ''
const pathEntries = currentPath.split(pathDelimiter).filter(Boolean)
const sanitizedEntries = pathEntries.filter((entry) => entry !== runtimeBinPath)
return [runtimeBinPath, ...sanitizedEntries].join(pathDelimiter)
}
function getRuntimeCommand(command) {
const extension = process.platform === 'win32' ? '.cmd' : ''
return path.join(path.dirname(process.execPath), `${command}${extension}`)
}
// Duplicate code from action/repository, keep this until
// found a better way to include typescript without transpiles
function runCommand(
/** @type {string[]} */ commands,
/** @type {string} */ cwd
) {
return spawnSync(commands[0], commands.slice(1), {
stdio: 'inherit',
cwd,
env: {
...process.env,
PATH: withRuntimeNodePath()
}
})
}
function isCommandFailed(result) {
return Boolean(result.error || result.signal || result.status !== 0)
}
function getGithubActionPath() {
return path.dirname(fileURLToPath(import.meta.url))
}
// Main
console.log('Action: ', process.env['GITHUB_ACTION'])
if (
process.env['GITHUB_ACTION'] === 'llunfeeds' ||
process.env['GITHUB_ACTION'] === '__llun_feeds'
) {
const actionPath = getGithubActionPath()
const nodeVersionResult = runCommand([process.execPath, '--version'], actionPath)
if (isCommandFailed(nodeVersionResult)) {
throw new Error('Fail to check node version')
}
const corepackCommand = getRuntimeCommand('corepack')
const enableCorepackResult = runCommand(
[corepackCommand, 'enable'],
actionPath
)
if (isCommandFailed(enableCorepackResult)) {
throw new Error('Fail to enable corepack')
}
const dependenciesResult = runCommand(
[corepackCommand, 'yarn', 'install'],
actionPath
)
if (isCommandFailed(dependenciesResult)) {
throw new Error('Fail to run setup')
}
const executeResult = runCommand(
[process.execPath, '--import', 'tsx', 'index.ts'],
actionPath
)
if (isCommandFailed(executeResult)) {
throw new Error('Fail to site builder')
}
}