generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
176 lines (150 loc) · 4.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const core = require('@actions/core');
const exec = require('@actions/exec');
const os = require('os')
const debug = require('debug')('background-server-action');
const { ping } = require('./src/ping')
const isWindows = () => os.platform() === 'win32'
const isUrl = (s) => /^https?:\/\//.test(s)
/**
* Parses input command, finds the tool and
* the runs the command.
*/
const execCommand = (
fullCommand,
waitToFinish = true,
label = 'executing'
) => {
console.log('%s command "%s"', label, fullCommand)
const promise = exec.exec('bash', ['-c', fullCommand], { cwd: core.getInput('cwd') });
if (waitToFinish) {
return promise
}
}
// most @actions toolkit packages have async methods
async function run() {
let command
if (isWindows()) {
// allow custom Windows command command
command =
core.getInput('command-windows') || core.getInput('command')
} else {
command = core.getInput('command')
}
if (!command) {
return
}
// allow commands to be separated using commas or newlines
const separateCommands = command
.split(/,|\n/)
.map((s) => s.trim())
.filter(Boolean)
debug(
`Separated ${
separateCommands.length
} main commands ${separateCommands.join(', ')}`
)
return Promise.all(separateCommands.map(async (command) => {
return execCommand(
command,
true,
`run command "${command}"`
)
}))
}
const buildAppMaybe = () => {
const buildApp = core.getInput('build')
if (!buildApp) {
return Promise.resolve()
}
debug(`building application using "${buildApp}"`)
return execCommand(buildApp, true, 'build app')
}
const startServersMaybe = () => {
let startCommand
if (isWindows()) {
// allow custom Windows start command
startCommand =
core.getInput('start-windows') || core.getInput('start')
} else {
startCommand = core.getInput('start')
}
if (!startCommand) {
debug('No start command found')
return Promise.resolve()
}
// allow commands to be separated using commas or newlines
const separateStartCommands = startCommand
.split(/,|\n/)
.map((s) => s.trim())
.filter(Boolean)
debug(
`Separated ${
separateStartCommands.length
} start commands ${separateStartCommands.join(', ')}`
)
return separateStartCommands.map((startCommand) => {
return execCommand(
startCommand,
false,
`start server "${startCommand}"`
)
})
}
/**
* Pings give URL(s) until the timeout expires.
* @param {string} waitOn A single URL or comma-separated URLs
* @param {Number?} waitOnTimeout in seconds
*/
const waitOnUrl = (waitOn, waitOnTimeout = 60) => {
console.log(
'waiting on "%s" with timeout of %s seconds',
waitOn,
waitOnTimeout
)
const waitTimeoutMs = waitOnTimeout * 1000
const waitUrls = waitOn
.split(',')
.map((s) => s.trim())
.filter(Boolean)
debug(`Waiting for urls ${waitUrls.join(', ')}`)
// run every wait promise after the previous has finished
// to avoid "noise" of debug messages
return waitUrls.reduce((prevPromise, url) => {
return prevPromise.then(() => {
debug(`Waiting for url ${url}`)
return ping(url, waitTimeoutMs)
})
}, Promise.resolve())
}
const waitOnMaybe = () => {
const waitOn = core.getInput('wait-on')
if (!waitOn) {
return Promise.resolve();
}
const waitOnTimeout = core.getInput('wait-on-timeout') || '60'
const timeoutSeconds = parseFloat(waitOnTimeout)
if (isUrl(waitOn)) {
return waitOnUrl(waitOn, timeoutSeconds)
}
console.log('Waiting using command "%s"', waitOn)
return execCommand(waitOn, true)
}
buildAppMaybe()
.then(startServersMaybe)
.then(waitOnMaybe)
.then(run)
.then(() => {
debug('all done, exiting')
// force exit to avoid waiting for child processes,
// like the server we have started
// see https://github.com/actions/toolkit/issues/216
process.exit(0)
})
.catch((error) => {
// final catch - when anything goes wrong, throw an error
// and exit the action with non-zero code
debug(error.message)
debug(error.stack)
core.setFailed(error.message)
process.exit(1)
})