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

zhangxianjie-cli

v1.0.1

Published

> CLI Target: 让我们的操作变得更高效、更规范!

Downloads

3

Readme

从零构建自己的 CLI 程序

CLI Target: 让我们的操作变得更高效、更规范!

功能

  1. 介绍面板mycli
  2. 创建项目mycli create <projectName>

步骤

  1. 初始化项目

    • package.json > bin 字段 声明命令指向的执行文件,像这样:
    {
      "name": "zhangxianjie-cli",
      "bin": {
        "mycli": "index.js"
      }
    }
    • 创建 index.js 文件,并添加以下代码:
    #!/usr/bin/env node // 告诉计算机用 node 解释器来执行下面的代码
    console.log("zhangxianjie-cli is running...");
    • 执行 npm link 命令,将 mycli 链接到全局环境中,这样我们就可以在任何地方使用 mycli 命令了
    • 测试命令:npx which mycli
  2. 实现用户与终端交互:

    • npm i commander -S 专门处理参数解析
    • npm i inquirer -S 提供交互输入方式如下拉框、输入框等
    • 测试代码如下:
     #!/usr/bin/env node
    
     import { program } from "commander";
     import inquirer from "inquirer";
    
     // 定义 create 命令
     program
     .command("create <project>")
     .description("创建一个新项目")
     .action((project) => {
         inquirer
         .prompt([
             {
             type: "input",
             name: "ProjectName",
             message: "请输入项目名称:",
             default: project || "my-project",
             },
         ])
         .then((answers) => {
             console.log("answers::: ", answers);
         });
     });
    
     //   解析 args、设置选项、监听事件
     program.parse();
  3. 实现模板下载功能

    • 安装npm i shelljs 用于帮助我们执行终端命令
    • 安装npm i ora 用于帮助我们展示加载动画
    • 安装npm i chalk 用于帮助我们美化文字输出
    • 最终代码如下
    import inquirer from "inquirer";
    import shell from "shelljs";
    import ora from "ora";
    import chalk from "chalk";
    
    const gitProjects = {
      js: {
        repository: "[email protected]:zhangxianjue-templates/javascript.git",
        desc: "创建原生 JS 项目",
      },
    };
    
    // 创建指令的回调函数
    export const createProjectAction = (project) => {
      inquirer
        .prompt([
          {
            type: "input",
            name: "ProjectName",
            message: "请输入项目名称:",
            default: project || "my-project",
          },
          {
            type: "list",
            name: "ProjectType",
            message: "请选择项目类型:",
            choices: Object.keys(gitProjects),
          },
        ])
        .then((answers) => {
          const { ProjectName, ProjectType } = answers;
          // 克隆项目
          const spinner = ora().start();
          const cloneCode = shell.exec(`git clone ${gitProjects[ProjectType].repository} ${ProjectName}`);
          if (cloneCode.code == 0) {
            spinner.succeed(chalk.green("项目克隆成功"));
            shell.rm("-rf", `${ProjectName}/.git`); // 删除 .git 文件夹
            shell.exit(0); // 退出 shell
          } else {
            spinner.fail(chalk.red("项目克隆失败"));
            shell.exit(1); // 0 表示成功,非 0 表示失败
          }
        });
    };