-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
76 lines (67 loc) · 2.03 KB
/
index.ts
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
import * as core from '@actions/core'
import exec from 'actions-exec-wrapper'
const defaultPackages = [
'@actions/core',
'@actions/exec',
'@actions/github',
'actions-exec-listener',
]
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
type PackageManager = 'npm' | 'yarn'
function assertIsPackageManager(manager: string): asserts manager is PackageManager {
if (manager !== 'npm' && manager !== 'yarn') {
throw new Error(`Specified node package manager is ${manager}, neither npm nor yarn.`)
}
}
const installPackages = async (manager: PackageManager, packages: string[]) => {
let command = ''
if (manager === 'npm') {
command = 'npm install'
} else {
command = 'yarn add'
}
if (packages.length < 1) {
console.log('There is no package to install.')
return
}
core.startGroup(`${command} ${packages.join(' ')}`)
await exec.exec(command, packages, {
cwd: __dirname,
})
core.endGroup()
}
const runScript = async (script: string) => {
const args: { [key: string]: any} = {
Buffer,
__dirname,
__filename,
console,
exports,
module,
process,
require,
TextDecoder,
TextEncoder,
URL,
URLSearchParams,
WebAssembly,
}
const scriptFunc = new AsyncFunction(Object.keys(args), script)
return await scriptFunc(...Object.values(args))
}
const main = async () => {
const packageManager = core.getInput('package-manager', { required: true })
assertIsPackageManager(packageManager)
const requiredPackages = core.getInput('required-packages')
.split(/\n|\s/) // Split by space or new line
.filter(pkg => pkg !== '') // Remove empty item
const script = core.getInput('script', { required: true })
console.log(script)
await installPackages(packageManager, [...requiredPackages, ...defaultPackages])
await runScript(script)
}
main().catch(e => {
console.error(e)
core.setFailed(`An error occurred: ${e.message || JSON.stringify(e)}`)
})