mirror of
https://github.com/pnpm/action-setup.git
synced 2026-03-04 08:01:02 +08:00
Revert old styling
This commit is contained in:
parent
b2ad3f504b
commit
4a8f7ada16
@ -4,6 +4,7 @@ import runSelfInstaller from "./run";
|
|||||||
import { exec } from "child_process";
|
import { exec } from "child_process";
|
||||||
import { promisify } from "util";
|
import { promisify } from "util";
|
||||||
|
|
||||||
|
export { runSelfInstaller };
|
||||||
const execAsync = promisify(exec);
|
const execAsync = promisify(exec);
|
||||||
|
|
||||||
export async function install(inputs: Inputs) {
|
export async function install(inputs: Inputs) {
|
||||||
@ -17,7 +18,9 @@ export async function install(inputs: Inputs) {
|
|||||||
endGroup();
|
endGroup();
|
||||||
|
|
||||||
if (status) {
|
if (status) {
|
||||||
setFailed(`Something went wrong, self-installer exits with code ${status}`);
|
return setFailed(
|
||||||
|
`Something went wrong, self-installer exits with code ${status}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,134 +1,115 @@
|
|||||||
import { addPath, exportVariable } from "@actions/core";
|
import { addPath, exportVariable } from '@actions/core'
|
||||||
import { spawn } from "child_process";
|
import { spawn } from 'child_process'
|
||||||
import { rm, writeFile, mkdir, copyFile } from "fs/promises";
|
import { rm, writeFile, mkdir, copyFile } from 'fs/promises'
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from 'fs'
|
||||||
import path from "path";
|
import path from 'path'
|
||||||
import { execPath } from "process";
|
import { execPath } from 'process'
|
||||||
import util from "util";
|
import util from 'util'
|
||||||
import { Inputs } from "../inputs";
|
import { Inputs } from '../inputs'
|
||||||
import { parse as parseYaml } from "yaml";
|
import { parse as parseYaml } from 'yaml'
|
||||||
|
|
||||||
export async function runSelfInstaller(inputs: Inputs): Promise<number> {
|
export async function runSelfInstaller(inputs: Inputs): Promise<number> {
|
||||||
const { version, dest, packageJsonFile, standalone } = inputs;
|
const { version, dest, packageJsonFile, standalone } = inputs
|
||||||
const { GITHUB_WORKSPACE } = process.env;
|
const { GITHUB_WORKSPACE } = process.env
|
||||||
|
|
||||||
// prepare self install
|
// prepare self install
|
||||||
await rm(dest, { recursive: true, force: true });
|
await rm(dest, { recursive: true, force: true })
|
||||||
// create dest directory after removal
|
// create dest directory after removal
|
||||||
await mkdir(dest, { recursive: true });
|
await mkdir(dest, { recursive: true })
|
||||||
const pkgJson = path.join(dest, "package.json");
|
const pkgJson = path.join(dest, 'package.json')
|
||||||
// we have ensured the dest directory exists, we can write the file directly
|
// we have ensured the dest directory exists, we can write the file directly
|
||||||
await writeFile(pkgJson, JSON.stringify({ private: true }));
|
await writeFile(pkgJson, JSON.stringify({ private: true }))
|
||||||
|
|
||||||
// copy .npmrc if it exists to install from custom registry
|
// copy .npmrc if it exists to install from custom registry
|
||||||
if (GITHUB_WORKSPACE) {
|
if (GITHUB_WORKSPACE) {
|
||||||
try {
|
try {
|
||||||
await copyFile(
|
await copyFile(path.join(GITHUB_WORKSPACE, '.npmrc'), path.join(dest, '.npmrc'))
|
||||||
path.join(GITHUB_WORKSPACE, ".npmrc"),
|
|
||||||
path.join(dest, ".npmrc"),
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Swallow error if .npmrc doesn't exist
|
// Swallow error if .npmrc doesn't exist
|
||||||
if (
|
if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error
|
||||||
!util.types.isNativeError(error) ||
|
|
||||||
!("code" in error) ||
|
|
||||||
error.code !== "ENOENT"
|
|
||||||
)
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare target pnpm
|
// prepare target pnpm
|
||||||
const target = await readTarget({ version, packageJsonFile, standalone });
|
const target = await readTarget({ version, packageJsonFile, standalone })
|
||||||
const cp = spawn(
|
const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], {
|
||||||
execPath,
|
|
||||||
[path.join(__dirname, "pnpm.cjs"), "install", target, "--no-lockfile"],
|
|
||||||
{
|
|
||||||
cwd: dest,
|
cwd: dest,
|
||||||
stdio: ["pipe", "inherit", "inherit"],
|
stdio: ['pipe', 'inherit', 'inherit'],
|
||||||
},
|
})
|
||||||
);
|
|
||||||
|
|
||||||
const exitCode = await new Promise<number>((resolve, reject) => {
|
const exitCode = await new Promise<number>((resolve, reject) => {
|
||||||
cp.on("error", reject);
|
cp.on('error', reject)
|
||||||
cp.on("close", resolve);
|
cp.on('close', resolve)
|
||||||
});
|
})
|
||||||
if (exitCode === 0) {
|
if (exitCode === 0) {
|
||||||
const pnpmHome = path.join(dest, "node_modules/.bin");
|
const pnpmHome = path.join(dest, 'node_modules/.bin')
|
||||||
addPath(pnpmHome);
|
addPath(pnpmHome)
|
||||||
exportVariable("PNPM_HOME", pnpmHome);
|
exportVariable('PNPM_HOME', pnpmHome)
|
||||||
}
|
}
|
||||||
return exitCode;
|
return exitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readTarget(opts: {
|
async function readTarget(opts: {
|
||||||
readonly version?: string | undefined;
|
readonly version?: string | undefined
|
||||||
readonly packageJsonFile: string;
|
readonly packageJsonFile: string
|
||||||
readonly standalone: boolean;
|
readonly standalone: boolean
|
||||||
}) {
|
}) {
|
||||||
const { version, packageJsonFile, standalone } = opts;
|
const { version, packageJsonFile, standalone } = opts
|
||||||
const { GITHUB_WORKSPACE } = process.env;
|
const { GITHUB_WORKSPACE } = process.env
|
||||||
|
|
||||||
let packageManager;
|
let packageManager
|
||||||
|
|
||||||
if (GITHUB_WORKSPACE) {
|
if (GITHUB_WORKSPACE) {
|
||||||
try {
|
try {
|
||||||
const content = readFileSync(
|
const content = readFileSync(path.join(GITHUB_WORKSPACE, packageJsonFile), 'utf8');
|
||||||
path.join(GITHUB_WORKSPACE, packageJsonFile),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
({ packageManager } = packageJsonFile.endsWith(".yaml")
|
({ packageManager } = packageJsonFile.endsWith(".yaml")
|
||||||
? parseYaml(content, { merge: true })
|
? parseYaml(content, { merge: true })
|
||||||
: JSON.parse(content));
|
: JSON.parse(content)
|
||||||
|
)
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
// Swallow error if package.json doesn't exist in root
|
// Swallow error if package.json doesn't exist in root
|
||||||
if (
|
if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error
|
||||||
!util.types.isNativeError(error) ||
|
|
||||||
!("code" in error) ||
|
|
||||||
error.code !== "ENOENT"
|
|
||||||
)
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version) {
|
if (version) {
|
||||||
if (
|
if (
|
||||||
typeof packageManager === "string" &&
|
typeof packageManager === 'string' &&
|
||||||
packageManager.startsWith("pnpm@") &&
|
packageManager.startsWith('pnpm@') &&
|
||||||
packageManager.replace("pnpm@", "") !== version
|
packageManager.replace('pnpm@', '') !== version
|
||||||
) {
|
) {
|
||||||
throw new Error(`Multiple versions of pnpm specified:
|
throw new Error(`Multiple versions of pnpm specified:
|
||||||
- version ${version} in the GitHub Action config with the key "version"
|
- version ${version} in the GitHub Action config with the key "version"
|
||||||
- version ${packageManager} in the package.json with the key "packageManager"
|
- version ${packageManager} in the package.json with the key "packageManager"
|
||||||
Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`);
|
Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${standalone ? "@pnpm/exe" : "pnpm"}@${version}`;
|
return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!GITHUB_WORKSPACE) {
|
if (!GITHUB_WORKSPACE) {
|
||||||
throw new Error(`No workspace is found.
|
throw new Error(`No workspace is found.
|
||||||
If you've intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file,
|
If you've intended to let pnpm/action-setup read preferred pnpm version from the "packageManager" field in the package.json file,
|
||||||
please run the actions/checkout before pnpm/action-setup.
|
please run the actions/checkout before pnpm/action-setup.
|
||||||
Otherwise, please specify the pnpm version in the action configuration.`);
|
Otherwise, please specify the pnpm version in the action configuration.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof packageManager !== "string") {
|
if (typeof packageManager !== 'string') {
|
||||||
throw new Error(`No pnpm version is specified.
|
throw new Error(`No pnpm version is specified.
|
||||||
Please specify it by one of the following ways:
|
Please specify it by one of the following ways:
|
||||||
- in the GitHub Action config with the key "version"
|
- in the GitHub Action config with the key "version"
|
||||||
- in the package.json with the key "packageManager"`);
|
- in the package.json with the key "packageManager"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!packageManager.startsWith("pnpm@")) {
|
if (!packageManager.startsWith('pnpm@')) {
|
||||||
throw new Error("Invalid packageManager field in package.json");
|
throw new Error('Invalid packageManager field in package.json')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (standalone) {
|
if (standalone) {
|
||||||
return packageManager.replace("pnpm@", "@pnpm/exe@");
|
return packageManager.replace('pnpm@', '@pnpm/exe@')
|
||||||
}
|
}
|
||||||
|
|
||||||
return packageManager;
|
return packageManager
|
||||||
}
|
}
|
||||||
|
|
||||||
export default runSelfInstaller;
|
export default runSelfInstaller
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user