繁体   English   中英

如何发布控制 CLI 关键字的 NPM 包

[英]How Publish an NPM Package that Controls a CLI Keyword

我不确定搜索这个问题的正确词汇是什么,但我可以提到几个实现我试图选择的模式的包:

  1. shx
  2. cross-env
  3. npm-run

一旦你安装了这些包中的任何一个,它们就可以通过 CLI 获得。 例如,在我安装cross-env作为新项目的依赖项后,我可以在我的package.json创建一个 npm 脚本,类似于"start": "cross-env NODE_ENV=production webpack"

我检查了这些项目的package.json文件,它们都使用bin字段,但是如果我初始化一个本地项目( npm init )并添加一个 bin 字段,它在我的命令行上无法识别它,即使在运行npm install .

那么我怎样才能获得同样的功能呢? 如果问题很清楚,或者我是否应该添加一些其他信息,也请告诉我。

我的package.json如下:

{
  "name": "sloth-cli",
  "version": "1.0.0",
  "description": "a dead simple Chron cli tool for Node",
  "bin": {
    "sloth": "node ."
  },
  "main": "index.js",
  "scripts": {
    "start": "node ."
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Vandivier/sloth.git"
  },
  "keywords": [
    "chron"
  ],
  "author": "John Vandivier",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Vandivier/sloth/issues"
  },
  "homepage": "https://github.com/Vandivier/sloth#readme"
}

我最终找到了答案并让sloth-cli工作。 package.json bin确实是关键,但是在定义 bin 之后,我们必须在本地运行npm link

此外, bin的内容不能包含典型的 npm 脚本语法。 它必须引用一个设置为在 CLI 上下文中执行的文件。 这可以很简单,就像在前面加上#! /usr/bin/env node #! /usr/bin/env node位于 javascript 文件的顶部。

一旦一个包被发布到 npm,那些将它安装为依赖项的人就不需要运行npm link Npm 处理那部分。 以下是sloth-cli的当前工作package.json

{
  "name": "sloth-cli",
  "version": "1.0.2",
  "description": "a dead simple Chron cli tool for Node",
  "bin": {
    "sloth": "./index.js"
  },
  "main": "index.js",
  "scripts": {
    "immediately": "sloth .1 \"npm run say-hello\" true true",
    "start": "sloth .1 \"npm run say-hello\"",
    "say-hello": "node hello.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Vandivier/sloth.git"
  },
  "keywords": [
    "chron"
  ],
  "author": "John Vandivier",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Vandivier/sloth/issues"
  },
  "homepage": "https://github.com/Vandivier/sloth#readme",
  "dependencies": {
    "shelljs": "^0.8.1"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM