feat: add exec-powershell package

This commit is contained in:
Amin Yahyaabadi 2022-08-07 16:59:24 -07:00
parent 2b1515ae6b
commit 46635fc18e
13 changed files with 203 additions and 92 deletions

5
.gitignore vendored
View File

@ -11,9 +11,8 @@ temp-*
*.tsbuildinfo *.tsbuildinfo
# Build directories # Build directories
./dist/ dist/
!./dist/setup_cpp.js !./dist/
!./dist/setup_cpp.js.map
.parcel-cache .parcel-cache
exe/ exe/
*.log *.log

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/setup_cpp.mjs vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -34,7 +34,7 @@
"lint.prettier": "prettier --write .", "lint.prettier": "prettier --write .",
"lint.tsc": "tsc --noEmit | loose-ts-check", "lint.tsc": "tsc --noEmit | loose-ts-check",
"pack.exe": "shx rm -rf ./dist/tsconfig.tsbuildinfo && node ./dev/scripts/pack-exe.js", "pack.exe": "shx rm -rf ./dist/tsconfig.tsbuildinfo && node ./dev/scripts/pack-exe.js",
"prepare": "npm run build", "prepare": "pnpm run -r build && pnpm run -w build",
"start.docker": "docker run -t setup_cpp .", "start.docker": "docker run -t setup_cpp .",
"start.docker.arch": "docker run -t setup_cpp:arch .", "start.docker.arch": "docker run -t setup_cpp:arch .",
"start.docker.fedora": "docker run -t setup_cpp:fedora .", "start.docker.fedora": "docker run -t setup_cpp:fedora .",

View File

@ -0,0 +1,34 @@
{
"name": "exec-powershell",
"version": "1.0.0",
"description": "Run a powershell command.",
"homepage": "https://github.com/aminya/setup-cpp",
"license": "Apache-2.0",
"author": "Amin Yahyaabadi",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"source": "./src/index.ts",
"scripts": {
"build": "tsc"
},
"dependencies": {
"execa": "^5.1.1",
"which": "^2.0.2"
},
"devDependencies": {
"@types/which": "^2.0.1"
},
"keywords": [
"powershell",
"pwsh",
"exec",
"execa",
"spawn",
"system",
"github-actions",
"github",
"actions",
"gitlab",
"ci"
]
}

View File

@ -0,0 +1,48 @@
import execa from "execa"
import which from "which"
/** The cached powershell path */
let powershell: string | undefined
/**
* Execute a powershell command.
*
* @param command The powershell command to execute
* @param startupFlags The optional startup flags to be passed to powershell.
*
* Defaults to `["-NoProfile", "-NoLogo", "-NonInteractive"]`. This means that the Powershell profile is not sourced first.
* @param execOptions The options passed to `execa`.
*
* Defaults to `{ stdio: "inherit" }`
* @note It prefers `pwsh` over `powershell`
*/
export function execPowershell(
command: string,
startupFlags: string[] = ["-NoProfile", "-NoLogo", "-NonInteractive"],
execOptions: execa.Options = { stdio: "inherit" }
) {
return execa(getPowerShell(), [...startupFlags, "-c", command], execOptions)
}
/**
* Get the path to the powershell executable.
*
* @note It prefers `pwsh` over `powershell`
* @note It caches the path for the subsequent calls to this function
*/
export function getPowerShell() {
if (powershell === undefined) {
const maybePwsh = which.sync("pwsh", { nothrow: true })
if (maybePwsh !== null) {
powershell = maybePwsh
}
const maybePowerShell = which.sync("powershell", { nothrow: true })
if (maybePowerShell !== null) {
powershell = maybePowerShell
}
}
if (powershell === undefined) {
throw new Error("Could not find powershell")
}
return powershell
}

View File

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}

View File

@ -8,6 +8,9 @@ overrides:
core-js: '*' core-js: '*'
babel-eslint: npm:@babel/eslint-parser babel-eslint: npm:@babel/eslint-parser
importers:
.:
specifiers: specifiers:
'@actions/core': ^1.9.0 '@actions/core': ^1.9.0
'@actions/exec': ^1.1.1 '@actions/exec': ^1.1.1
@ -47,7 +50,6 @@ specifiers:
ubuntu-version: ^2.0.0 ubuntu-version: ^2.0.0
untildify: ^4.0.0 untildify: ^4.0.0
which: ^2.0.2 which: ^2.0.2
dependencies: dependencies:
'@actions/core': 1.9.0 '@actions/core': 1.9.0
'@actions/exec': 1.1.1 '@actions/exec': 1.1.1
@ -64,7 +66,6 @@ dependencies:
ubuntu-version: 2.0.0 ubuntu-version: 2.0.0
untildify: 4.0.0 untildify: 4.0.0
which: 2.0.2 which: 2.0.2
devDependencies: devDependencies:
'@types/cross-spawn': 6.0.2 '@types/cross-spawn': 6.0.2
'@types/jest': 28.1.6 '@types/jest': 28.1.6
@ -90,6 +91,17 @@ devDependencies:
ts-jest: 28.0.7_bi2kohzqnxavgozw3csgny5hju ts-jest: 28.0.7_bi2kohzqnxavgozw3csgny5hju
typescript: 4.7.4 typescript: 4.7.4
packages/exec-powershell:
specifiers:
'@types/which': ^2.0.1
execa: ^5.1.1
which: ^2.0.2
dependencies:
execa: 5.1.1
which: 2.0.2
devDependencies:
'@types/which': 2.0.1
packages: packages:
/@actions/cache/3.0.0: /@actions/cache/3.0.0:

3
pnpm-workspace.yaml Normal file
View File

@ -0,0 +1,3 @@
packages:
- "./"
- "packages/*"

View File

@ -7,7 +7,11 @@ import { execPowershell } from "../exec/powershell"
import { delimiter } from "path" import { delimiter } from "path"
import { escapeSpace } from "../path/escape_space" import { escapeSpace } from "../path/escape_space"
/** An add path function that works locally or inside GitHub Actions */ /**
* Add an environment variable.
*
* This function is cross-platforms and works in all the local or CI systems.
*/
export async function addEnv(name: string, valGiven: string | undefined, shouldEscapeSpace: boolean = false) { export async function addEnv(name: string, valGiven: string | undefined, shouldEscapeSpace: boolean = false) {
const val = shouldEscapeSpace ? escapeSpace(valGiven) : valGiven const val = shouldEscapeSpace ? escapeSpace(valGiven) : valGiven
try { try {
@ -27,7 +31,11 @@ export async function addEnv(name: string, valGiven: string | undefined, shouldE
} }
} }
/** An add path function that works locally or inside GitHub Actions */ /**
* Add a path to the PATH environment variable.
*
* This function is cross-platforms and works in all the local or CI systems.
*/
export async function addPath(path: string) { export async function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}` process.env.PATH = `${path}${delimiter}${process.env.PATH}`
try { try {

View File

@ -17,8 +17,8 @@
"preserveSymlinks": true, "preserveSymlinks": true,
"removeComments": false, "removeComments": false,
"skipLibCheck": false, "skipLibCheck": false,
"lib": ["ES2018", "dom"], "lib": ["ES2020", "dom"],
"target": "ES2018", "target": "ES2020",
"allowJs": true, "allowJs": true,
"esModuleInterop": true, "esModuleInterop": true,
"resolveJsonModule": true, "resolveJsonModule": true,