Skip to content

Commit

Permalink
use shell-quote.quote(args) instead of args.join(" ") (#4215)
Browse files Browse the repository at this point in the history
* override shell-quote.quote to allow non-string args
which will be stringified

* update snapshots
something weird is going on with jest snapshot encoding
- double backslash \\ is changed into a forward slash /
- this doesn't effect real usage \\@ is what is actually returned from shell-quote and that works
  • Loading branch information
RamIdeas authored Oct 23, 2023
1 parent d74c35f commit 950bc40
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-parrots-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"create-cloudflare": patch
"wrangler": patch
---

fix various logging of shell commands to correctly quote args when needed
5 changes: 4 additions & 1 deletion packages/create-cloudflare/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { detectPackageManager } from "helpers/packages";
import semver from "semver";
import { version } from "../package.json";
import { validateProjectDirectory } from "./common";
import * as shellquote from "./helpers/shell-quote";
import { templateMap } from "./templateMap";
import type { C3Args } from "types";

Expand Down Expand Up @@ -55,7 +56,9 @@ export const runLatest = async () => {
// the parsing logic of `npm create` requires `--` to be supplied
// before any flags intended for the target command.
const argString =
npm === "npm" ? `-- ${args.join(" ")}` : `${args.join(" ")}`;
npm === "npm"
? `-- ${shellquote.quote(args)}`
: `${shellquote.quote(args)}`;

await runCommand(`${npm} create cloudflare@latest ${argString}`);
};
Expand Down
6 changes: 5 additions & 1 deletion packages/create-cloudflare/src/helpers/shell-quote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import shellquote from "shell-quote";

export const quote = shellquote.quote;
export const quote = function (args: (string | number | boolean)[]) {
const stringArgs = args.map((arg) => String(arg));

return shellquote.quote(stringArgs);
};

export function parse(cmd: string, env?: Record<string, string>): string[] {
// This is a workaround for a bug in shell-quote on Windows
Expand Down
5 changes: 4 additions & 1 deletion packages/create-cloudflare/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
runDeploy,
setupProjectDirectory,
} from "./common";
import * as shellquote from "./helpers/shell-quote";
import type { C3Args, PagesGeneratorContext } from "types";

/** How many times to retry the create project command before failing. */
Expand Down Expand Up @@ -162,7 +163,9 @@ const createProject = async (ctx: PagesGeneratorContext) => {
const CLOUDFLARE_ACCOUNT_ID = ctx.account.id;

try {
const compatFlags = ctx.framework?.config.compatibilityFlags?.join(" ");
const compatFlags = shellquote.quote(
ctx.framework?.config.compatibilityFlags ?? []
);
const compatFlagsArg = compatFlags
? `--compatibility-flags ${compatFlags}`
: "";
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("generate", () => {
`"✨ Created no-template/wrangler.toml"`
);
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe \`init\` command is no longer supported. Please use \`mockpm create cloudflare@2 no-template\` instead.[0m
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mThe \`init\` command is no longer supported. Please use \`mockpm create cloudflare/@2 no-template\` instead.[0m
The \`init\` command will be removed in a future version.
Expand Down
86 changes: 43 additions & 43 deletions packages/wrangler/src/__tests__/init.test.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/wrangler/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ export async function initHandler(args: InitArgs) {
c3Arguments.unshift(...shellquote.parse(getC3CommandFromEnv()));

// Deprecate the `init --from-dash` command
const replacementC3Command = `\`${packageManager.type} ${c3Arguments.join(
" "
const replacementC3Command = `\`${packageManager.type} ${shellquote.quote(
c3Arguments
)}\``;

logger.warn(
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FatalError } from "../errors";
import { logger } from "../logger";
import * as metrics from "../metrics";
import { getBasePath } from "../paths";
import * as shellquote from "../utils/shell-quote";
import { buildFunctions } from "./buildFunctions";
import { ROUTES_SPEC_VERSION, SECONDS_TO_WAIT_FOR_PROXY } from "./constants";
import {
Expand Down Expand Up @@ -797,7 +798,7 @@ async function spawnProxyProcess({
);
}

logger.log(`Running ${command.join(" ")}...`);
logger.log(`Running ${shellquote.quote(command)}...`);
const proxy = spawn(
command[0].toString(),
command.slice(1).map((value) => value.toString()),
Expand Down
6 changes: 5 additions & 1 deletion packages/wrangler/src/utils/shell-quote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import shellquote from "shell-quote";

export const quote = shellquote.quote;
export const quote = function (args: (string | number | boolean)[]) {
const stringArgs = args.map((arg) => String(arg));

return shellquote.quote(stringArgs);
};

export function parse(cmd: string, env?: Record<string, string>): string[] {
// This is a workaround for a bug in shell-quote on Windows
Expand Down

0 comments on commit 950bc40

Please sign in to comment.