better shellscriptとしてのdeno
はじめに
denoで一枚ペラのTypeScriptが動きます。 better shellscriptとしてのdenoを考えてみます。
denoのインストール
どこからかdenoをインストールします。 ArchLinuxならyayでインストールできます。
yay -S deno
$ deno --versiondeno 1.32.5 (release, x86_64-unknown-linux-gnu)v8 11.2.214.9typescript 5.0.3
他のディストリの場合は公式のインストール方法を参照。
TypeScriptインタプリタとしてのdeno
denoを引数なしで起動するとTypeScriptのREPLが起動します。
$ denoDeno 1.32.5exit using ctrl+d, ctrl+c, or close()REPL is running with all permissions allowed.To specify permissions, run `deno repl` with allow flags.> console.log('hello deno')hello denoundefined> function add(a: int, b: int): int { return a+b }undefined> add(1,2)3> add("a", 2)"a2"
型宣言も書けますが、特に怒ってくれないのであまり意味がないようです。
denoでTSファイルの実行
deno run
run
サブコマンドでローカルのTSファイルを実行できます。
console.log('Hello, deno!')
$ deno run hello.tsHello, deno!
さらにURLを指定することで、TSファイルをダウンロードしてその場で実行できます。
$ deno run https://deno.land/std/examples/welcome.tsWelcome to Deno!
私のブログでもstaticファイルとして置いています。ファイル一覧は /static/deno で確認できます。
$ deno run https://a.conao3.com/static/deno/hello-world.v1.tsHello, deno!
権限管理
denoはnodeの反省を生かしてスクリプトに対して与える権限をコントロールすることができます。 権限を与えないとファイルアクセスやネットワークアクセスができないようになっています。
console.logは権限を与えなくても実行できます。
console.log('Hello, deno!')
$ deno run hello.tsHello, deno!
しかし、ファイルアクセスはできません。
const decoder = new TextDecoder("utf-8");const data = await Deno.readFile("hello.ts");console.log(decoder.decode(data));
実行すると権限を追加で与えるか聞いてきます。
$ deno run readfile.ts┌ ⚠️ Deno requests read access to "hello.ts".├ Requested by `Deno.readFile()` API.├ Run again with --allow-read to bypass this prompt.└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) >>
n
を入力すると拒否でき、 y
を入力すると許可できます。
> n┌ ⚠️ Deno requests read access to "hello.ts".❌ Denied read access to "hello.ts".error: Uncaught PermissionDenied: Requires read access to "hello.ts", run again with the --allow-read flagconst data = await Deno.readFile("hello.ts"); ^ at async Object.readFile (ext:deno_fs/30_fs.js:684:18) at async file:///home/conao/dev/tmp/deno/readfile.ts:2:14
> y┌ ⚠️ Deno requests read access to "hello.ts".✅ Granted read access to "hello.ts".console.log('Hello, deno!')
便利。
とはいえ、自分のスクリプトの場合 -A
を与えることにはなりそうです。
まとめ
いろいろ単発のサンプルスクリプトを書こうと思ったのですが、永遠に公開できなくなりそうなのでここまでにします。
このブログにdenoのスクリプト置くの結構便利そうなので、また何か書けたら書きます。