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

Commit

Permalink
feat(vars): Convert environment vars (#32)
Browse files Browse the repository at this point in the history
* 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
EnzoMartin authored and Kent C. Dodds committed Oct 4, 2016
1 parent f2275d5 commit 8da54d0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"test:watch": "mocha src/*.test.js -w --compilers js:babel/register",
"test": "istanbul cover -x *.test.js node_modules/mocha/bin/_mocha -- -R spec src/index.test.js --compilers js:babel/register",
"test": "istanbul cover -x *.test.js node_modules/mocha/bin/_mocha -- -R spec src/*.test.js --compilers js:babel/register",
"prepublish": "npm run build",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
Expand Down
19 changes: 19 additions & 0 deletions src/command.js
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;
}
50 changes: 50 additions & 0 deletions src/command.test.js
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');
});
});

});
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {spawn} from 'cross-spawn';
import commandConvert from './command';
export default crossEnv;

const envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/;
Expand All @@ -16,7 +17,7 @@ function crossEnv(args) {
function getCommandArgsAndEnvVars(args) { // eslint-disable-line
let command;
const envVars = Object.assign({}, process.env);
const commandArgs = args.slice();
const commandArgs = args.slice().map(commandConvert);

This comment has been minimized.

Copy link
@jdalton

jdalton Oct 4, 2016

.map will create a new array so no need to .slice() before.

This comment has been minimized.

Copy link
@kentcdodds

kentcdodds Oct 4, 2016

Owner

Thanks for that. I added that in my #34 fix :)

while (commandArgs.length) {
const shifted = commandArgs.shift();
const match = envSetterRegex.exec(shifted);
Expand Down
5 changes: 2 additions & 3 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import chai from 'chai';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import assign from 'lodash.assign';
chai.use(sinonChai);

const {expect} = chai;
Expand Down Expand Up @@ -93,13 +92,13 @@ describe(`cross-env`, () => {
if (process.env.APPDATA) {
env.APPDATA = process.env.APPDATA;
}
assign(env, expected);
Object.assign(env, expected);
expect(ret, 'returns what spawn returns').to.equal(spawned);
expect(proxied['cross-spawn'].spawn).to.have.been.calledOnce;
expect(proxied['cross-spawn'].spawn).to.have.been.calledWith(
'echo', ['hello world'], {
stdio: 'inherit',
env: assign({}, process.env, env)
env: Object.assign({}, process.env, env)
}
);

Expand Down

0 comments on commit 8da54d0

Please sign in to comment.