[英]How to use an npm package binary without installing it
I'm trying to create a CLI tool (in Node, on NPM) that creates an app using Brunch . 我正在尝试创建一个CLI工具(在NPM上的Node中),该工具使用Brunch创建一个应用程序。
So basically, the expected behaviour is that the user will type this to install my tool: 因此,基本上,预期的行为是用户将键入此内容以安装我的工具:
npm install -g my-cli-tool
And then, to create a new Brunch project inside a sub-directory named myApp
, they will run this in their terminal: 然后,要在名为myApp
的子目录中创建一个新的Brunch项目,他们将在其终端中运行该项目:
my-cli-tool myApp
Brunch will handle most of the work of creating the app itself. 早午餐将处理创建应用程序本身的大部分工作。
Just for reference, you can create Brunch projects by installing Brunch globally ( npm install -g brunch
) and then simply typing brunch new myApp
. 仅供参考,您可以通过全局安装Brunch( npm install -g brunch
),然后只需键入brunch new myApp
来创建Brunch项目。
I've already setup the necessary stuff for making an NPM package that exposes a binary, so I will just reduce it down to my one script. 我已经设置了制作暴露二进制文件的NPM软件包所必需的内容,因此我将其简化为一个脚本。
What I have tried so far is the following: 到目前为止,我已经尝试了以下方法:
#! /usr/bin/env node
var shell = require("shelljs");
if (!process.argv[2]) {
console.log('You need to supply a name for your app!');
console.log();
console.log('Try:');
console.log(' my-cli-tool <project-name>');
} else {
var appName = process.argv[2];
console.log('Your app is now being created...');
console.log('Installing Brunch...');
shell.exec('npm install --save brunch');
console.log('Creating app with Brunch...');
shell.exec('PATH=$(npm bin):$PATH brunch new ' + appName);
console.log('Success! Your app is done.');
}
It works as expected but the problem is, installing Brunch ( npm install --save brunch
) will create a node_modules
folder inside the current working directory along with a package.json
file. 它可以按预期工作,但问题是,安装Brunch( npm install --save brunch
)将在当前工作目录内创建一个node_modules
文件夹以及package.json
文件。
Ideally, I want the current working directory to be untouched except for the creation of a folder named myApp/
(which is done by Brunch itself). 理想情况下,除了创建名为myApp/
的文件夹(由Brunch自己完成)以外,我希望当前工作目录保持不变。 Essentially, I want to use Brunch to generate the app without having any trace of having installed Brunch itself. 本质上,我想使用Brunch来生成应用程序,而无需任何安装Brunch本身的痕迹。
I've taken a look at npm pack
but I am not sure how to apply it to my use case. 我看了npm pack
但不确定如何将其应用于用例。 Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。
your-awsesome-cli-tool
and call it from your package directory 解决方案:将早午餐放入your-awsesome-cli-tool
的依赖项中,然后从软件包目录中调用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.