Deno入门

简介

终于等到Deno发布1.0了,很开心。A secure runtime for JavaScript and TypeScript.

安装

1
brew install deno

运行一个例子:

1
deno run https://deno.land/std/examples/welcome.ts
2
# Welcome to Deno 🦕

特点

Deno 内置了开发者需要的各种功能,不再需要外部工具。打包、格式清理、测试、安装、文档生成、linting、脚本编译成可执行文件等,都有专门命令。

Deno是一个采用Rust开发,使用V8的简单,现代且安全的Jav app.ts aScript和TypeScript运行时。

  1. 默认为安全模式。除非明确启用,否则没有文件,网络或环境的访问权限。
  2. 开箱即用的支持TypeScript。
  3. 只需要一个可执行文件。
  4. 具有内置的工具,例如依赖项检查器(deno info)和代码格式化工具(deno fmt)。
  5. 拥有一组保证能与Deno一起使用的经过审查(审核)的标准模块:deno.land/std

执行deno -hdeno help,就可以显示 Deno 支持的子命令。

一个简单的例子

一个可以从项目里遍历所有颜色的简单示例。

1
const startTime = new Date();
2
const result = new Set();
3
const fileList: Array<string> = [];
4
async function printFilesNames(path: string) {
5
  for await (const entry of Deno.readDir(path)) {
6
    const newPath = `${path}/${entry.name}`;
7
    fileList.push(newPath);
8
    if (entry.isFile && entry.name.match(/\.less/)) {
9
      // console.log(entry);
10
      const decoder = new TextDecoder("utf-8");
11
      const data = await Deno.readFile(newPath);
12
      const content = decoder.decode(data);
13
      const list = content.match(/#[A-Fa-f0-9]{1,6}|rgba?\([0-9,]+\)/) || [];
14
      list.forEach((color) => {
15
        result.add(color);
16
      });
17
    }
18
    if (entry.isDirectory) {
19
      await printFilesNames(newPath);
20
    }
21
  }
22
}
23
printFilesNames(Deno.args[0]).then(() => console.log(Array.from(result), `读取文件数${fileList.length}`, `消耗时间${new Date().getTime() - startTime.getTime()}ms`));

执行方式

1
deno run --allow-read app.ts /Users/abc/project/demo

常见问题

1、使用std的时候遇到报错

1
error: TS2339 [ERROR]: Property 'utime' does not exist on type 'typeof Deno'.
2
    await Deno.utime(dest, statInfo.atime, statInfo.mtime);
3
               ~~~~~
4
    at https://deno.land/std/fs/copy.ts:90:16

这种问题一般是因为std的版本和deno的版本不一致的问题,目前std没有最新的1.0,可以通过添加--unstable标志解决。

1
deno run --unstable example.ts

2、没有文件系统的访问权限

通过添加–allow-read标志解决。