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

Commit

Permalink
v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Dec 25, 2015
2 parents a968abc + e1d35f9 commit 37410b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _lodashAssign2 = _interopRequireDefault(_lodashAssign);

exports['default'] = crossEnv;

var envSetterRegex = /(\w+)=(\w+)/;
var envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/;

function crossEnv(args) {
var _getCommandArgsAndEnvVars = getCommandArgsAndEnvVars(args);
Expand All @@ -40,7 +40,7 @@ function getCommandArgsAndEnvVars(args) {
var shifted = commandArgs.shift();
var match = envSetterRegex.exec(shifted);
if (match) {
envVars[match[1]] = match[2];
envVars[match[1]] = match[3] || match[4] || match[5];
} else {
command = shifted;
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cross-env",
"version": "1.0.5",
"version": "1.0.6",
"description": "Run commands that set environment variables across platforms",
"main": "src/index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {spawn} from 'cross-spawn-async';
import assign from 'lodash.assign';
export default crossEnv;

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

function crossEnv(args) {
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args);
Expand All @@ -19,7 +19,7 @@ function getCommandArgsAndEnvVars(args) {
const shifted = commandArgs.shift();
const match = envSetterRegex.exec(shifted);
if (match) {
envVars[match[1]] = match[2];
envVars[match[1]] = match[3] || match[4] || match[5];
} else {
command = shifted;
break;
Expand Down
45 changes: 37 additions & 8 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,60 @@ describe(`cross-env`, () => {
});

it(`should set environment variables and run the remaining command`, () => {
testEnvSetting('FOO_ENV=production');
testEnvSetting({
FOO_ENV: 'production'
}, 'FOO_ENV=production');
});

it(`should handle multiple env variables`, () => {
testEnvSetting('FOO_ENV=production', 'bar_env=dev');
testEnvSetting({
FOO_ENV: 'production',
BAR_ENV: 'dev'
}, 'FOO_ENV=production', 'BAR_ENV=dev');
});

it(`should handle special characters`, () => {
testEnvSetting({
FOO_ENV: './!?'
}, 'FOO_ENV=./!?');
});

it(`should handle single-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV=\'bar env\'');
});

it(`should handle double-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV="bar env"');
});

it(`should handle equality signs in quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'foo=bar'
}, 'FOO_ENV="foo=bar"');
});

it(`should do nothing given no command`, () => {
crossEnv([]);
expect(proxied['cross-spawn-async'].spawn).to.have.not.been.called;
});

function testEnvSetting(...envSettings) {
function testEnvSetting(expected, ...envSettings) {
const ret = crossEnv([...envSettings, 'echo', 'hello world']);
const env = {[getPathVar()]: process.env[getPathVar()]};
env.APPDATA = process.env.APPDATA;
envSettings.forEach(setting => {
const [prop, val] = setting.split('=');
env[prop] = val;
});
assign(env, expected);

expect(ret, 'returns what spawn returns').to.equal('spawn-returned');
expect(proxied['cross-spawn-async'].spawn).to.have.been.calledOnce;
expect(proxied['cross-spawn-async'].spawn).to.have.been.calledWith(
'echo', ['hello world'], {stdio: 'inherit', env: assign({}, process.env, env)}
'echo', ['hello world'], {
stdio: 'inherit',
env: assign({}, process.env, env)
}
);
}
});

0 comments on commit 37410b4

Please sign in to comment.