Skip to content

Commit

Permalink
feat: support port shortcut for #23
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 5, 2018
1 parent f07c57c commit 4c60ea7
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ node_js:
- '8'
before_script:
- npm prune
script:
- npm test
- npm run demo
- npm run demo2
- npm run demo3
- npm run demo4
after_success:
- npm run semantic-release
branches:
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"standard.run": "onSave"
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ If you use convention and name your scripts "start" and "test" you can simply pr
}
```

You can also shorten local url to just port

```json
{
"scripts": {
"start": "npm start",
"test": "mocha e2e-spec.js",
"ci": "server-test 8080"
}
}
```

## 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
10 changes: 5 additions & 5 deletions bin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

const args = process.argv.slice(2)
const la = require('lazy-ass')
const is = require('check-more-types')
const startAndTest = require('..')
const utils = require('../src/utils')

let start = 'start'
let test = 'test'
let url

if (args.length === 1 && is.url(args[0])) {
// passed just single url for example
if (args.length === 1 && utils.isUrlOrPort(args[0])) {
// passed just single url or port number, for example
// "start": "http://localhost:8080"
url = args[0]
url = utils.normalizeUrl(args[0])
} else {
la(args.length === 3, 'expect: <start script name> <url> <test script name>')
start = args[0]
url = args[1]
url = utils.normalizeUrl(args[1])
test = args[2]
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"test2": "curl http://127.0.0.1:9000",
"demo": "node bin/start.js http://127.0.0.1:9000",
"demo2": "node bin/start.js start http://127.0.0.1:9000 test2",
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test"
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test",
"demo4": "node bin/start.js 9000"
},
"devDependencies": {
"ban-sensitive-files": "1.9.2",
Expand Down
40 changes: 40 additions & 0 deletions src/start-server-and-test-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,49 @@

/* eslint-env mocha */
const startServerAndTest = require('.')
const la = require('lazy-ass')

describe('start-server-and-test', () => {
it('write this test', () => {
console.assert(startServerAndTest, 'should export something')
})
})

describe('utils', () => {
const utils = require('./utils')

context('isUrlOrPort', () => {
const isUrlOrPort = utils.isUrlOrPort

it('allows url', () => {
la(isUrlOrPort('http://localhost'))
la(isUrlOrPort('http://foo.com'))
la(isUrlOrPort('http://foo.com/bar/baz.html'))
la(isUrlOrPort('http://localhost:6000'))
})

it('allows port number or string', () => {
la(isUrlOrPort('6006'))
la(isUrlOrPort(8080))
})
})

context('normalizeUrl', () => {
const normalizeUrl = utils.normalizeUrl

it('passes url', () => {
la(normalizeUrl('http://localhost') === 'http://localhost')
la(normalizeUrl('http://localhost:6000') === 'http://localhost:6000')
})

it('changes port to localhost', () => {
la(normalizeUrl('6006') === 'http://localhost:6006')
la(normalizeUrl(8080) === 'http://localhost:8080')
})

it('returns original argument if does not know what to do', () => {
la(normalizeUrl('foo') === 'foo')
la(normalizeUrl(808000) === 808000)
})
})
})
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const is = require('check-more-types')

const isUrlOrPort = s => is.url(s) || is.port(parseInt(s))

const normalizeUrl = s => {
if (is.url(s)) {
return s
}
if (is.port(parseInt(s))) {
return `http://localhost:${s}`
}
// for anything else, return original argument
return s
}

module.exports = {
isUrlOrPort: isUrlOrPort,
normalizeUrl: normalizeUrl
}

0 comments on commit 4c60ea7

Please sign in to comment.