deno_task_shell icon indicating copy to clipboard operation
deno_task_shell copied to clipboard

[feature request] Support looping over simple lists

Open niedzielski opened this issue 2 years ago • 3 comments

Historically, looping has been pretty handy. Simple support is wanted to avoid lengthier JavaScripts:

$ for i in a b c; do echo 1 $i 2; done
1 a 2
1 b 2
1 c 2

The intended use case is simple lists and differs from xargs which is useful for mapping stdin to a command.

References

niedzielski avatar Mar 11 '23 19:03 niedzielski

A big criticism of deno task when it was initially made was about how we already have a scripting language (JavaScript) in Deno, but I argued that it's good to be used as a bookmarking system of common commands that are almost 1:1 with what you type on the command line. That way the command you execute on the command line that you want to save for the future and others doesn't need to be rewritten into JavaScript (you just copy and paste the command into your Deno config, then it hopefully works cross platform). I also argued that deno task should mostly just be used for simple orchestration and not anything complex which could be moved into JavaScript (writing large scripts in a json string is a pain anyway)

Due to that, it might be difficult to get this one in. Right now if people need loops the recommened thing to do would be to create a JavaScript file that does this and call it from a deno task via deno run ....

dsherret avatar Mar 11 '23 20:03 dsherret

That's fair :+1: FWIW, Deno has a reputation for not needing a bunch of scripts and configs. I think that's a little at odds though with this feature. Right now the ability to write practical Deno one-liners is pretty limited without more robust task support like looping.

I do think this opens a can of worms though for a vein of work the Deno team might not want to focus on. However, between no longer having a deno bundle command and limited NPM script-like support, I find myself having to write a handful of supplemental files in Deno projects that my NPM projects skirt around because they use plain shell for tasks.

deno_task_shell is brilliant(!) and I'm not suggesting I want to use shell in Deno but idiomatic NPM scripts assume shell and that gives a lot of succinct flexibility that Deno tasks doesn't have right now.

niedzielski avatar Mar 11 '23 20:03 niedzielski

for i in a b c; do echo 1 $i 2; done

@niedzielski For comparison, here's what it looks like to implement your example loop using Deno's eval command without a separate script file — slightly longer, but only just:

// deno.jsonc
{
  "tasks": {
    "loop": "deno eval 'for (const i of \"abc\") console.log(1, i, 2);'"
  }
}
% deno task loop
Task loop deno eval 'for (const i of "abc") console.log(1, i, 2);'
1 a 2
1 b 2
1 c 2

jsejcksn avatar May 11 '23 12:05 jsejcksn