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

Support quoted scripts #89

Merged
merged 4 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Run scripts that set and use environment variables across platforms
[![Code Coverage][coverage-badge]][coverage]
[![Dependencies][dependencyci-badge]][dependencyci]
[![version][version-badge]][package]
[![node-version][node-version-badge]][node]
[![downloads][downloads-badge]][npm-stat]

[![MIT License][license-badge]][LICENSE]
Expand Down Expand Up @@ -36,10 +37,6 @@ setting or using the environment variable properly for the platform. Just set it
like you would if it's running on a POSIX system, and `cross-env` will take care
of setting it properly.

## Prerequisites

- [NodeJS][node] version 4.0 or greater.

## Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and
Expand Down Expand Up @@ -87,6 +84,19 @@ the parent. This is quite useful for launching the same command with different
env variables or when the environment variables are too long to have everything
in one line.

## Gotchas

If you want to have the environment variable apply to several commands in series
then you will need to wrap those in quotes in your script. For example:

```json
{
"scripts": {
"greet": "cross-env GREETING=Hi NAME=Joe \"echo $GREETING && echo $NAME\""
}
}
```

## Inspiration

I originally created this to solve a problem I was having with my npm scripts in
Expand Down Expand Up @@ -129,6 +139,7 @@ MIT
[dependencyci]: https://dependencyci.com/github/kentcdodds/cross-env
[version-badge]: https://img.shields.io/npm/v/cross-env.svg?style=flat-square
[package]: https://www.npmjs.com/package/cross-env
[node-version-badge]: https://img.shields.io/badge/node-%3E%3D%204.8-orange.svg?style=flat-square
[downloads-badge]: https://img.shields.io/npm/dm/cross-env.svg?style=flat-square
[npm-stat]: http://npm-stat.com/charts.html?package=cross-env&from=2016-04-01
[license-badge]: https://img.shields.io/npm/l/cross-env.svg?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"cross-env": "dist/bin/cross-env.js"
},
"engines": {
"node": ">=4.0"
"node": ">=4.8"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a note somewhere in the README indicating Node Version support. I expect some people who have been using cross-env today will need to upgrade node before they can continue using it after this change.

How about we use this badge?

node-version

[![node-version][node-version-badge]][node]

<!-- down below, with the rest of the links -->
[node-version-badge]: https://img.shields.io/badge/node-%3E%3D%204.8-orange.svg?style=flat-square

Put it after the npm version badge and before the downloads badge. What do you think?

},
"scripts": {
"start": "nps",
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/
function crossEnv(args) {
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args)
if (command) {
const proc = spawn(command, commandArgs, {stdio: 'inherit', env})
const proc = spawn(command, commandArgs, {
stdio: 'inherit',
shell: true,
env,
})
process.on('SIGTERM', () => proc.kill('SIGTERM'))
process.on('SIGINT', () => proc.kill('SIGINT'))
process.on('SIGBREAK', () => proc.kill('SIGBREAK'))
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ it(`should handle equality signs in quoted strings`, () => {
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
})

it(`should handle quoted scripts`, () => {
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo $GREETING && echo $NAME'])
expect(
crossSpawnMock.spawn,
).toHaveBeenCalledWith('echo $GREETING && echo $NAME', [], {
stdio: 'inherit',
shell: true,
env: Object.assign({}, process.env, {
GREETING: 'Hi',
NAME: 'Joe',
}),
})
})

it(`should do nothing given no command`, () => {
crossEnv([])
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(0)
Expand Down Expand Up @@ -80,6 +94,7 @@ function testEnvSetting(expected, ...envSettings) {
expect(crossSpawnMock.spawn).toHaveBeenCalledTimes(1)
expect(crossSpawnMock.spawn).toHaveBeenCalledWith('echo', ['hello world'], {
stdio: 'inherit',
shell: true,
env: Object.assign({}, process.env, env),
})

Expand Down