forked from mathieudutour/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action-injections.js
52 lines (47 loc) · 1.54 KB
/
action-injections.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
/*
* Dependency injection for scripts that call .github/actions/ code
* Replaces action platform specific functionality with local machine functionality
*/
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import github from './github.js'
// Directs core logging to console
export function getCoreInject(debug) {
return {
info: console.log,
debug: (message) => (debug ? console.warn(chalk.blue(message)) : {}),
warning: (message) => console.warn(chalk.yellow(message)),
error: console.error,
setOutput: (name, value) => {
if (debug) {
console.log(`Output "${name}" set to: "${value}"`)
}
},
setFailed: (message) => {
if (debug) {
console.log('setFailed called.')
}
throw new Error(message)
},
}
}
// Writes strings that would be uploaded as artifacts to a local logs/ directory
const cwd = new URL('', import.meta.url).pathname
const logsPath = path.join(cwd, '..', '..', 'logs')
if (!fs.existsSync(logsPath)) {
fs.mkdirSync(logsPath)
}
export function getUploadArtifactInject(debug) {
return (name, contents) => {
const logFilename = path.join(logsPath, `${new Date().toISOString().substr(0, 16)}-${name}`)
if (debug) {
fs.writeFileSync(logFilename, contents)
console.log(`${name} artifact upload written to ${logFilename}`)
} else {
console.log(`Debug not enabled. ${name} artifact NOT written to ${logFilename}`)
}
}
}
// Uses local process.env GITHUB_TOKEN to create an octokit instance
export const octokitInject = github()