自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一經(jīng)開源就爆了!谷歌這個(gè)腳本工具注定要火

開源
最近谷歌開源了一個(gè)能夠幫助開發(fā)者快速編寫腳本的工具——ZX,短短幾天就登上了Github熱榜。

 大家都知道Bash很好用,但是在編寫腳本時(shí),人們通常會(huì)選擇一種更方便的編程語(yǔ)言,比如JavaScript,但是Node.js庫(kù)在使用之前還需要許多額外的操作,整體來說還是不夠方便,最近谷歌開源了一個(gè)能夠幫助開發(fā)者快速編寫腳本的工具——ZX,短短幾天就登上了Github熱榜。

ZX的安裝十分簡(jiǎn)單: 

  1. npm i -g zx 

接下來,你需要將你的腳本編寫在帶有.mjs擴(kuò)展名的文件中,以便能夠await在頂層使用。如果你喜歡.js擴(kuò)展名,可以將腳本包裝為void async function () {...}()。

將以下shebang添加到zx腳本的開頭: 

  1. #!/usr/bin/env zx 

現(xiàn)在,你將能夠像這樣運(yùn)行腳本: 

  1. chmod +x ./script.mjs  
  2. ./script.mjs 

或通過zx可執(zhí)行文件:

  1. zx ./script.mjs 

常用命令舉例

使用child_process包中提供的exec函數(shù)可以把字符串當(dāng)做命令執(zhí)行,并返回Promise<ProcessOutput>對(duì)象。 

  1. let count = parseInt(await $`ls -1 | wc -l`)  
  2. console.log(`Files count: ${count}`) 

例如,要并行上傳文件: 

  1. let hosts = [...]  
  2. await Promise.all(hosts.map(host =>  
  3.   $`rsync -azP ./src ${host}:/var/www`    
  4. )) 

如果執(zhí)行的程序返回一個(gè)非零的退出代碼, 將會(huì)拋出ProcessOutput對(duì)象: 

  1. try {  
  2.   await $`exit 1`  
  3. } catch (p) {  
  4.   console.log(`Exit code: ${p.exitCode}`)  
  5.   console.log(`Error: ${p.stderr}`)  

ProcessOutput 

  1. class ProcessOutput {  
  2.   readonly exitCode: number  
  3.   readonly stdout: string  
  4.   readonly stderr: string  
  5.   toString(): string  

cd(),更改當(dāng)前工作目錄 

  1. cd('/tmp')  
  2. await $`pwd` // outputs /tmp 

fetch(),對(duì)node-fetch包的包裝: 

  1. let resp = await fetch('http://wttr.in')  
  2. if (resp.ok) {  
  3.   console.log(await resp.text())  

question(),對(duì)readline包的包裝: 

  1. type QuestionOptions = { choices: string[] }  
  2. function question(query?: string, options?: QuestionOptions): Promise<string> 

用法: 

  1. let username = await question('What is your username? ')  
  2. let token = await question('Choose env variable: ', { 
  3.   choices: Object.keys(process.env) 
  4. }) 

sleep(),setTimeout函數(shù)的包裝。

  1. function sleep(ms: number): Promise<void> 

用法: 

  1. await sleep(1000) 

chalk包,該包直接可用無需導(dǎo)入內(nèi)部腳本:

  1. console.log(chalk.blue('Hello world!')) 

執(zhí)行遠(yuǎn)程腳本,如果zx可執(zhí)行文件的參數(shù)以開頭https://,則將下載并執(zhí)行該文件。

  1. console.log(chalk.blue('Hello world!')) 

最后,附上ZX在Github上的項(xiàng)目地址:https://github.com/google/zx  

 

責(zé)任編輯:龐桂玉 來源: 開源前線
相關(guān)推薦

2024-10-29 15:29:06

2022-05-30 07:36:07

Python腳本函數(shù)

2024-10-28 12:54:36

2022-08-02 20:21:00

SaaS驅(qū)動(dòng)PLG

2021-10-29 06:56:15

Python腳本解釋器

2021-04-12 13:36:59

開源技術(shù) 工具

2019-04-15 14:17:28

iTunes蘋果macOS

2023-07-22 13:44:26

AI代碼

2021-10-09 18:26:59

二叉樹多叉樹搜索

2019-12-24 09:49:02

微軟英語(yǔ)瀏覽器

2023-08-01 14:07:05

模型AI

2021-05-13 10:20:44

谷歌工具技術(shù)

2023-11-14 09:25:00

AI訓(xùn)練

2021-01-27 11:36:34

代碼開發(fā)工具

2023-03-02 11:44:08

AI技術(shù)

2015-12-30 10:36:59

2020-10-27 10:58:07

Linux內(nèi)核操作系統(tǒng)

2019-03-27 15:29:55

智慧園區(qū)華為

2023-04-03 09:56:22

模型系統(tǒng)

2023-05-11 11:46:58

AI翻唱
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)