Skip to content

Commit

Permalink
Fix checks for "command" and "dir" combination (#936)
Browse files Browse the repository at this point in the history
* Fix checks for "command" and "dir" combination

* Add tests for new dir flag behaviour

* Formatting
  • Loading branch information
RaeesBhatti authored Jun 19, 2020
1 parent c56c0a7 commit 2ddcff3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/utils/detect-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports.serverSettings = async (devConfig, flags, projectDir, log) => {
if (flags.dir) {
settings = await getStaticServerSettings(settings, flags, projectDir, log)
;['command', 'targetPort'].forEach(p => {
if (devConfig.hasOwnProperty(p)) {
if (flags[p]) {
throw new Error(
`"${p}" option cannot be used in conjunction with "dir" flag which is used to run a static server`
)
Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports.serverSettings = async (devConfig, flags, projectDir, log) => {
if (settings.command === 'npm' && !['start', 'run'].includes(settings.args[0])) {
settings.args.unshift('run')
}
if (devConfig.command) {
if (!settings.noCmd && devConfig.command) {
settings.command = assignLoudly(devConfig.command.split(/\s/)[0], settings.command || null, tellUser('command')) // if settings.command is empty, its bc no settings matched
settings.args = assignLoudly(devConfig.command.split(/\s/).slice(1), [], tellUser('command')) // if settings.command is empty, its bc no settings matched
}
Expand Down
24 changes: 19 additions & 5 deletions src/utils/detect-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,31 @@ test('serverSettings: "dir" flag', async t => {
t.is(settings.noCmd, true)
})

test('serverSettings: "dir" flag with "targetPort"', async t => {
const devConfig = { framework: '#auto', targetPort: 1234, functions: path.join(sitePath, 'functions') }
test('serverSettings: "dir" flag and "command" as config param', async t => {
const devConfig = {
framework: '#auto',
command: 'npm start',
publish: path.join(sitePath, 'build'),
functions: path.join(sitePath, 'functions'),
}
const flags = { dir: sitePath }
const settings = await serverSettings(devConfig, flags, sitePath, () => {})
t.is(settings.command, undefined)
t.is(settings.noCmd, true)
t.is(settings.dist, flags.dir)
})

test('serverSettings: "dir" and "targetPort" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath, targetPort: 1234 }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, sitePath, () => {})
}, /"targetPort" option cannot be used in conjunction with "dir" flag/)
})

test('serverSettings: "dir" flag with "command"', async t => {
const devConfig = { framework: '#auto', command: 'ding', functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath }
test('serverSettings: "dir" and "command" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath, command: 'ding' }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, sitePath, () => {})
}, /"command" option cannot be used in conjunction with "dir" flag/)
Expand Down

0 comments on commit 2ddcff3

Please sign in to comment.