资讯专栏INFORMATION COLUMN

如何结合npm做一个前端脚手架

pf_miles / 3441人阅读

摘要:背景需求在日常开发中,随着项目越来越多,能重复利用的代码就越多,急需一套基本的模板来初始化项目,把代码放到自己的或上,每次又得找到地址重新一下,很麻烦期望的结果。。。

背景需求:在日常开发中,随着项目越来越多,能重复利用的代码就越多,急需一套基本的模板来初始化项目,把代码放到自己的gitlab或github上,每次又得找到地址重新clone一下,很麻烦
期望的结果:XXX init 。。。一行命令解决
步骤:
1、申请一个npm账号,https://www.npmjs.com/
2、写一个npm项目
package.json:

    {
  "name": "windssr",
  "version": "1.0.8",
  "description": "a tool service for react-ssr",
  "main": "index.js",
  "bin": {
    "windssr": "index.js"
  },
  "author": "windseek",
  "license": "ISC",
  "dependencies": {
    "chalk": "^2.4.2",
    "co": "^4.6.0",
    "co-prompt": "^1.0.0",
    "commander": "^2.20.0"
  },
  "scripts": {
    "test": "test"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Windseek/react-ssr-cli.git"
  },
  "keywords": [
    "react",
    "react-ssr",
    "wind-ssr",
    "react-ssr-cli"
  ],
  "bugs": {
    "url": "https://github.com/Windseek/react-ssr-cli/issues"
  },
  "homepage": "https://github.com/Windseek/react-ssr-cli#readme"
}

init.js

"use strict"
const exec = require("child_process").exec
const co = require("co")
const prompt = require("co-prompt")
const config = require("./template.json")
const chalk = require("chalk")

module.exports = () => {
 co(function *() {
    // 处理用户输入
      let tplName = yield prompt("Template name (you can input one like react, vue, angular): ")
      let projectName = yield prompt("Project name: ")
      let gitUrl,branch;
      console.log(config.tpl);
    if (!config.tpl[tplName]) {
        console.log(chalk.red("
 × Template does not support!"))
        process.exit()
    }
    gitUrl = config.tpl[tplName].url
    branch = config.tpl[tplName].branch

    // git命令,远程拉取项目并自定义项目名
    let cmdStr = `git clone ${gitUrl} ${projectName} && cd ${projectName} && git checkout ${branch}`

    exec(cmdStr, (error, stdout, stderr) => {
      if (error) {
        console.log(error)
        process.exit()
      }
      console.log(chalk.green("
 √ Generation completed!"))
      console.log(`
 cd ${projectName} && npm install 
`)
      process.exit()
    })
  })
}

index.js

#!/usr/bin/env node --harmony

"use strict"

process.env.NODE_PATH = __dirname + "/../node_modules/"

const program = require("commander")

program
    .version(require("./package").version)

program
    .usage("")
program
    .command("init")
    .description("Generate a new project")
    .alias("i")
    .action(() => {
        require("./init.js")()
    })

program.parse(process.argv)

template.json:

{"tpl":{"react":{"url":"https://github.com/Windseek/reactssr.git","branch":"master"}}}

写好了需要发布一下喽:
1、npm login,填写用户名,密码
2、修改package.json里version
3、npm publish
4、去https://www.npmjs.com/ 上看看成功没有

相关说明:
1、package.json里定义bin的位置
2、index里定义bin命令指定执行的代码
3、init里执行shell,去git上拿代码模板

安装和初始化:
1、npm install windssr -g
2、windssr init
3、选择要使用的模板,react,angular和vue

如果觉的对你有用的话,支持一下哈

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/106054.html

相关文章

  • 前端核心工具:yarn、npm、cnpm三者如何优雅的在一起使用 ?

    摘要:由于文件中版本号的特点,下面三个版本号在安装的时候代表不同的含义。安装版本统一为了防止拉取到不同的版本,有一个锁定文件记录了被确切安装上的模块的版本号。 showImg(https://segmentfault.com/img/bVbs8Rg?w=1920&h=1080); 一位用不好包管理器的前端,是一个入门级前端,一个用不好webpack的前端,是一个初级前端 三个包管理器是可以一...

    sihai 评论0 收藏0
  • 前端核心工具:yarn、npm、cnpm三者如何优雅的在一起使用 ?

    摘要:由于文件中版本号的特点,下面三个版本号在安装的时候代表不同的含义。安装版本统一为了防止拉取到不同的版本,有一个锁定文件记录了被确切安装上的模块的版本号。 showImg(https://segmentfault.com/img/bVbs8Rg?w=1920&h=1080); 一位用不好包管理器的前端,是一个入门级前端,一个用不好webpack的前端,是一个初级前端 三个包管理器是可以一...

    plus2047 评论0 收藏0
  • 前端核心工具:yarn、npm、cnpm三者如何优雅的在一起使用 ?

    摘要:由于文件中版本号的特点,下面三个版本号在安装的时候代表不同的含义。安装版本统一为了防止拉取到不同的版本,有一个锁定文件记录了被确切安装上的模块的版本号。 showImg(https://segmentfault.com/img/bVbs8Rg?w=1920&h=1080); 一位用不好包管理器的前端,是一个入门级前端,一个用不好webpack的前端,是一个初级前端 三个包管理器是可以一...

    HitenDev 评论0 收藏0

发表评论

0条评论

pf_miles

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<