Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use es6 code features and refactoring #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb-base",
"rules": {
"import/no-dynamic-require": "off",
"no-await-in-loop": "off"
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
*.log
.eslintcache
*.log
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
language: node_js
node_js:
- "stable"
- "0.12"
- "0.10"
- "0.8"
- 'node'
- 'stable'
- 12
- 10
- 8

script:
- npm run lint
- npm run spellcheck
- npm run test
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
# nrun

Alternative package.json scripts runner for node.js
[![Build Status](https://travis-ci.org/2do2go/nrun.svg?branch=master)](https://travis-ci.org/2do2go/nrun)

An alternative package.json scripts runner for node.js. `nrun` gets script from `package.json` and runs it using `spawn` and current
shell.

You can use nrun to run your package.json scripts with direct arguments
passing (without `--`). E.g. run `test` with specific reporter:
## Installation

```sh
$ npm install nrun
```

nrun test --reporter dot
## Usage

Let's imagine there are follow scripts in `package.json`

```json
"scripts": {
"lint": "eslint ./",
"test": "jest"
}
```

contrasting to ```npm run test -- --reporter dot```.
Now you can run scripts like so:

nrun gets script from `package.json` and runs it using `spawn` and current
shell.
```
$ nrun lint
$ nrun test --bail
$ nrun test --coverage --coverageReporters=text-lcov
```

Instead of
```
$ npm run lint
$ npm run test -- --bail
$ npm run test -- --coverage --coverageReporters=text-lcov
```

[![Build Status](https://travis-ci.org/2do2go/nrun.svg?branch=master)](https://travis-ci.org/2do2go/nrun)
Also you can activate a completion. Just add the content of the `./bin/completion.sh` file to your `.bashrc`, or just run follow command:

```sh
$ nrun --completion >> ~/.bashrc
```
55 changes: 55 additions & 0 deletions bin/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path');

const isWin = process.platform.substring(0, 3) === 'win';

const getSpawnOpts = () => {
const pathName = ['PATH', 'Path', 'path'].find((name) => process.env[name]);

return {
stdio: 'inherit',
env: {
...process.env,
[pathName]: [
process.env[pathName],
path.delimiter,
path.resolve('node_modules', '.bin'),
].join(''),
},
};
};

const getSpawnParams = (command) => ({
shell: process.env.SHELL || '/bin/sh',
shellArgs: ['-c', command],
opts: getSpawnOpts(),
});

const getSpawnParamsWin = (command) => ({
shell: process.env.comspec || 'cmd',
shellArgs: ['/s', '/c', `"${command}"`],
opts: {
...getSpawnOpts(),
windowsVerbatimArguments: true,
},
});


const replaceArgs = (arg) => arg.replace(/"/g, '\\"');
const replaceArgsWin = (arg) => arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, '$1$1');
const argReplacer = isWin ? replaceArgsWin : replaceArgs;


exports.isWin = isWin;
exports.getSpawnParams = isWin ? getSpawnParamsWin : getSpawnParams;

exports.addArguments = (command, args) => {
if (args.length) {
const argsStr = args
.map((arg) => `"${argReplacer(arg)}"`)
.join(' ');

return `${command} ${argsStr}`;
}

return command;
};
163 changes: 0 additions & 163 deletions bin/nrun

This file was deleted.

82 changes: 82 additions & 0 deletions bin/nrun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const { addArguments } = require('./helpers');
const { writeStdout, writeStderr, spawnCommand } = require('./utils');

const {
name: packageName,
scripts: packageScripts,
} = require(path.join(process.cwd(), 'package.json'));


function getScriptsToRun(scriptName, scriptArgs) {
return ['pre', '', 'post']
.filter((prefix) => packageScripts[prefix + scriptName])
.map((prefix) => {
const name = prefix + scriptName;
const args = prefix ? [] : scriptArgs;

return {
name,
command: addArguments(packageScripts[name], args),
};
});
}

async function exit(code) {
await Promise.all([writeStdout(''), writeStderr('')]);
process.exit(code);
}

/* eslint-disable no-console */
async function runScripts(scripts) {
// eslint-disable-next-line no-restricted-syntax
for (const [index, { name, command }] of Object.entries(scripts)) {
console.log('> nrun %s', name);
console.log('> %s', command);

await spawnCommand(command);

if (index < scripts.length - 1) {
console.log('');
}
}
}

async function validateArgs([, , scriptName, ...scriptArgs]) {
if (!scriptName) {
console.log(`Scripts for "${packageName}":`);
Object.entries(packageScripts).forEach(([name, script]) => {
console.log(` ${name}`);
console.log(` ${script}`);
});

return exit(0);
}

if (scriptName === '--completion') {
console.log(
fs.readFileSync(
path.join(__dirname, 'completion.sh'),
'utf8',
),
);

return exit(0);
}

if (!packageScripts[scriptName]) {
console.error(`Unknown script "${scriptName}" for package "${packageName}"`);
return exit(1);
}

return { scriptName, scriptArgs };
}
/* eslint-enable */

validateArgs(process.argv)
.then(({ scriptName, scriptArgs }) => runScripts(getScriptsToRun(scriptName, scriptArgs)))
.then(exit)
.catch(exit);
Loading