Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

remove duplicate of PATH environment variable on Windows #218

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const crossSpawnMock = require('cross-spawn')
const isWindowsMock = require('../is-windows')

jest.mock('../is-windows')
jest.mock('../patch-path')
jest.mock('cross-spawn')

const crossEnv = require('../')
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/patch-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const patchPathEnv = require('../patch-path')
const isWindowsMock = require('../is-windows')

jest.mock('../is-windows')

beforeEach(() => {
isWindowsMock.mockReturnValue(true)
})

test(`remove duplicate of PATH variable if the current OS is Windows`, () => {
isWindowsMock.mockReturnValue(true)
const env = {PATH: 'path1', Path: 'path2'}
patchPathEnv(env)
expect(env).toStrictEqual({PATH: 'path1'})
})

test(`do nothing if the current OS is not Windows`, () => {
isWindowsMock.mockReturnValue(false)
const env = {PATH: 'path1', Path: 'path2'}
patchPathEnv(env)
expect(env).toStrictEqual({PATH: 'path1', Path: 'path2'})
})
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const {spawn} = require('cross-spawn')
const commandConvert = require('./command')
const varValueConvert = require('./variable')
const patchPathEnv = require('./patch-path')

module.exports = crossEnv

const envSetterRegex = /(\w+)=('(.*)'|"(.*)"|(.*))/

function crossEnv(args, options = {}) {
patchPathEnv(process.env)
const [envSetters, command, commandArgs] = parseCommand(args)
const env = getEnvVars(envSetters)
if (command) {
Expand Down
9 changes: 9 additions & 0 deletions src/patch-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const isWindows = require('./is-windows')

module.exports = patchPathEnv

function patchPathEnv(env) {
if (isWindows() && env.Path && env.PATH) {
delete env.Path
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some comments here to explain why we're doing this because looking at this code as it is is unclear.

}
}