我不知道什么是正确的词汇表,甚至可以搜索这个问题,但我可以提到几个实现我正在尝试学习的模式的包:
shxcross-envnpm-run一旦安装了这些软件包,它们就可以通过CLI获得。例如,在我将cross-env作为一个新项目的依赖项安装之后,我可以在我的package.json中创建一个类似于"start": "cross-env NODE_ENV=production webpack"的npm脚本。
我检查了这些项目的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"
}发布于 2018-02-14 15:27:34
我最终找到了答案,并让sloth-cli发挥了作用。bin in package.json确实是关键,但是在定义bin之后,我们必须在本地运行npm link。
此外,bin的内容不能包含典型的npm脚本语法。它必须引用设置为在CLI上下文中执行的文件。这可以简单到javascript文件顶部的#! /usr/bin/env node。
一旦将包发布到npm,那些将其作为依赖项安装的人不需要运行npm link。国家预防机制处理这部分。下面是当前工作的package.json for sloth-cli
{
"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"
}
}https://stackoverflow.com/questions/48738912
复制相似问题