1597213440
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
1597213440
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
1596666360
TypeScript provides a command-line utility tsc
that compiles (transpiles) TypeScript files (_.ts_
) into JavaScript. However, the tsc
compiler (short for TypeScript compiler) needs a JSON configuration file to look for TypeScript files in the project and generate valid output files at a correct location.
When you run tsc
command in a directory, TypeScript compiler looks for the tsconfig.json
file in the current directory and if it doesnβt find one, then it keeps looking up the directory tree until it finds one. The directory where the tsconfig.json
is located is considered as the root of the project.
You can manually provide a path to the tsconfig.json
file using --project
or -p
command-line flag. This file doesnβt need to have the tsconfig.json
filename if you are using this flag with the exact file path. However, you can also provide the directory path that contains the tsconfig.json
file.
$ tsc -p /proj/x/tsconfig.dev.json
If the TypeScript compiler fails to locate this configuration file, you would get an error. But you can provide settings enlisted in this file through the equivalent command-line options which we will cover in the next lesson.
tsconfig.json
So what does this file contain and what exactly it controls?
{
"files": [
"src/lib/person.ts",
"src/lib/student.ts",
"src/main.ts"
],
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./dist/development"
}
}
The tsconfig.json
file is a standard JSON file, however, it supports JSON5 specifications, so you can use comments, single quotes, and more. It contains some root-level options and some compiler options. The root-level options are options that are outside of the compilerOptions
object, so in the above example, files
is a root-level option.
The root-level options control how the project is presented to the TypeScript compiler, such as which TypeScript files to consider for the compilation. The compiler options contain settings for the TypeScript compiler such as where to output the compiled JavaScript files in the project directory.
These options control how the project is presented to the TypeScript compiler for the compilation and static type analysis. These options must be kept outside compilerOptions
object of the tsconfig.json
file.
files
The files
array contains the location of the TypeScript files to consider for the compilation. These can be either relative paths or absolute paths on the disk. A relative path is located relative to the location of the tsconfig.json
file (AKA root of the project).
/projects/sample/
βββ a.ts
βββ src/
| βββ b.ts
| βββ c.ts
| βββ ignore.ts
| βββ lib/
| βββ d.ts
| βββ e.ts
βββ tsconfig.json
Letβs consider that we have the above directory structure in our project. As you can see, the TypeScript files (.ts) are located in multiple directories. We want to compile all the .ts
files except the ignore.ts
file. Hence we would provide relative paths of these files in the files
options of tsconfig.json
.
// tsconfig.json
{
"files": [
"a.ts",
"src/b.ts",
"./src/c.ts",
"src/lib/d.ts",
"./src/lib/e.ts"
]
}
You can also provide absolute paths of these files but relative paths are most recommended since they would be consistent on all the systems. All the relative paths are resolved against the path of tsconfig.json
file in the project. You can optionally provide ./
or ../
prefix to locate the file.
Since we havenβt provided any compilerOptions
values, all the default values for the compiler options are used which we will talk about in a bit. The TypeScript compiler compiles these files and outputs the JavaScript with .js
extension by keeping the same file name as the individual input file.
The TypeScript compiler also preserves the original file path, hence the .js
output file will be generated where the input file was in the directory structure. When you run the tsc
command from the directory where your tsconfig.json
file is located, you are going to see the result below.
/projects/sample/
βββ a.js
βββ a.ts
βββ src/
| βββ b.js
| βββ b.ts
| βββ c.js
| βββ c.ts
| βββ ignore.ts
| βββ lib/
| βββ d.js
| βββ d.ts
| βββ e.js
| βββ e.ts
βββ tsconfig.json
As you can see, the TypeScript compiler compiled all the input TypeScript files listed inside files
array of tsconfig.json
. You canβt see the ignore.js
file since ignore.ts
file was not included in the files
array.
The directory where the tsconfig.json
file is located is considered as the root of the project, AKA the root directory. You can also include a file from outside this root directory, such by including "../x.ts"
in the files
array where x
would be in the parent directory of the root directory. Since the TypeScript compiler preserves the input file path, it will generate x.js
in the parent directory of the root directory.
include & exclude
The files
option is great when you have relatively few files to work with. But when your project is big and contains hundreds of source files located in a nested directory structure, then handpicking file paths is not practical.
To solve this issue, we can use include
option. This option is just like files, however, we can optionally provide glob patterns to locate input files. The exclude
options behave the same, except it removes the files from the compilation that may have been included by the include
option.
// tsconfig.json
{
"include": [
"a.ts",
"src/**/*.ts"
],
"exclude": [
"./**/*/ignore.ts"
]
}
In the above tsconfig.json
, we have removed the files
option and added include
which adds a.ts
file from the root directory and all the .ts
file from the src
directory. Since this would also include any ignore.ts
from the src
directory, we have provided the exclude
option that excludes any ignore.ts
file from the compilation if located inside the src
directory.
When we run the tsc
command now, results wonβt be any different since the files considered for the compilation both in the previous example and this example are the same.
/projects/sample/
βββ a.js
βββ a.ts
βββ src/
| βββ b.js
| βββ b.ts
| βββ c.js
| βββ c.ts
| βββ ignore.ts
| βββ lib/
| βββ d.js
| βββ d.ts
| βββ e.js
| βββ e.ts
βββ tsconfig.json
The TypeScript compiler automatically excludes files from the "node_modules"
, "bower_components"
, "jspm_packages"
and "<outDir>"
directories, where <outDir>
is the value of outDir
compiler-option provided by you. This prevents any .ts
file from these directories getting included in the compilation process by accident.
#nodejs #typescript #deno #programming #javascript #deno
1593152871
What is Deno?
Deno is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on productivity. It was announced by Dahl in 2018 during his talk β10 Things I Regret About Node.jsβ
#deno #node #javascript #typescript #developer
1609724218
Deno is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on productivity.
#typescript #javascript #deno #developer #programming
1589771038
Pengenalan deno, dan membuat api sederhana dengan deno
00:06 Introduction Deno
01:49 Instalasi
03:52 Membuat Hello World
04:38 Membuat Server
07:50 Membuat REST API
#deno #node #javascript #typescript