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

Commit d09dea5

Browse files
hgwoodKent C. Dodds
authored and
Kent C. Dodds
committed
feat(args): convert embedded and braced variables in command args (#86)
* feat(args): convert variables embedded in command args * feat(args): convert braced variables in command args * style(args): full test name on one line * refactor(args): make commandConvert definitly stateless BREAKING CHANGE: `echo $var2/$var1` would not be changed on windows, now it is. This is kind of a bug, but we're doing a major version bump to be safe.
1 parent c1a9ed0 commit d09dea5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/command.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ import isWindows from 'is-windows'
22

33
export default commandConvert
44

5-
const envUseUnixRegex = /\$(\w+)/ // $my_var
6-
const envUseWinRegex = /%(.*?)%/ // %my_var%
7-
85
/**
96
* Converts an environment variable usage to be appropriate for the current OS
107
* @param {String} command Command to convert
118
* @returns {String} Converted command
129
*/
1310
function commandConvert(command) {
11+
const envUseUnixRegex = /\$(\w+)|\${(\w+)}/g // $my_var or ${my_var}
12+
const envUseWinRegex = /%(.*?)%/g // %my_var%
1413
const isWin = isWindows()
1514
const envExtract = isWin ? envUseUnixRegex : envUseWinRegex
16-
const match = envExtract.exec(command)
17-
if (match) {
18-
command = isWin ? `%${match[1]}%` : `$${match[1]}`
19-
}
20-
return command
15+
return command.replace(envExtract, isWin ? '%$1$2%' : '$$$1')
2116
}

src/command.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,29 @@ test(`is stateless`, () => {
3030
isWindowsMock.__mock.returnValue = true
3131
expect(commandConvert('$test')).toBe(commandConvert('$test'))
3232
})
33+
34+
test(`converts embedded unix-style env variables usage for windows`, () => {
35+
isWindowsMock.__mock.returnValue = true
36+
expect(commandConvert('$test1/$test2/$test3')).toBe(
37+
'%test1%/%test2%/%test3%',
38+
)
39+
})
40+
41+
test(`converts embedded windows-style env variables usage for linux`, () => {
42+
isWindowsMock.__mock.returnValue = false
43+
expect(commandConvert('%test1%/%test2%/%test3%')).toBe(
44+
'$test1/$test2/$test3',
45+
)
46+
})
47+
48+
// eslint-disable-next-line max-len
49+
test(`leaves embedded variables unchanged when using correct operating system`, () => {
50+
isWindowsMock.__mock.returnValue = false
51+
expect(commandConvert('$test1/$test2/$test3')).toBe('$test1/$test2/$test3')
52+
})
53+
54+
test(`converts braced unix-style env variable usage for windows`, () => {
55+
isWindowsMock.__mock.returnValue = true
56+
// eslint-disable-next-line no-template-curly-in-string
57+
expect(commandConvert('${test}')).toBe('%test%')
58+
})

0 commit comments

Comments
 (0)