1.0.23 • Published 6 years ago

@bfunjs/bfun-cli v1.0.23

Weekly downloads
47
License
MIT
Repository
github
Last release
6 years ago

简介

bfun-cli 是一个通用的脚手架,可用来快速自由的搭建各种项目。

文档

# 安装
npm i bfun-cli -g

# 配置项目模板库,第一次安装完成后至少需要执行一次 bfun config
# 默认:--dir 当前目录(bfun --config)
# 参考:只需执行一次即可,后续执行会覆盖,如无权限,请使用 sudo bfun config
bfun config --dir <dir name>
bfun config --git <git repo>
bfun config --npm <npm repo>

# 新建一个文件夹并进入此文件夹
mkdir app
cd app

# 初始化
bfun init

模板

  • 模板简介

    模板入口为:bfun.js,只要你的git、npm或是dir内包含bfun.js即会调用;

module.exports = {
    minVersion: '1.0.1', // 最低需要bfun-cli的版本,必填
    maxVersion: '2.0.0', // 最高支持bfun-cli的版本,必填
    async initial({ inquirer }) {
        const data = await inquirer.prompt([]);

        return {
            type: 'npm',
            url: 'https://registry.npm.taobao.org/@bfunjs/easy-doge/1.0.0',
            skipCompile: false, // default false
            context: {
                ...data,
                custom: 'bfun'
            }
        };
    },
    async created() {
        console.log('创建完毕!');
    },
    async command({ program }) {
        // 注册你的命令
    }
};
  • 生命周期

    生命周期为:initial => compile => created

  • initial({ inquirer })

    完全支持inquirer,你需要在initial中进行询问,并返回需要初始化的对象

参数描述
type类型,可用值类型为:npm、git、dir
url如果type是git,值则为git地址;如果是type是npm,值则为npm地址;如果是type是dir,值则为文件夹路径
skipCompile跳过初始化,默认为false,当你需要在created自己实现模板编译时可设置为true,设置为true时,将不再执行compile函数
contextcompile时会传入,用于模板赋值(例如:{name: '项目名'}会将模板中的{{name}}赋值成为“项目名”)
  • compile(fileName, context) 每个文件在编译时均会调用,如果不设置则会使用默认模板引擎(handlebars)编译模版;如果你习惯使用其它模板引擎,可以在此处执行自己的逻辑,为每个文件进行处理,下面是默认的compile函数。
// 默认compile函数
function compile(fileName, context) {
    const content = fs.readFileSync(fileName).toString();
    const result = handlebars.compile(content)(context);
    fs.writeFileSync(fileName, result);
};
  • created()

    当创建完后会调用,如果skipCompile设置为true,你可以在这里执行自己的逻辑

  • command({ program })

    注册自己的命令,如bfun publish,完全支持program库