-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from juergenhoetzel/githubscript
Use javascript-action instead of platform dependent Shell commands
- Loading branch information
Showing
2 changed files
with
80 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
const {sep, join, basename} = require('path'); | ||
const suffix = (os.type() === 'Windows_NT') ? '.bat' : ''; | ||
|
||
// Get raw download URL for path in this repo or pull-request repo | ||
// (depending on context) automatically adds (.bat) extension to path | ||
// if running on windows to make Github actions steps platform agnostic | ||
function getRawUrl(context, path) { | ||
const {repository, pull_request} = context.payload; | ||
const branch = pull_request ? pull_request.head.ref : repository.default_branch; | ||
const repo_full_name = pull_request ? pull_request.head.repo.full_name : repository.full_name; | ||
return `https://raw.github.com/${repo_full_name}/${branch}/${path}${suffix}`; | ||
} | ||
|
||
// Download script and make it executable (required on Unix) | ||
async function downloadScript(github, context, file, outdir) { | ||
const url = getRawUrl(context, file); | ||
const response = await github.request(url); | ||
const out = join(outdir, basename(file) + suffix); | ||
await fs.promises.writeFile(out, response.data); | ||
console.log(`Downloaded ${url} to ${out}`); | ||
if (os.type() !== 'Windows_NT') { | ||
fs.chmodSync(out, fs.constants.S_IXUSR | fs.constants.S_IRUSR); | ||
} | ||
return out; | ||
} | ||
|
||
// simple tilde expansion | ||
function expandTilde(path) { | ||
return path.replace('~', (os.type() === 'Windows_NT') ? `${process.env.USERPROFILE}${sep}` : `${process.env.HOME}${sep}`); | ||
} | ||
|
||
module.exports = { | ||
downloadScript, | ||
expandTilde | ||
}; |