npm 的 scripts 下寫的命令太多就很容易很亂,各類第三方輪子都只能解決一部分問題,總感受不是很好用,想找個相似 make 的工具只能找到 jake, 但是 jake 的 API 太老,竟然不少都不支持 promise, 代碼也很少,就乾脆本身造輪子了, 感受效果還行。javascript
安裝java
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)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'`.
})
而後就能夠運行任務了git
# 安裝在本地 node_modules 目錄下
npx foy build
npx foy build1
npx foy task
# 安裝在全局
foy build
foy build1