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

Commit

Permalink
feat(args): convert braced variables in command args
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Wood committed Mar 14, 2017
1 parent 371ac46 commit a53f3e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import isWindows from 'is-windows'

export default commandConvert

const envUseUnixRegex = /\$(\w+)/ // $my_var
const envUseWinRegex = /%(.*?)%/ // %my_var%
const envUseUnixRegex = /\$(\w+)|\${(\w+)}/g // $my_var or ${my_var}
const envUseWinRegex = /%(.*?)%/g // %my_var%

/**
* Converts an environment variable usage to be appropriate for the current OS
Expand All @@ -13,5 +13,5 @@ const envUseWinRegex = /%(.*?)%/ // %my_var%
function commandConvert(command) {
const isWin = isWindows()
const envExtract = isWin ? envUseUnixRegex : envUseWinRegex
return command.replace(envExtract, isWin ? '%$1%' : '$$$1')
return command.replace(envExtract, isWin ? '%$1$2%' : '$$$1')
}
6 changes: 6 additions & 0 deletions src/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ test(`
isWindowsMock.__mock.returnValue = false
expect(commandConvert('$test1/$test2/$test3')).toBe('$test1/$test2/$test3')
})

test(`converts braced unix-style env variable usage for windows`, () => {
isWindowsMock.__mock.returnValue = true
// eslint-disable-next-line no-template-curly-in-string
expect(commandConvert('${test}')).toBe('%test%')
})

0 comments on commit a53f3e6

Please sign in to comment.