Enhanced scripting support for JavaScript/TypeScript with Deno

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:

  • It can import modules from any location on the web,
  • It is secure by default. Imported module can run in sandbox.
  • It is Supports TypeScript out of the box.
  • It is much more that Node

Requirement

  • Os: Mac, linux
  • Installing deno
  • Make sure that Deno is on the bin path. If you 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

Installation

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"  

Script Input Modes

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.

Interpreter Usage

To use Deno as interpreter for a script:

  • Just create a script just point todeno-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 lsjob 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.

Inlined Usage

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:

  • Directly provide a js scriptlet as argument
$ 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 -pof --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
  • Pipe a js snippet into Deno and instruct it to read from stdin by using - as script argument
echo '
console.log("hello world")
' | deno-script -
  • Using heredoc (preferred solution for inlining) which gives you some more flexibility to also use single quotes in your script:
deno-script - <<"EOF"
console.log("It's a beautiful day!")
EOF
  • Since the piped content is considered as a regular script it can also have dependencies
deno-script - <<"EOF"
  import {hello} from "https://raw.githubusercontent.com/jiraguha/js-playgroud/master/hello-lib.ts"
  hello("JP")
EOF

Read Usage

The colors.txt used here is available here

  • read a file line by line
 deno-script --read-file-line "console.log(line.split(';')[0])" colors.txt

line give access to each line

  • read a file as an all
 deno-script --read-file "console.log(lines[0])" colors.txt

lines give access to all lines in iterable

  • read a text line by line
 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])" 
  • read a text as an all
cat colors.txt | xargs -0 deno-script --read-text "console.log(lines[3])" 

Roadmap

TODO

Developed for 🦕 with ❤️

Download Details:

Author: jiraguha

Source Code: https://github.com/jiraguha/deno-script

#deno #nodejs #node #typescript #javascript

Enhanced scripting support for JavaScript/TypeScript with Deno
6.45 GEEK