npm 的 scripts 下寫的命令太多就很容易很亂,各種第三方輪子都只能解決一部分問題,總感覺不是很好用,想找個類似 make 的工具只能找到 jake, 可是 jake 的 API 太老,居然很多都不支持 promise, 代碼也不多,就乾脆自己造輪子了, 感覺效果還行。
特點:
- 基於 promise 的任務和內置工具函數(fs/shell), 無縫支持 async/await
- 類似於 shelljs 的跨平臺 shell dsl, 人人都會寫 shell
- 易學易用,無需爲寫僅僅幾個 build 命令而花費幾個小時去尋找和學習第三方包
-
很小的安裝成本
- 無縫和第三方支持 promise 的工具包整合,不需要封裝成插件就能用
使用:
安裝
yarn add -D foy # or npm i -D foy
# Or Install globally with
yarn add -g foy # or npm i -g foy
在項目根目錄下增加一個 Foyfile.js (或者 Foyfile.ts, 需要安裝 ts-node)
import { task, desc, option, strict, fs } from 'foy'
task('build', async ctx => {
await ctx.exec('tsc')
})
desc('Build ts files with tsc')
option('-w, --watch', 'watch file changes')
strict() // This will throw an error if you passed some options that doesn't defined via `option()`
task('build2', async ctx => {
await ctx.exec(`tsc ${ctx.options.watch ? '-w' : ''}`)
})
task('task', async ctx => {
await fs.rmrf('/some/dir/or/file') // Remove directory or file
await fs.copy('/src', '/dist') // Copy folder or file
let json = await fs.readJson('./xx.json')
await ctx.env('NODE_ENV', 'production')
await ctx.cd('./src')
await ctx.exec('some command') // Execute an command
let { stdout } = await ctx.exec('ls', { stdio: 'pipe' }) // Get the stdout, default is empty because it's redirected to current process via `stdio: 'inherit'`.
})
然後就可以運行任務了
# 安裝在本地 node_modules 目錄下
npx foy build
npx foy build1
npx foy task
# 安裝在全局
foy build
foy build1