Skip to content

blankPen/Pok

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

❯ Pok

version travis downloads

Simple and Elegant project creator
>_ Easier for create template projects with Git▌


❯ 安装

npm install pok -g

❯ 使用

在终端中使用

# initial a project by pok-template
pok ssh://xxx.git

在NodeJS中使用

const pok = require('pok')

pok.create({
    remote: 'ssh://xxx.git',
    branch: 'master'
})

❯ 创建一个Pok模板项目

初始化模板

pok git@github.com:blankPen/pok-example.git

配置pok.config.js

module.exports = function creator(ctx) {
    return {
        name: 'pok-template',
        // 在模板初始化前执行,根据setup配置的参数进行项目生成
        async setup() {
            // 通过prompts与用户进行交互获取用户配置项
            const res = await ctx.prompts([
                {
                    name: 'projectName',
                    type: 'text',
                    message: '项目名:',
                    initial: defaultProjectName,
                    onState: (state) => (String(state.value).trim() || defaultProjectName)
                },
            ], {
                onCancel() {
                    process.exit(1);
                }
            });
            let sourceDir = 'normal';
            if (res.needRematch) sourceDir = 'rematch';

            // 返回setup所需配置参数
            return {
                autoInstall: true,
                sourceDir: sourceDir,
                outputDir: res.projectName,
                env: res
            };
        },
        // 项目创建结束后回调,一般用于打印结束日志
        end() {
            console.log(ctx.chalk.green(`\n  项目创建完成,快速启动: `))
            console.log(`    cd ${ctx.setupConfig.outputDir}`)
            console.log(`    npm run dev`)
        }
    }
}

setup配置项

  • sourceDir, 模板代码所在的文件夹,默认值:./
  • outputDir, 要生成模板代码的输出目录,当未进行配置时 pok 会主动询问用户要输出的目录地址
  • autoInstall, 是否自动执行 npm install 安装项目依赖,默认值:false
  • env, 模板环境参数,配置的参数将用于hbs模板生成,具体参考:https://handlebarsjs.com/

Context

为了方便模板创建提供的上下文,内置一些常用库

  • setupConfig, setup() 返回的结果,方便在end()中使用
  • prompts Lightweight, beautiful and user-friendly interactive prompts
  • shelljs 🐚 Portable Unix shell commands for Node.js
  • chalk 🖍 Terminal string styling done right

❯ Licence

MIT

About

Simple and Elegant project creator

Resources

License

Stars

Watchers

Forks

Packages

No packages published