Skip to content

Commit

Permalink
feat: allow arbitrary commands, close #165
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 23, 2019
1 parent be02efb commit 81e05aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 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
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"
}
1 change: 1 addition & 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' 9000 'npm run unit'",
"travis-deploy-once": "travis-deploy-once"
},
"devDependencies": {
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

0 comments on commit 81e05aa

Please sign in to comment.