npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eno-cli

v1.0.6

Published

commander文档:[https://github.com/tj/commander.js/blob/HEAD/Readme_zh-CN.md]()

Downloads

9

Readme

搭建脚手架

一、创建指令

commander文档:https://github.com/tj/commander.js/blob/HEAD/Readme_zh-CN.md

为简化使用,Commander 提供了一个全局对象:

const { program } = require("commander")

配置命令使用.command()或者.addCommand()

.command参数:

第一个参数:命令名称

第二个参数:命令参数 必选参数使用<>,可选参数使用[]

例如:

program
  .command('create <project> [destination]') //命令名
  .description('clone a repository into a newly created directory') //命令描述
  .action(createProjectTemplate);//命令执行函数,传入参数project, destination
1.1 下载模板执行函数

下载目标模板,使用download-git-repo,下载一个git仓库到目标文件夹,并带选项和回调

const download = require('download-git-repo')

第一个参数:git地址 ,地址前需要加上direct:,例如direct:url

第二个参数:目标文件名

第三个参数:下载参数

clone:默认false,及下载项目,true,使用 git clone克隆项目

第四个参数:callback,执行错误的回调函数

1.2 util.promisify

此方法可将有回调函数的方法转化为 promise格式进行调用,因为我们需要在执行完下载指令之后执行其他指令,所以将下载方法promise化

const {promisify} = require('util')
const download = promisify(require('download-git-repo'))
1.3下载依赖包

下载依赖包,需要node打开一个额外的进程,node的核心模块 child_process中的spwan方法可以执行命令并开启一个新的进程。

该方法接收3个参数:

第一个参数:command 要运行的命令,npm或者yarn

第二个参数:字符串参数列表,如果你要执行 npm install命令,那么该数组就是 ['install']。如果你要执行 npm install axios,那么这个数组就应该为 ['install', 'axios']

第三个参数:是一个配置对象,常用的是cwd,子进程的当前工作目录

1.3.1 开启一个进的进程
const { spawn } = require("child_process")

const spawnChildProcess = (...args) => {
  return new Promise((resolve) => {
    const childProcess = spawn(...args)
    childProcess.stdout.pipe(process.stdout) // 子进程的输出 pipe 给父进程的输出
    /**
	或者通过监听 data 事件来获取结果
	childProcess.stdout.on('data', (data) => {
      		// console.log(data);
    	});
    */
    childProcess.on('close', () => {
      resolve() 
      console.log("子进程关闭");
  
    })
  })
}
1.3.2 下载依赖包

注意:在执行npm(yarn)时,windows系统会将转化为为npm.cmd(yarn.cmd),但是,在linux和Mac系统上,是没有这个转化的,process.platform可以判断当前的操作系统,os.platform方法也可以

const createProjectTemplate= async (project) => {
  try {
    //1.克隆项目
    await download('direct:git地址', project, { clone: true }) 

    // 2. 执行npm install命令安装依赖
    const command = process.platform === 'win32' ? 'npm.cmd' : 'npm'
  
    await spawnChildProcess (command, ['install'], { cwd: `./${project}` })

    //3.安装完成提示
    console.log(chalk.blue(`$ cd ${project}`))
    console.log(chalk.blue(`$ npm run serve`))

  } catch (err) {
    console.log(err);
  }
}
1.4 操作提示

依赖包下载完成后,提示进入目标文件并执行项目

变色文字使用chalk包

  //3.安装完成提示
    console.log(chalk.blue(`$ cd ${project}`))
    console.log(chalk.blue(`$ npm run serve`))

二、解析指令

.parse(),解析指令

第一个参数:需要解析的字符串数组,也可以省略使用process.argv

第二个参数:如果参数遵守约定不同于node,可传递from选项指定遵循的脚本

  • node: 默认值,argv[0]是应用,argv[1]是要跑的脚本,后续为用户参数
  • electron:argv[1]根据 electron 应用是否打包而变化
  • user:来自用户的所有参数
program.parse(process.argv); // 指明,按 node 约定
program.parse(); // 默认,自动识别 electron
program.parse(['-f', 'filename'], { from: 'user' })
program
  .command('create <project> [destination]') //命令名
  .description('clone a repository into a newly created directory') //命令描述
  .action(createProjectAction);
program.parse(process.argv)