├── .gitignore ├── README.md ├── bin └── ns ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | *.swp 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # npm-scripts 2 | 3 | Run your npm scripts more efficiently 4 | 5 | [![NPM](https://nodei.co/npm/npm-scripts.png?downloads=true&stars=true)](https://nodei.co/npm/npm-scripts/) 6 | 7 | ## Usage 8 | 9 | ``` 10 | npm install -g npm-scripts 11 | ``` 12 | 13 | Now in a project directory, where you have your `package.json`, run `ns`: 14 | 15 | ``` 16 | $ ns 17 | ? Select a package.json script command to run, e.g. npm run [command]. (Use arrow keys) 18 | ❯ start 19 | install 20 | test 21 | test-debug 22 | nightwatch 23 | sauce 24 | static 25 | (Move up and down to reveal more choices) 26 | ``` 27 | 28 | _The above is just example output._ 29 | 30 | You can also use `ns [command]` as a shortcut. So something like this: 31 | 32 | ``` 33 | ns install 34 | ns build 35 | ns start 36 | ``` 37 | 38 | This also works with the beginning of the command like `ns i` for `ns install`. 39 | 40 | For available options, use `ns -h`. 41 | -------------------------------------------------------------------------------- /bin/ns: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | require('../'); 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var cli = require('ltcdr'); 4 | var path = require('path'); 5 | var spawn = require('child_process').spawn; 6 | var mothership = require('mothership'); 7 | var inquirer = require('inquirer'); 8 | var pkg = require('./package'); 9 | 10 | var packRes = mothership.sync(process.cwd(), function (pack) { 11 | return pack.name; 12 | }); 13 | var targetPkg = packRes.pack; 14 | 15 | if (!targetPkg) { 16 | console.error('Sorry, but we could not find a `package.json` file'); 17 | process.exit(1); 18 | } 19 | 20 | var defaultCommands = ['test', 'install', 'start']; 21 | var commands = targetPkg.scripts ? Object.keys(targetPkg.scripts) : []; 22 | var allCommands = mergeDefaultCommands(commands, defaultCommands); 23 | 24 | cli 25 | .version(pkg.version) 26 | .usage('[command|run shell-code]'); 27 | 28 | cli 29 | .command('*') 30 | .description('Run an arbitrary script without `npm run [command]`.') 31 | .action(function (command) { 32 | if (allCommands.indexOf(command) > -1) { 33 | runCommand(command); 34 | } 35 | else { 36 | var possible = allCommands.filter(function (possibleCommand) { 37 | return possibleCommand.indexOf(command) === 0; 38 | }); 39 | 40 | if (possible.length === 1) { 41 | console.log('Shortcut used for `' + possible[0] + '`.'); 42 | runCommand(possible[0]); 43 | } 44 | else if (possible.length > 1) { 45 | console.log('Multiple possibilities found, please choose one'); 46 | chooseCommand(possible); 47 | } 48 | else { 49 | console.log('Command \'' + command + '\' doesn\'t exist in this project, try `ns` instead.'); 50 | } 51 | } 52 | }); 53 | 54 | cli.parse(process.argv); 55 | 56 | if (process.argv.length === 2) { 57 | chooseCommand(mergeDefaultCommands(commands, defaultCommands)); 58 | } 59 | 60 | function runCommand(command) { 61 | var args = [command]; 62 | 63 | if (defaultCommands.indexOf(command) === -1) { 64 | args.unshift('run'); 65 | } 66 | 67 | //console.log('Running `npm ' + args.join(' ') + '` command.'); 68 | 69 | return spawn('npm', args, { stdio: 'inherit' }); 70 | } 71 | 72 | function mergeDefaultCommands(commands, defaultCommands) { 73 | defaultCommands.forEach(function (item) { 74 | if (commands.indexOf(item) === -1) { 75 | commands.push(item); 76 | } 77 | }); 78 | 79 | return commands; 80 | } 81 | 82 | function chooseCommand(availableCommands) { 83 | inquirer.prompt([ 84 | { 85 | type: 'list', 86 | name: 'command', 87 | message: 'Select a package.json script command to run, e.g. npm run [command].', 88 | default: 'start', 89 | choices: availableCommands 90 | } 91 | ], function(answers) { 92 | runCommand(answers.command); 93 | }); 94 | } 95 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-scripts", 3 | "version": "1.4.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "bin": { 10 | "ns": "./bin/ns" 11 | }, 12 | "repository": { 13 | "url": "https://github.com/knownasilya/npm-scripts", 14 | "type": "git" 15 | }, 16 | "author": "Ilya Radchenko ", 17 | "license": "ISC", 18 | "dependencies": { 19 | "inquirer": "^0.8.2", 20 | "ltcdr": "^2.2.1", 21 | "mothership": "^0.3.0" 22 | } 23 | } 24 | --------------------------------------------------------------------------------