adrianmcli
adrianmcli

Reputation: 1996

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.

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:

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.

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.

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.

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). Essentially, I want to use Brunch to generate the app without having any trace of having installed Brunch itself.

I've taken a look at npm pack but I am not sure how to apply it to my use case. Any help would be appreciated, thanks.

Upvotes: 1

Views: 749

Answers (1)

webdeb
webdeb

Reputation: 13211

  1. It's crazy to install brunch each time you create an App
  2. Nobody needs a wrapper around a wrapper
  3. Solution: Put brunch in the dependencies of your-awsesome-cli-tool and call it from your package directory

Upvotes: 2

Related Questions