Skip to content

Commit

Permalink
feat: custom commands (#166)
Browse files Browse the repository at this point in the history
Allows to pass in addition to NPM script names any commands. For example

```
start-test 'http-server -c-1 --silent' 8000 'npm run test -- --verbose'
```


* feat: allow arbitrary commands, close #165

* chore: add port argument to the test server script

* chore: add client script for testing

* chore: described custom commands in README
  • Loading branch information
bahmutov committed Apr 23, 2019
1 parent be02efb commit 2979b15
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 52 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ script:
- npm run demo7
- START_SERVER_AND_TEST_INSECURE=1 npm run demo9
- npm run demo-cross-env
- npm run demo-commands
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ This command is meant to be used with NPM script commands. If you have a "start

To execute all tests simply run `npm run ci`

### Commands

In addition to using NPM script names, you can pass entire commands (surround them with quotes so it is still a single string) that will be executed "as is". For example, to start globally installed `http-server` before running and recording [Cypress.io](https://www.cypress.io) tests you can use

```
start-server-and-test 'http-server -c-1 --silent' 8000 './node_modules/.bin/cypress run --record'
```

### Alias

You can use either `start-server-and-test`, `server-test` or `start-test` commands in your scripts.
Expand Down Expand Up @@ -110,6 +118,8 @@ You can provide multiple resources to wait on, separated by a pipe `|`. _(be sur
}
```

or for multiple ports simply: `server-test '8000|9000' test`.

## Note for webpack-dev-server users

If you are using [webpack-dev-server](https://www.npmjs.com/package/webpack-dev-server) (directly or via `angular/cli` or other boilerplates) then please use the following URL form to check
Expand Down
38 changes: 23 additions & 15 deletions __snapshots__/utils-spec.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
exports['utils getArguments handles 3 arguments with http-get url 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http-get://localhost:8080"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments returns 3 arguments 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:8080"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments returns 3 arguments with url 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:8080"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments understands custom commands 1'] = {
"start": "custom-command --with argument",
"url": [
"http://localhost:3000"
],
"test": "test-command --x=1"
}

exports['utils getArguments understands several ports 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:3000",
"http://localhost:4000",
"http://localhost:5000"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments understands single :port 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:3000"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments understands single port 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:3000"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments understands start plus url 1'] = {
"start": "start-server",
"url": [
"http://localhost:6000"
],
"test": "test"
"test": "npm run test"
}

exports['utils getArguments understands url plus test 1'] = {
"start": "start",
"start": "npm run start",
"url": [
"http://localhost:6000"
],
"test": "test"
"test": "npm run test"
}
123 changes: 100 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"demo9": "node src/bin/start.js start-https \"https://127.0.0.1:9000\" test4",
"demo10": "node src/bin/start.js start-fail http://127.0.0.1:9000 test",
"demo-cross-env": "node src/bin/start.js start-cross-env 9000",
"demo-commands": "node src/bin/start.js 'node test/server.js --port 8800' 8800 'node test/client --port 8800'",
"travis-deploy-once": "travis-deploy-once"
},
"devDependencies": {
Expand All @@ -97,7 +98,9 @@
"deps-ok": "1.4.1",
"dont-crack": "1.2.1",
"git-issues": "1.3.1",
"got": "9.6.0",
"license-checker": "24.1.0",
"minimist": "1.2.0",
"mocha": "6.1.4",
"pre-git": "3.17.1",
"prettier-standard": "8.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/bin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const utils = require('../utils')

const { start, test, url } = utils.getArguments(args)

console.log(`starting server using command "npm run ${start}"`)
console.log(`and when url "${url}" is responding`)
console.log(`running tests using command "npm run ${test}"`)
console.log('starting server using command "%s"', start)
console.log('and when url "%s" is responding', url)
console.log('running tests using command "%s"', test)

startAndTest({ start, url, test }).catch(e => {
console.error(e)
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ function startAndTest ({ start, url, test }) {
url
)

debug('starting server, verbose mode?', isDebug())
debug('starting server with command "%s", verbose mode?', start, isDebug())

const startCommand = 'npm run start'
const server = execa.shell(startCommand, { stdio: 'inherit' })
const server = execa.shell(start, { stdio: 'inherit' })
let serverStopped

function stopServer () {
Expand Down Expand Up @@ -87,9 +86,8 @@ function startAndTest ({ start, url, test }) {
})

function runTests () {
const testCommand = `npm run ${test}`
debug('running test script command: %s', testCommand)
return execa.shell(testCommand, { stdio: 'inherit' })
debug('running test script command: %s', test)
return execa.shell(test, { stdio: 'inherit' })
}

return waited
Expand Down
14 changes: 14 additions & 0 deletions src/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('utils', () => {
})

it('understands start plus url', () => {
// note that this script "start-server" does not exist
// thus it is left as is - without "npm run" part
snapshot(getArguments(['start-server', '6000']))
})

Expand All @@ -59,6 +61,18 @@ describe('utils', () => {
it('understands several ports', () => {
snapshot(getArguments(['3000|4000|5000']))
})

it('understands custom commands', () => {
// these commands are NOT script names in the package.json
// thus they will be run as is
snapshot(
getArguments([
'custom-command --with argument',
'3000',
'test-command --x=1'
])
)
})
})

context('isUrlOrPort', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const getArguments = cliArgs => {
test = cliArgs[2]
}

if (isPackageScriptName(start)) {
start = `npm run ${start}`
}

if (isPackageScriptName(test)) {
test = `npm run ${test}`
}

return {
start,
url,
Expand Down
Loading

0 comments on commit 2979b15

Please sign in to comment.