deno-script 🦕
Enhanced scripting support for JavaScript/TypeScript with Deno 🦕 on *nix-based systems.
It is largely inspired by kscript. The idea is to leverage the scripting abilities of javascript using Deno.
I feel that scripting can be so much fun with Deno
as:
Node
homebrew
the last point should be done automatically. If not, make sure to manually add them to your .bash_profile
(or similar)… see bellow!# Add this to your .bash_profile
export DENO_INSTALL="$HOME/.deno"
export PATH=$PATH:$DENO_INSTALL/bin
If your are on zsh
:
curl -sSL "https://raw.githubusercontent.com/jiraguha/deno-script/master/install.sh" | bash -s "master" "zsh"
For others:
curl -sSL "https://raw.githubusercontent.com/jiraguha/deno-script/master/install.sh" | bash -s "master" "bash"
The main mode of operation is deno run <script>
. The <script>
can be a Javascript *.js or Typescritpt *.ts file , a script URL, -
for stdin, a process substitution file handle.
To use Deno as interpreter for a script:
deno-script
in the shebang line of your scripts:#!/usr/bin/env deno-script
// In hello.js
console.log("hello world")
for (let arg of Deno.args) {
console.log(`arg: ${arg}`)
}
Make it executable
$ chmod u+x hello.js;
Execute it
$ ./hello.js;
You can make a similar script doing the ls
job using Deno API’s!
#!/bin/bash deno-script
// In hello.js
for (const dirEntry of Deno.readDirSync("./")) {
console.log(dirEntry.name);
}
If we execute this script, we will have a error
error: Uncaught PermissionDenied: read access to "./", run again with the --allow-read flag
at unwrapResponse (rt/10_dispatch_json.js:25:13)
at sendSync (rt/10_dispatch_json.js:52:12)
at Object.readDirSync (rt/30_fs.js:105:16)
at file:///Users/jpi/dev/deno/deno-ls-like.js:3:29
This is were Deno shine! Deno will not let you implicitly have access to your directories. You need to explicitly ask the permission to Deno.
You could specify it in the shebang:
#!/bin/bash deno-script --allow-read
For more about Deno security go here.
To use kscript in a workflow without creating an additional script file, you can also use one of its supported modes for /inlined usage/.
The following modes are supported:
$ deno-script -i "console.log('hello', Deno.args[0])" JP
I can use pipe with it
ls | xargs -L 1 deno-script -i 'console.log(`file: ${Deno.args[0]}`)'
-L 1
of options of xargs
is to manage the execution of each stream pipe elements (see)
You could get the same result with -p
of --pipe
option
ls -la | deno-script -p "console.log('hello', Deno.args[0])"
You can manage several arguments
deno-script -i '
for (let arg of Deno.args) {
console.log(`arg: ${arg}`)
} ' arg1 arg2 arg3
echo '
console.log("hello world")
' | deno-script -
deno-script - <<"EOF"
console.log("It's a beautiful day!")
EOF
deno-script - <<"EOF"
import {hello} from "https://raw.githubusercontent.com/jiraguha/js-playgroud/master/hello-lib.ts"
hello("JP")
EOF
The colors.txt
used here is available here
deno-script --read-file-line "console.log(line.split(';')[0])" colors.txt
line
give access to each line
deno-script --read-file "console.log(lines[0])" colors.txt
lines
give access to all lines in iterable
deno-script --read-text-line "console.log(line.split(';')[1])" \
"Viridian; #40826D; 64; 130; 109; 161; 51; 38
Violet; #7F00FF; 127; 0; 255; 270; 100; 50
Ultramarine; #3F00FF; 63; 0; 255; 255; 100; 50
Turquoise; #40E0D0; 64; 224; 208; 174; 71; 56
Teal; #008080; 0; 128; 128; 180; 100; 25"
OR
cat colors.txt | xargs -0 deno-script --read-text-line "console.log(line.split(';')[1])"
cat colors.txt | xargs -0 deno-script --read-text "console.log(lines[3])"
TODO
Developed for 🦕 with ❤️
Author: jiraguha
Source Code: https://github.com/jiraguha/deno-script
#deno #nodejs #node #typescript #javascript