Skip to content

Commit 4c60ea7

Browse files
committed
feat: support port shortcut for #23
1 parent f07c57c commit 4c60ea7

File tree

7 files changed

+87
-6
lines changed

7 files changed

+87
-6
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ node_js:
99
- '8'
1010
before_script:
1111
- npm prune
12+
script:
13+
- npm test
14+
- npm run demo
15+
- npm run demo2
16+
- npm run demo3
17+
- npm run demo4
1218
after_success:
1319
- npm run semantic-release
1420
branches:

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"standard.run": "onSave"
3+
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ If you use convention and name your scripts "start" and "test" you can simply pr
5151
}
5252
```
5353

54+
You can also shorten local url to just port
55+
56+
```json
57+
{
58+
"scripts": {
59+
"start": "npm start",
60+
"test": "mocha e2e-spec.js",
61+
"ci": "server-test 8080"
62+
}
63+
}
64+
```
65+
5466
## Note for webpack-dev-server users
5567

5668
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

bin/start.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
const args = process.argv.slice(2)
44
const la = require('lazy-ass')
5-
const is = require('check-more-types')
65
const startAndTest = require('..')
6+
const utils = require('../src/utils')
77

88
let start = 'start'
99
let test = 'test'
1010
let url
1111

12-
if (args.length === 1 && is.url(args[0])) {
13-
// passed just single url for example
12+
if (args.length === 1 && utils.isUrlOrPort(args[0])) {
13+
// passed just single url or port number, for example
1414
// "start": "http://localhost:8080"
15-
url = args[0]
15+
url = utils.normalizeUrl(args[0])
1616
} else {
1717
la(args.length === 3, 'expect: <start script name> <url> <test script name>')
1818
start = args[0]
19-
url = args[1]
19+
url = utils.normalizeUrl(args[1])
2020
test = args[2]
2121
}
2222

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
"test2": "curl http://127.0.0.1:9000",
7878
"demo": "node bin/start.js http://127.0.0.1:9000",
7979
"demo2": "node bin/start.js start http://127.0.0.1:9000 test2",
80-
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test"
80+
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test",
81+
"demo4": "node bin/start.js 9000"
8182
},
8283
"devDependencies": {
8384
"ban-sensitive-files": "1.9.2",

src/start-server-and-test-spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,49 @@
22

33
/* eslint-env mocha */
44
const startServerAndTest = require('.')
5+
const la = require('lazy-ass')
56

67
describe('start-server-and-test', () => {
78
it('write this test', () => {
89
console.assert(startServerAndTest, 'should export something')
910
})
1011
})
12+
13+
describe('utils', () => {
14+
const utils = require('./utils')
15+
16+
context('isUrlOrPort', () => {
17+
const isUrlOrPort = utils.isUrlOrPort
18+
19+
it('allows url', () => {
20+
la(isUrlOrPort('http://localhost'))
21+
la(isUrlOrPort('http://foo.com'))
22+
la(isUrlOrPort('http://foo.com/bar/baz.html'))
23+
la(isUrlOrPort('http://localhost:6000'))
24+
})
25+
26+
it('allows port number or string', () => {
27+
la(isUrlOrPort('6006'))
28+
la(isUrlOrPort(8080))
29+
})
30+
})
31+
32+
context('normalizeUrl', () => {
33+
const normalizeUrl = utils.normalizeUrl
34+
35+
it('passes url', () => {
36+
la(normalizeUrl('http://localhost') === 'http://localhost')
37+
la(normalizeUrl('http://localhost:6000') === 'http://localhost:6000')
38+
})
39+
40+
it('changes port to localhost', () => {
41+
la(normalizeUrl('6006') === 'http://localhost:6006')
42+
la(normalizeUrl(8080) === 'http://localhost:8080')
43+
})
44+
45+
it('returns original argument if does not know what to do', () => {
46+
la(normalizeUrl('foo') === 'foo')
47+
la(normalizeUrl(808000) === 808000)
48+
})
49+
})
50+
})

src/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const is = require('check-more-types')
2+
3+
const isUrlOrPort = s => is.url(s) || is.port(parseInt(s))
4+
5+
const normalizeUrl = s => {
6+
if (is.url(s)) {
7+
return s
8+
}
9+
if (is.port(parseInt(s))) {
10+
return `http://localhost:${s}`
11+
}
12+
// for anything else, return original argument
13+
return s
14+
}
15+
16+
module.exports = {
17+
isUrlOrPort: isUrlOrPort,
18+
normalizeUrl: normalizeUrl
19+
}

0 commit comments

Comments
 (0)