Compare commits

...

18 Commits

Author SHA1 Message Date
Han Yeong-woo aec73ae744 Add wangler files in fixtures 2023-09-19 03:52:00 +09:00
Han Yeong-woo f0cc3efccf Install yarn and pnpm on test 2023-09-19 03:51:27 +09:00
Han Yeong-woo 50b529e7b8 Fix pass arg issue 2023-09-19 03:50:54 +09:00
Han Yeong-woo 6771675815 Run format 2023-09-19 01:37:01 +09:00
Jacob M-G Evans 4ae0557f8d Update two-rocks-hope.md 2023-09-18 11:33:50 -05:00
Han Yeong-woo 7d7b98826e Add changeset 2023-09-19 01:30:36 +09:00
Han Yeong-woo d1073d57ba Run format 2023-09-19 01:30:35 +09:00
Han Yeong-woo c3b99e2c18 Ignore to pnpm-lock.yaml in formatting 2023-09-19 01:30:35 +09:00
Han Yeong-woo 2375787b23 Move test fixtures to parent directory 2023-09-19 01:30:35 +09:00
Han Yeong-woo 11f981cea4 Add tests for package managers support 2023-09-19 01:30:34 +09:00
Han Yeong-woo 868dbd9a40 Remove unused function 2023-09-19 01:30:34 +09:00
Han Yeong-woo a009342d77 Add packageManager setting 2023-09-19 01:30:34 +09:00
Han Yeong-woo f2e4cda4dd Detect package manager and uses its commands
Fix #156
2023-09-19 01:30:33 +09:00
Han Yeong-woo f4f2e854d7 Add isValidPackcageManager() util 2023-09-19 01:30:33 +09:00
Han Yeong-woo cbe5f5b523 Add detect package manager util function 2023-09-19 01:30:33 +09:00
Jacob M-G Evans fcf2d83f3d Merge pull request #172 from cloudflare/demosjarco/main
Node 20 update
2023-09-18 11:29:49 -05:00
DemosJarco f5d1ca36ae Added comment with link 2023-09-04 11:54:36 -07:00
DemosJarco 4d6d6abfb4 Update action.yml 2023-09-04 11:50:59 -07:00
24 changed files with 316 additions and 37 deletions
+7
View File
@@ -0,0 +1,7 @@
---
"wrangler-action": minor
---
Support for package managers other than npm, such as pnpm and yarn.
fixes #156
+40 -1
View File
@@ -111,4 +111,43 @@ jobs:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: delete --name wrangler-action-test --force
# END Setup and teardown of Workers w/ Secrets Tests
# END Setup and teardown of Workers w/ Secrets Tests
- name: Support packageManager variable
uses: ./
with:
workingDirectory: "./test/empty"
packageManager: "npm"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
- name: Support npm package manager
uses: ./
with:
workingDirectory: "./test/npm"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
- name: Install yarn
run: npm i -g yarn
- name: Support yarn package manager
uses: ./
with:
workingDirectory: "./test/yarn"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
- name: Install pnpm
run: npm i -g pnpm
- name: Support pnpm package manager
uses: ./
with:
workingDirectory: "./test/pnpm"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
+1
View File
@@ -0,0 +1 @@
pnpm-lock.yaml
+5 -1
View File
@@ -4,7 +4,8 @@ branding:
color: "orange"
description: "Deploy your Cloudflare projects from GitHub using Wrangler"
runs:
using: "node16"
# Possible values: https://github.com/actions/runner/blob/main/src/Runner.Common/Util/NodeUtil.cs#L9
using: "node20"
main: "dist/index.mjs"
inputs:
apiToken:
@@ -40,3 +41,6 @@ inputs:
vars:
description: "A string of environment variable names, separated by newlines. These will be bound to your Worker using the values of matching environment variables declared in `env` of this workflow."
required: false
packageManager:
description: "The name of the package manager to install and run wrangler. If not provided, it will be detected via the lock file. Valid values: [npm, pnpm, yarn]"
required: false
+58 -16
View File
@@ -1,20 +1,46 @@
import {
getBooleanInput,
getInput,
getMultilineInput,
setFailed,
info as originalInfo,
error as originalError,
endGroup as originalEndGroup,
error as originalError,
info as originalInfo,
startGroup as originalStartGroup,
getBooleanInput,
setFailed,
} from "@actions/core";
import { execSync, exec } from "node:child_process";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
import { exec, execSync } from "node:child_process";
import * as util from "node:util";
import {
PackageManager,
checkWorkingDirectory,
detectPackageManager,
isValidPackageManager,
semverCompare,
} from "./utils";
const execAsync = util.promisify(exec);
const DEFAULT_WRANGLER_VERSION = "3.5.1";
interface PackageManagerCommands {
install: string;
exec: string;
}
const PACKAGE_MANAGER_COMMANDS = {
npm: {
install: "npm i",
exec: "npx",
},
yarn: {
install: "yarn add",
exec: "yarn",
},
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.
*/
@@ -28,8 +54,24 @@ const config = {
VARS: getMultilineInput("vars"),
COMMANDS: getMultilineInput("command"),
QUIET_MODE: getBooleanInput("quiet"),
PACKAGE_MANAGER: getInput("packageManager"),
} as const;
function realPackageManager(): PackageManager {
if (isValidPackageManager(config.PACKAGE_MANAGER)) {
return config.PACKAGE_MANAGER;
}
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 {
if (!config.QUIET_MODE || bypass) {
originalInfo(message);
@@ -94,7 +136,7 @@ function installWrangler() {
);
}
startGroup("📥 Installing Wrangler");
const command = `npm install wrangler@${config["WRANGLER_VERSION"]}`;
const command = `${pkgManagerCmd.install} wrangler@${config["WRANGLER_VERSION"]}`;
info(`Running command: ${command}`);
execSync(command, { cwd: config["workingDirectory"], env: process.env });
info(`✅ Wrangler installed`, true);
@@ -115,7 +157,7 @@ async function execCommands(commands: string[], cmdType: string) {
try {
const arrPromises = commands.map(async (command) => {
const cmd = command.startsWith("wrangler")
? `${getNpxCmd()} ${command}`
? `${pkgManagerCmd.exec} ${command}`
: command;
info(`🚀 Executing command: ${cmd}`);
@@ -155,9 +197,9 @@ async function legacyUploadSecrets(
) {
const arrPromises = secrets
.map((secret) => {
const command = `echo ${getSecret(
secret,
)} | ${getNpxCmd()} wrangler secret put ${secret}`;
const command = `echo ${getSecret(secret)} | ${
pkgManagerCmd.exec
} wrangler secret put ${secret}`;
return environment ? command.concat(` --env ${environment}`) : command;
})
.map(
@@ -198,7 +240,7 @@ async function uploadSecrets() {
const secretCmd = `echo "${JSON.stringify(secretObj).replaceAll(
'"',
'\\"',
)}" | ${getNpxCmd()} wrangler secret:bulk ${environmentSuffix}`;
)}" | ${pkgManagerCmd.exec} wrangler secret:bulk ${environmentSuffix}`;
execSync(secretCmd, {
cwd: workingDirectory,
@@ -247,7 +289,7 @@ async function wranglerCommands() {
command = command.concat(` --env ${environment}`);
}
const cmd = `${getNpxCmd()} wrangler ${command} ${
const cmd = `${pkgManagerCmd.exec} wrangler ${command} ${
(command.startsWith("deploy") || command.startsWith("publish")) &&
!command.includes(`--var`)
? getVarArgs()
@@ -271,9 +313,9 @@ async function wranglerCommands() {
main();
export {
wranglerCommands,
execCommands,
uploadSecrets,
authenticationSetup,
execCommands,
installWrangler,
uploadSecrets,
wranglerCommands,
};
+38 -15
View File
@@ -1,19 +1,11 @@
import { expect, test, describe } from "vitest";
import { checkWorkingDirectory, getNpxCmd, semverCompare } from "./utils";
import path from "node:path";
test("getNpxCmd ", async () => {
process.env.RUNNER_OS = "Windows";
expect(getNpxCmd()).toBe("npx.cmd");
process.env.RUNNER_OS = "Mac";
expect(getNpxCmd()).toBe("npx");
process.env.RUNNER_OS = "Linux";
expect(getNpxCmd()).toBe("npx");
delete process.env.RUNNER_OS;
});
import { describe, expect, test } from "vitest";
import {
checkWorkingDirectory,
detectPackageManager,
isValidPackageManager,
semverCompare,
} from "./utils";
describe("semverCompare", () => {
test("should return false if the second argument is equal to the first argument", () => {
@@ -43,3 +35,34 @@ describe("checkWorkingDirectory", () => {
);
});
});
describe("detectPackageManager", () => {
test("should return name of package manager for current workspace", () => {
expect(detectPackageManager()).toBe("npm");
});
test("should return npm if package-lock.json exists", () => {
expect(detectPackageManager("test/npm")).toBe("npm");
});
test("should return yarn if yarn.lock exists", () => {
expect(detectPackageManager("test/yarn")).toBe("yarn");
});
test("should return pnpm if pnpm-lock.yaml exists", () => {
expect(detectPackageManager("test/pnpm")).toBe("pnpm");
});
test("should return null if no package manager is detected", () => {
expect(detectPackageManager("test/empty")).toBe(null);
});
});
test("isValidPackageManager", () => {
expect(isValidPackageManager("npm")).toBe(true);
expect(isValidPackageManager("pnpm")).toBe(true);
expect(isValidPackageManager("yarn")).toBe(true);
expect(isValidPackageManager("")).toBe(false);
expect(isValidPackageManager("ppnpm")).toBe(false);
});
+21 -4
View File
@@ -1,10 +1,6 @@
import { existsSync } from "node:fs";
import * as path from "node:path";
export function getNpxCmd() {
return process.env.RUNNER_OS === "Windows" ? "npx.cmd" : "npx";
}
/**
* A helper function to compare two semver versions. If the second arg is greater than the first arg, it returns true.
*/
@@ -33,3 +29,24 @@ export function checkWorkingDirectory(workingDirectory = ".") {
throw new Error(`Directory ${workingDirectory} does not exist.`);
}
}
export type PackageManager = "npm" | "yarn" | "pnpm";
export function detectPackageManager(
workingDirectory = ".",
): PackageManager | null {
if (existsSync(path.join(workingDirectory, "package-lock.json"))) {
return "npm";
}
if (existsSync(path.join(workingDirectory, "yarn.lock"))) {
return "yarn";
}
if (existsSync(path.join(workingDirectory, "pnpm-lock.yaml"))) {
return "pnpm";
}
return null;
}
export function isValidPackageManager(name: string): name is PackageManager {
return name === "npm" || name === "yarn" || name === "pnpm";
}
+10
View File
@@ -0,0 +1,10 @@
{
"name": "wrangler-action-test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wrangler-action-test"
}
}
}
+1
View File
@@ -0,0 +1 @@
export default {};
+3
View File
@@ -0,0 +1,3 @@
{
"name": "wrangler-action-detect-package-manager-test"
}
+4
View File
@@ -0,0 +1,4 @@
name = "wrangler-action-test"
main = "./index.ts"
compatibility_date = "2023-07-07"
workers_dev = true
+10
View File
@@ -0,0 +1,10 @@
{
"name": "wrangler-action-environment-test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wrangler-action-environment-test"
}
}
}
+26
View File
@@ -0,0 +1,26 @@
type Env = {
SECRET1?: string;
SECRET2?: string;
};
export default {
fetch(request: Request, env: Env) {
const url = new URL(request.url);
if (url.pathname === "/secret-health-check") {
const { SECRET1, SECRET2 } = env;
if (SECRET1 !== "SECRET_1_VALUE" || SECRET2 !== "SECRET_2_VALUE") {
throw new Error("SECRET1 or SECRET2 is not defined");
}
return new Response("OK");
}
// @ts-expect-error
return Response.json({
...request,
headers: Object.fromEntries(request.headers),
});
},
};
+10
View File
@@ -0,0 +1,10 @@
{
"name": "wrangler-action-npm-test",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wrangler-action-npm-test"
}
}
}
+3
View File
@@ -0,0 +1,3 @@
{
"name": "wrangler-action-npm-test"
}
+4
View File
@@ -0,0 +1,4 @@
name = "wrangler-action-test"
main = "./index.ts"
compatibility_date = "2023-07-07"
workers_dev = true
+26
View File
@@ -0,0 +1,26 @@
type Env = {
SECRET1?: string;
SECRET2?: string;
};
export default {
fetch(request: Request, env: Env) {
const url = new URL(request.url);
if (url.pathname === "/secret-health-check") {
const { SECRET1, SECRET2 } = env;
if (SECRET1 !== "SECRET_1_VALUE" || SECRET2 !== "SECRET_2_VALUE") {
throw new Error("SECRET1 or SECRET2 is not defined");
}
return new Response("OK");
}
// @ts-expect-error
return Response.json({
...request,
headers: Object.fromEntries(request.headers),
});
},
};
+3
View File
@@ -0,0 +1,3 @@
{
"name": "wrangler-action-pnpm-test"
}
+5
View File
@@ -0,0 +1,5 @@
lockfileVersion: '6.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
+4
View File
@@ -0,0 +1,4 @@
name = "wrangler-action-test"
main = "./index.ts"
compatibility_date = "2023-07-07"
workers_dev = true
+26
View File
@@ -0,0 +1,26 @@
type Env = {
SECRET1?: string;
SECRET2?: string;
};
export default {
fetch(request: Request, env: Env) {
const url = new URL(request.url);
if (url.pathname === "/secret-health-check") {
const { SECRET1, SECRET2 } = env;
if (SECRET1 !== "SECRET_1_VALUE" || SECRET2 !== "SECRET_2_VALUE") {
throw new Error("SECRET1 or SECRET2 is not defined");
}
return new Response("OK");
}
// @ts-expect-error
return Response.json({
...request,
headers: Object.fromEntries(request.headers),
});
},
};
+3
View File
@@ -0,0 +1,3 @@
{
"name": "wrangler-action-yarn-test"
}
+4
View File
@@ -0,0 +1,4 @@
name = "wrangler-action-test"
main = "./index.ts"
compatibility_date = "2023-07-07"
workers_dev = true
+4
View File
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1