emmmm... 💪...
daily's Introduction
daily's People
daily's Issues
2. Writing Better Scripts
zx A tool for writing better scripts
使用方式
使用 zx 执行一个 nodejs 文件
zx a.js
使用自执行文件, 需要添加 shebang. #!/usr/bin/env zx
chmod +x ./a.js
./a.js
原理
原理比较简单。
- 和通常的 cli 一样,都需要添加 shebang
#!/usr/bin/env node
. - 添加常用的函数到 nodejs global。目前常用的有 查询函数$, 请求函数 fetch, 以及 cd 命令,之后可能会内建更多的常用指令吧。
使用场景
本在提供一个快速编写 scripts 的方式,所以内建了很多函数。
值得一提的是支持执行一个放在网路上的脚本。
zx https://raw.githubusercontent.com/google/zx/main/examples/backup-github.mjs
这样通过请求一个网络资源,来实现 git 的备份。
比如这个时候社区再来一个脚本共享的站点,比如 https://zx.org
, 大家共建的官方脚本比如 https://zx.org/shell/backupGit.js
。
不过这样比较危险,因为通过执行一个外部的脚本很容易出现安全问题~~
比对
比对 jiti, 两者其实都是内建一些函数来实现的。
jiti 内建的是 ts 的 compile 以及 esm 的处理。
zx 内建的是 global 的函数。
整体上都是想用相对简洁的方式提供给使用者更大限度的便利。这就是命令行工具的初衷吧~
1. Runtime Typescript and ESM support for Node.js
- jiti Runtime Typescript and ESM support for Node.js
原理:通过 jiti x.ts
的执行,本质上还是使用 babel 处理 ts,es module 则是使用额外的处理来实现。
可以使用的场景: demo场景下面,单文件的快速执行。
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.