Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(local): Ignore env specification when it's not a file #2698

Merged
merged 4 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/cli/src/commands/local/index.ts
@@ -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
@@ -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()) {
justinwilaby marked this conversation as resolved.
Show resolved Hide resolved
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
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
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