簡體   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