birgersp
birgersp12mo ago

Deno.Command arg with an asterisk?

I am trying to execute a shell command from Deno, where one of the command args contains an asterisk. Example:
const output = new Deno.Command("cp", { args: ["source/*", "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
const output = new Deno.Command("cp", { args: ["source/*", "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
It yields:
cp: cannot stat 'source/*': No such file or directory
Q: How can I pass an asterisk to one of the Deno.Command args?
2 Replies
ioB
ioB12mo ago
The asterisk is a shell-only concept, and not something cp can really do. This is a fundamental "limitation" of the fork-exec call happening here. You'd need to manually enumerate the files yourself:
const source = Deno.readDirSync("source/*").map((entry)=>`source/${entry.name}`)
const output = new Deno.Command("cp", { args: [...source, "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
const source = Deno.readDirSync("source/*").map((entry)=>`source/${entry.name}`)
const output = new Deno.Command("cp", { args: [...source, "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
alternatively, if you really want to use shell constructs, you can simply call a shell instead
const output = new Deno.Command("sh", { args: ["-c", "cp source/* destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
const output = new Deno.Command("sh", { args: ["-c", "cp source/* destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
birgersp
birgersp12mo ago
❤️ for helping noobs like me, you are a true hero