deno fmt をコードの中で呼ぶ

6086 ワード

deno のバージョン 1.25.3 時点の話

format.ts
const process = Deno.run({
  cmd: [Deno.execPath(), "fmt", "-"],
  stdin: "piped",
  stdout: "piped",
});

await process.stdin.write(
  new TextEncoder().encode(
    "const a: number =28; console.log(20,(3+(4      *  10))     )"
  )
);
process.stdin.writable.close();

console.log(new TextDecoder().decode(await process.output()));

実行コマンド

deno run --allow-run --allow-read ./format.ts

出力

const a: number = 28;
console.log(20, 3 + (4 * 10));

Deno.run を使っているため, --allow-run のフラグが必要. そのため Deno Deploy では動かない.
TypeError: Deno.run is not a function

Runtime API として使えるようになれば, --allow-run のフラグはつけなくて良くなりそう.

↓ 追加するかの議論