Detect package manager and uses its commands

Fix #156
This commit is contained in:
Han Yeong-woo 2023-09-02 06:33:25 +09:00
parent f4f2e854d7
commit f2e4cda4dd
No known key found for this signature in database
GPG key ID: A46E340CB4E4D6AD
3 changed files with 72 additions and 16 deletions

View file

@ -1,20 +1,45 @@
import { import {
getBooleanInput,
getInput, getInput,
getMultilineInput, getMultilineInput,
setFailed,
info as originalInfo,
error as originalError,
endGroup as originalEndGroup, endGroup as originalEndGroup,
error as originalError,
info as originalInfo,
startGroup as originalStartGroup, startGroup as originalStartGroup,
getBooleanInput, setFailed,
} from "@actions/core"; } from "@actions/core";
import { execSync, exec } from "node:child_process"; import { exec, execSync } from "node:child_process";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
import * as util from "node:util"; import * as util from "node:util";
import {
PackageManager,
checkWorkingDirectory,
detectPackageManager,
semverCompare,
} from "./utils";
const execAsync = util.promisify(exec); const execAsync = util.promisify(exec);
const DEFAULT_WRANGLER_VERSION = "3.5.1"; const DEFAULT_WRANGLER_VERSION = "3.5.1";
interface PackageManagerCommands {
install: string;
exec: string;
}
const PACKAGE_MANAGER_COMMANDS = {
npm: {
install: "npm i",
exec: "npm exec",
},
yarn: {
install: "yarn add",
exec: "yarn exec",
},
pnpm: {
install: "pnpm add",
exec: "pnpm exec",
},
} as const satisfies Readonly<Record<PackageManager, PackageManagerCommands>>;
/** /**
* A configuration object that contains all the inputs & immutable state for the action. * A configuration object that contains all the inputs & immutable state for the action.
*/ */
@ -30,6 +55,17 @@ const config = {
QUIET_MODE: getBooleanInput("quiet"), QUIET_MODE: getBooleanInput("quiet"),
} as const; } as const;
function realPackageManager(): PackageManager {
const packageManager = detectPackageManager(config.workingDirectory);
if (packageManager !== null) {
return packageManager;
}
throw new Error("Package manager is not detected");
}
const pkgManagerCmd = PACKAGE_MANAGER_COMMANDS[realPackageManager()];
function info(message: string, bypass?: boolean): void { function info(message: string, bypass?: boolean): void {
if (!config.QUIET_MODE || bypass) { if (!config.QUIET_MODE || bypass) {
originalInfo(message); originalInfo(message);
@ -94,7 +130,7 @@ function installWrangler() {
); );
} }
startGroup("📥 Installing Wrangler"); startGroup("📥 Installing Wrangler");
const command = `npm install wrangler@${config["WRANGLER_VERSION"]}`; const command = `${pkgManagerCmd.install} wrangler@${config["WRANGLER_VERSION"]}`;
info(`Running command: ${command}`); info(`Running command: ${command}`);
execSync(command, { cwd: config["workingDirectory"], env: process.env }); execSync(command, { cwd: config["workingDirectory"], env: process.env });
info(`✅ Wrangler installed`, true); info(`✅ Wrangler installed`, true);
@ -115,7 +151,7 @@ async function execCommands(commands: string[], cmdType: string) {
try { try {
const arrPromises = commands.map(async (command) => { const arrPromises = commands.map(async (command) => {
const cmd = command.startsWith("wrangler") const cmd = command.startsWith("wrangler")
? `${getNpxCmd()} ${command}` ? `${pkgManagerCmd.exec} ${command}`
: command; : command;
info(`🚀 Executing command: ${cmd}`); info(`🚀 Executing command: ${cmd}`);
@ -155,9 +191,9 @@ async function legacyUploadSecrets(
) { ) {
const arrPromises = secrets const arrPromises = secrets
.map((secret) => { .map((secret) => {
const command = `echo ${getSecret( const command = `echo ${getSecret(secret)} | ${
secret, pkgManagerCmd.exec
)} | ${getNpxCmd()} wrangler secret put ${secret}`; } wrangler secret put ${secret}`;
return environment ? command.concat(` --env ${environment}`) : command; return environment ? command.concat(` --env ${environment}`) : command;
}) })
.map( .map(
@ -198,7 +234,7 @@ async function uploadSecrets() {
const secretCmd = `echo "${JSON.stringify(secretObj).replaceAll( const secretCmd = `echo "${JSON.stringify(secretObj).replaceAll(
'"', '"',
'\\"', '\\"',
)}" | ${getNpxCmd()} wrangler secret:bulk ${environmentSuffix}`; )}" | ${pkgManagerCmd.exec} wrangler secret:bulk ${environmentSuffix}`;
execSync(secretCmd, { execSync(secretCmd, {
cwd: workingDirectory, cwd: workingDirectory,
@ -247,7 +283,7 @@ async function wranglerCommands() {
command = command.concat(` --env ${environment}`); command = command.concat(` --env ${environment}`);
} }
const cmd = `${getNpxCmd()} wrangler ${command} ${ const cmd = `${pkgManagerCmd.exec} wrangler ${command} ${
(command.startsWith("deploy") || command.startsWith("publish")) && (command.startsWith("deploy") || command.startsWith("publish")) &&
!command.includes(`--var`) !command.includes(`--var`)
? getVarArgs() ? getVarArgs()
@ -271,9 +307,9 @@ async function wranglerCommands() {
main(); main();
export { export {
wranglerCommands,
execCommands,
uploadSecrets,
authenticationSetup, authenticationSetup,
execCommands,
installWrangler, installWrangler,
uploadSecrets,
wranglerCommands,
}; };

10
test/base/package-lock.json generated Normal file
View file

@ -0,0 +1,10 @@
{
"name": "wrangler-action-test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wrangler-action-test"
}
}
}

10
test/environment/package-lock.json generated Normal file
View file

@ -0,0 +1,10 @@
{
"name": "wrangler-action-environment-test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wrangler-action-environment-test"
}
}
}