This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vars): Convert environment vars (#32)
* feat(vars): Convert environment vars - Converts environment vars used in `npm run` to account for windows/linux respectively - Add tests for command - Update `npm test` to look for all .test.js files * fix(assign): Remove lodash.assign - Use Object.assign
- Loading branch information
1 parent
f2275d5
commit 8da54d0
Showing
5 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default commandConvert; | ||
|
||
const envUseUnixRegex = /\$(\w+)/g; // $my_var | ||
const envUseWinRegex = /\%(.*?)\%/g; // %my_var% | ||
const isWin = process.platform === 'win32'; | ||
const envExtract = isWin ? envUseUnixRegex : envUseWinRegex; | ||
|
||
/** | ||
* Converts an environment variable usage to be appropriate for the current OS | ||
* @param {String} command Command to convert | ||
* @returns {String} Converted command | ||
*/ | ||
function commandConvert(command) { | ||
const match = envExtract.exec(command); | ||
if (match) { | ||
command = isWin ? `%${match[1]}%` : `$${match[1]}`; | ||
} | ||
return command; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import chai from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
import proxyquire from 'proxyquire'; | ||
chai.use(sinonChai); | ||
|
||
const {expect} = chai; | ||
|
||
describe(`commandConvert`, () => { | ||
const platform = process.platform; | ||
let commandConvert; | ||
|
||
describe(`on Windows`, () =>{ | ||
beforeEach(() =>{ | ||
Object.defineProperty(process, 'platform', {value: 'win32'}); | ||
commandConvert = proxyquire('./command', {}); | ||
}); | ||
|
||
afterEach(() =>{ | ||
Object.defineProperty(process, 'platform', {value: platform}); | ||
}); | ||
|
||
it(`should convert unix-style env variable usage for windows`, () =>{ | ||
expect(commandConvert('$test')).to.equal('%test%'); | ||
}); | ||
|
||
it(`should leave command unchanged when not a variable`, () =>{ | ||
expect(commandConvert('test')).to.equal('test'); | ||
}); | ||
}); | ||
|
||
describe(`on Unix-based`, () =>{ | ||
beforeEach(() => { | ||
Object.defineProperty(process, 'platform', {value: 'linux'}); | ||
commandConvert = proxyquire('./command', {}); | ||
}); | ||
|
||
afterEach(() =>{ | ||
Object.defineProperty(process, 'platform', {value: platform}); | ||
}); | ||
|
||
it(`should convert windows-style env variable usage for linux`, () =>{ | ||
expect(commandConvert('%test%')).to.equal('$test'); | ||
}); | ||
|
||
it(`should leave variable unchanged when using correct operating system`, () =>{ | ||
expect(commandConvert('$test')).to.equal('$test'); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.map
will create a new array so no need to.slice()
before.