foy: 轻量级的基于 nodejs 的通用 build 工具


声明:本文转载自https://my.oschina.net/ZaneYoung/blog/2979150,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

npm 的 scripts 下写的命令太多就很容易很乱,各种第三方轮子都只能解决一部分问题,总感觉不是很好用,想找个类似 make 的工具只能找到 jake, 可是 jake 的 API 太老,居然很多都不支持 promise, 代码也不多,就干脆自己造轮子了, 感觉效果还行。

特点:

  • 基于 promise 的任务和内置工具函数(fs/shell), 无缝支持 async/await
  • 类似于 shelljs 的跨平台 shell dsl, 人人都会写 shell
  • 易学易用,无需为写仅仅几个 build 命令而花费几个小时去寻找和学习第三方包
  • 很小的安装成本
    • foy: install size
    • gulp: install size
    • grunt: install size
  • 无缝和第三方支持 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

本文发表于2018年12月05日 19:00
(c)注:本文转载自https://my.oschina.net/ZaneYoung/blog/2979150,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1719 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1