Git Product home page Git Product logo

cli-demo's Introduction

开发cli工具流程

初始化项目

新建开发目录,进入目录后使用npm初始化项目,填写项目基本信息。

mkdir my-cli
cd my-cli
npm init

打开package.json文件,加入bin参数

{
  "name": "my-cli", // 项目名称
  "version": "1.0.0",
  "description": "my-cli",
  "type":"module",  // 为了支持node里使用import
  "main": "index.js",
  "bin": { // 最重要字段,有这个,终端才可以执行我们的命令
    "my-cli": "./bin/index.js" // my-cli表示我们的cli程序名称,路径是我们的执行代码文件
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

新建代码文件

一般我们新建一个bin目录,里面index.js是我们的执行代码文件。(可以随便建,和package.json里bin定义的一样即可)

// ./bin/index.js
#!/usr/bin/env node
console.log("cli-demo");

注意,第一行代码#!/usr/bin/env node非常重要,定义了代码执行程序为node,不然可能会以文本形式打开文件。

本地安装

在项目跟目录,执行npm install -g,即可把本地开发的cli项目全局安装到本地,方便测试。

开发cli常用辅助包

cli-demo's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.