Skip to content

Commit

Permalink
bug(local): Ignore env specification when it's not a file (heroku#2698)
Browse files Browse the repository at this point in the history
* Provide understandable error when env file is not a file

* Rework logic so we can warn and ignore env specification when it's not a file

* Update tests

* Update tests to expect --env to node foreman
  • Loading branch information
eablack authored Mar 15, 2024
1 parent 20665ff commit d23407c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
11 changes: 9 additions & 2 deletions packages/cli/src/commands/local/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {FileCompletion} from '@heroku-cli/command/lib/completions'
import {Args, Command, Flags} from '@oclif/core'

import * as fs from 'fs'
import color from '@heroku-cli/color'
import {fork as foreman} from '../../lib/local/fork-foreman'

// eslint-disable-next-line node/no-missing-require
Expand Down Expand Up @@ -63,8 +64,14 @@ $ heroku local web=1,worker=2`,
this.error('--concurrency is no longer available\nUse forego instead: https://github.com/ddollar/forego')
}

let envFile = flags.env || '.env'
if (fs.existsSync(envFile) && !fs.statSync(envFile).isFile()) {
this.warn(`The specified location for the env file, ${color.bold(envFile)}, is not a file, ignoring.`)
envFile = ''
}

if (flags.procfile) execArgv.push('--procfile', flags.procfile)
if (flags.env) execArgv.push('--env', flags.env)
execArgv.push('--env', envFile)
if (flags.port) execArgv.push('--port', flags.port)

if (args.processname) {
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/commands/local/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {FileCompletion} from '@heroku-cli/command/lib/completions'
import {Command, Flags} from '@oclif/core'

import color from '@heroku-cli/color'
import {fork as foreman} from '../../lib/local/fork-foreman'
import * as fs from 'fs'

export default class Run extends Command {
static description = 'run a one-off command'
Expand Down Expand Up @@ -31,7 +32,13 @@ export default class Run extends Command {
this.error(errorMessage, {exit: -1})
}

if (flags.env) execArgv.push('--env', flags.env)
let envFile = flags.env || '.env'
if (fs.existsSync(envFile) && !fs.statSync(envFile).isFile()) {
this.warn(`The specified location for the env file, ${color.bold(envFile)}, is not a file, ignoring.`)
envFile = ''
}

execArgv.push('--env', envFile)
if (flags.port) execArgv.push('--port', flags.port)

execArgv.push('--') // disable node-foreman flag parsing
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/unit/commands/local/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('local', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['start', 'web,other'])
expect(argv).is.eql(['start', '--env', '.env', 'web,other'])
})
.command(['local:start'])
.it('can call foreman start via the local:start alias')
Expand All @@ -37,7 +37,7 @@ describe('local', () => {
})
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
expect(arguments[0]).is.eql(['start', 'web,other'])
expect(arguments[0]).is.eql(['start', '--env', '.env', 'web,other'])
})
.command(['local'])
.it('can call foreman start with no arguments')
Expand All @@ -58,7 +58,7 @@ describe('local', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['start', '--procfile', 'Procfile.other', 'web,background'])
expect(argv).is.eql(['start', '--procfile', 'Procfile.other', '--env', '.env', 'web,background'])
expect(argv).to.not.include('release', 'the release process is not included')
})
.command(['local', '--procfile', 'Procfile.other'])
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('local', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['start', 'web,other'])
expect(argv).is.eql(['start', '--env', '.env', 'web,other'])
})
.command(['local', 'web,other'])
.it('can call foreman start with only arguments')
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/unit/commands/local/run.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('local:run', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['run', '--', 'echo', 'hello'])
expect(argv).is.eql(['run', '--env', '.env', '--', 'echo', 'hello'])
})
.command(['local:run', 'echo', 'hello'])
.it('can handle one argument passed to foreman after the -- argument separator')
Expand All @@ -24,7 +24,7 @@ describe('local:run', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['run', '--', 'echo', 'hello', 'world'])
expect(argv).is.eql(['run', '--env', '.env', '--', 'echo', 'hello', 'world'])
})
.command(['local:run', 'echo', 'hello', 'world'])
.it('can handle multiple argument passed to foreman after the `--` argument separator')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('local:run', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['run', '--port', '4200', '--', 'bin/serve'])
expect(argv).is.eql(['run', '--env', '.env', '--port', '4200', '--', 'bin/serve'])
})
.command(['local:run', 'bin/serve', '--port', '4200'])
.it('is passed to foreman an a --port flag before the `--` argument separator')
Expand All @@ -64,7 +64,7 @@ describe('local:run', () => {
.stub(foreman, 'fork', function () {
// eslint-disable-next-line prefer-rest-params
const argv = arguments[0]
expect(argv).is.eql(['run', '--port', '4200', '--', 'bin/serve'])
expect(argv).is.eql(['run', '--env', '.env', '--port', '4200', '--', 'bin/serve'])
})
.command(['local:run', 'bin/serve', '-p', '4200'])
.it('is can pass the `-p` shorthand to foreman an a --port flag before the `--` argument separator')
Expand Down

0 comments on commit d23407c

Please sign in to comment.