-
Notifications
You must be signed in to change notification settings - Fork 757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use shell-quote
package
#4080
use shell-quote
package
#4080
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import shellquote from "shell-quote"; | ||
|
||
export const quote = shellquote.quote; | ||
|
||
export function parse(cmd: string, env?: Record<string, string>): string[] { | ||
const entries = shellquote.parse(cmd, env); | ||
const argv: string[] = []; | ||
|
||
for (const entry of entries) { | ||
// use string entries, as is | ||
if (typeof entry === "string") { | ||
argv.push(entry); | ||
continue; | ||
} | ||
|
||
// ignore comments | ||
if ("comment" in entry) { | ||
continue; | ||
} | ||
|
||
// we don't want to resolve globs, passthrough the pattern unexpanded | ||
if (entry.op === "glob") { | ||
argv.push(entry.pattern); | ||
continue; | ||
} | ||
|
||
// any other entry.op is a ControlOperator (e.g. && or ||) we don't want to support | ||
throw new Error( | ||
`Only simple commands are supported, please don't use the "${entry.op}" operator in "${cmd}".` | ||
); | ||
} | ||
|
||
return argv; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4323,7 +4323,7 @@ addEventListener('fetch', event => {});` | |
mockSubDomainRequest(); | ||
mockUploadWorkerRequest(); | ||
await runWrangler( | ||
"deploy --dry-run --outdir dist --define abc:'https://www.abc.net.au/news/'" | ||
`deploy --dry-run --outdir dist --define "abc:'https://www.abc.net.au/news/'"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't a correct example of how a user would specify the Before (incorrect, note the missing quotes by the time node.js sees the value): ▶ node -e 'console.log(process.argv.slice(1))' -- --define abc:'123'
[ '--define', 'abc:123' ] After (correct, note the quotes in the value as expected): ▶ node -e 'console.log(process.argv.slice(1))' -- --define "abc:'123'"
[ '--define', "abc:'123'" ] |
||
); | ||
|
||
expect(fs.readFileSync("dist/index.js", "utf-8")).toContain( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -528,7 +528,7 @@ describe("wrangler", () => { | |
}, | ||
}); | ||
await runWrangler( | ||
`kv:key put dKey dVal --namespace-id some-namespace-id --metadata {"mKey":"mValue"}` | ||
`kv:key put dKey dVal --namespace-id some-namespace-id --metadata '{"mKey":"mValue"}'` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't a correct example of how a user would specify the metadata arg with JSON. Before (incorrect, note the missing quotes which would fail JSON.parse) ▶ node -e 'console.log(process.argv.slice(1))' -- --metadata {"mKey":"mValue"}
[ '--metadata', '{mKey:mValue}' ] After (correct, note the valid JSON as expected): ▶ node -e 'console.log(process.argv.slice(1))' -- --metadata '{"mKey":"mValue"}'
[ '--metadata', '{"mKey":"mValue"}' ] |
||
); | ||
expect(requests.count).toEqual(1); | ||
expect(std.out).toMatchInlineSnapshot( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { getPackageManager } from "./package-manager"; | |
import { parsePackageJSON, parseTOML, readFileSync } from "./parse"; | ||
import { getBasePath } from "./paths"; | ||
import { requireAuth } from "./user"; | ||
import * as shellquote from "./utils/shell-quote"; | ||
import { CommandLineArgsError, printWranglerBanner } from "./index"; | ||
|
||
import type { RawConfig } from "./config"; | ||
|
@@ -184,7 +185,7 @@ export async function initHandler(args: InitArgs) { | |
} | ||
|
||
const c3Arguments = [ | ||
...getC3CommandFromEnv().split(" "), | ||
...shellquote.parse(getC3CommandFromEnv()), | ||
Comment on lines
-187
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would have failed in scenarios where the c3 command was pointing to a local executable, for example, in a directory with spaces |
||
fromDashScriptName, | ||
...(yesFlag && isNpm(packageManager) ? ["-y"] : []), // --yes arg for npx | ||
...(isNpm(packageManager) ? ["--"] : []), | ||
|
@@ -252,7 +253,7 @@ export async function initHandler(args: InitArgs) { | |
c3Arguments.unshift("-y"); // arg for npx | ||
} | ||
|
||
c3Arguments.unshift(...getC3CommandFromEnv().split(" ")); | ||
c3Arguments.unshift(...shellquote.parse(getC3CommandFromEnv())); | ||
Comment on lines
-255
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would have failed in scenarios where the c3 command was pointing to a local executable, for example, in a directory with spaces |
||
|
||
// Deprecate the `init --from-dash` command | ||
const replacementC3Command = `\`${packageManager.type} ${c3Arguments.join( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import shellquote from "shell-quote"; | ||
|
||
export const quote = shellquote.quote; | ||
|
||
export function parse(cmd: string, env?: Record<string, string>): string[] { | ||
const entries = shellquote.parse(cmd, env); | ||
const argv: string[] = []; | ||
|
||
for (const entry of entries) { | ||
// use string entries, as is | ||
if (typeof entry === "string") { | ||
argv.push(entry); | ||
continue; | ||
} | ||
|
||
// ignore comments | ||
if ("comment" in entry) { | ||
continue; | ||
} | ||
|
||
// we don't want to resolve globs, passthrough the pattern unexpanded | ||
if (entry.op === "glob") { | ||
argv.push(entry.pattern); | ||
continue; | ||
} | ||
|
||
// any other entry.op is a ControlOperator (e.g. && or ||) we don't want to support | ||
throw new Error( | ||
`Only simple commands are supported, please don't use the "${entry.op}" operator in "${cmd}".` | ||
); | ||
} | ||
|
||
return argv; | ||
} | ||
Comment on lines
+1
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicated across C3 and wrangler. I will extract to a new shared package when #4038 is merged |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test forgot to specify the
database
positional argument. It was passing before because thecommand
argument has a space (and previously we split the whole command by space), resulting incommand
parsed as'select
anddatabase
parsed as1;'