├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "4" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # server-ready-cli [![Build Status](https://travis-ci.org/typicode/server-ready-cli.svg?branch=master)](https://travis-ci.org/typicode/server-ready-cli) 2 | 3 | > Run commands only when a server is available 4 | 5 | Useful if you want to run things in parallel but need to wait for a server to be ready. 6 | 7 | _See [server-ready](https://github.com/typicode/server-ready) for the programmatic API._ 8 | 9 | ## Install 10 | 11 | ```sh 12 | $ npm install -g server-ready-cli 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```sh 18 | $ server-ready --port 3000 -- some-command 19 | ``` 20 | 21 | ## Example 22 | 23 | In your `package.json`, you can use `server-ready-cli` to run `client` only when `server` is available. 24 | 25 | ```json 26 | { 27 | "scripts": { 28 | "client": "server-ready --port 3000 -- node client.js", 29 | "server": "node server.js", 30 | "start": "npm run server & npm run client & wait" 31 | } 32 | } 33 | ``` 34 | 35 | __Tip__ `server-ready-cli` works well with [npm-run-all](https://github.com/mysticatea/npm-run-all). 36 | 37 | ## License 38 | 39 | MIT - [Typicode :cactus:](https://github.com/typicode) 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const execa = require('execa') 3 | const serverReady = require('server-ready') 4 | 5 | const argv = require('yargs') 6 | .usage('Usage: $0 -p -- ') 7 | .help('h') 8 | .alias('h', 'help') 9 | .version() 10 | .alias('v', 'version') 11 | .option('p', { 12 | alias: 'port', 13 | demand: true, 14 | default: 3000, 15 | type: 'number', 16 | nargs: 1 17 | }) 18 | .option('t', { 19 | alias: 'timeout', 20 | default: 10000, 21 | description: 'timeout in ms', 22 | type: 'number', 23 | nargs: 1 24 | }) 25 | .demandCommand(1) 26 | .argv 27 | 28 | serverReady.timeout = argv.timeout 29 | 30 | serverReady(argv.port, (err) => { 31 | if (err) { 32 | console.error( 33 | `server-ready - can't connect to port ${argv.port} (timeout ${serverReady.timeout}ms)` 34 | ) 35 | process.exit(1) 36 | } 37 | 38 | execa.shellSync(argv._.join(' '), { stdio: 'inherit' }) 39 | }) 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server-ready-cli", 3 | "version": "0.1.0", 4 | "description": "", 5 | "main": "index.js", 6 | "bin": { 7 | "server-ready": "index.js" 8 | }, 9 | "scripts": { 10 | "test": "node test && standard" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/typicode/server-ready-cli.git" 15 | }, 16 | "author": "typicode ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/typicode/server-ready-cli/issues" 20 | }, 21 | "homepage": "https://github.com/typicode/server-ready-cli#readme", 22 | "dependencies": { 23 | "execa": "^0.5.0", 24 | "server-ready": "^0.3.1", 25 | "yargs": "^6.6.0" 26 | }, 27 | "engines": { 28 | "node": ">=4" 29 | }, 30 | "devDependencies": { 31 | "standard": "^8.6.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert') 2 | const http = require('http') 3 | const execa = require('execa') 4 | 5 | const result = execa.sync('node', [ 6 | 'index.js', 7 | '--port', '4000', 8 | '--timeout', '1000', 9 | '--', 'echo ok' 10 | ]) 11 | assert.equal(result.status, 1) 12 | assert.equal(result.stdout, '') 13 | assert.notEqual(result.stderr, '') 14 | 15 | http.createServer((req, res) => { 16 | res.end() 17 | }).listen(4000, () => { 18 | console.log('Server listening') 19 | const result = execa.sync('node', [ 20 | 'index.js', 21 | '--port', '4000', 22 | '--timeout', '1000', 23 | '--', 'echo ok' 24 | ]) 25 | assert.equal(result.status, 0) 26 | assert(result.stdout.includes('ok')) 27 | process.exit(0) 28 | }) 29 | --------------------------------------------------------------------------------