othernpm
Version:
Run npm commands in other directories
111 lines (82 loc) • 2.95 kB
Markdown
[![npm version][npm-badge]][npm-version]
[![Travis Build Status][travis-badge]][travis]
[![App Veyor Build status][appveyor-badge]][appveyor]
[![Coverage Status][coveralls-badge]][coveralls]
[![Dependency Status][david-badge]][david]
[![devDependency Status][david-dev-badge]][david-dev]
[]: https://img.shields.io/npm/v/othernpm.svg
[]: https://www.npmjs.org/package/othernpm
[]: https://travis-ci.org/ryym/othernpm.svg?branch=master
[]: https://travis-ci.org/ryym/othernpm
[]: https://ci.appveyor.com/api/projects/status/rgc3vn28sc9robt0/branch/master?svg=true
[]: https://ci.appveyor.com/project/ryym/othernpm/branch/master
[]: https://coveralls.io/repos/github/ryym/othernpm/badge.svg?branch=master
[]: https://coveralls.io/github/ryym/othernpm?branch=master
[]: https://david-dm.org/ryym/othernpm.svg
[]: https://david-dm.org/ryym/othernpm
[]: https://david-dm.org/ryym/othernpm/dev-status.svg
[]: https://david-dm.org/ryym/othernpm#info=devDependencies
Othernpm provides a way to run npm commands in other directories.
A typical use of this module would be to run npm scripts of subdirectories
that have own `package.json`.
Othernpm allows you to specify a path where npm commands run.
```sh
% onpm ./templates install
```
You can also define paths in `package.json` and use them to specify
a path.
```json
{
"name": "package name",
...
"othernpm": {
"examples": "./examples",
"site": "./site"
}
}
```
```sh
% onpm site run build
% onpm examples test
```
For example, this repository defines a npm-script named `eg` in its [package.json][root-pkg].
By this, we can run any npm command in [test/example] from the root directory like:
[]: ./package.json
[]: ./test/example
```sh
% npm run eg -- run
% npm run eg -- install --save-dev mocha
```
Othernpm also provides API used in Node.js scripts.
The module loads configurations from `package.json` automatically
so that you can use them as functions.
```javascript
const onpm = require('othernpm');
// Run the 'start' script in the examples directory.
onpm.examples('start');
// Run the 'build' script in the site directory.
onpm.site('run build');
// Run npm commands in another directory.
onpm('./templates')('test');
```
The othernpm function returns a [spawn] object.
[]: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
```javascript
const build = onpm.examples('run build');
build.on('exit', exitCode => console.log(exitCode));
```
```sh
npm install --save-dev othernpm
```
MIT