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

Commit afaae55

Browse files
author
Kent C. Dodds
committed
feat(lib): Initial commit! ✨
0 parents  commit afaae55

15 files changed

+327
-0
lines changed

.editorconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# all files
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 2
12+
charset = utf-8
13+
trim_trailing_whitespace = true
14+
max_line_length = 120
15+
16+
[*.js]
17+
quote_type = single
18+
curly_bracket_next_line = false
19+
spaces_around_operators = true
20+
spaces_around_brackets = inside
21+
indent_brace_style = BSD KNF
22+
23+
# HTML
24+
[*.html]
25+
quote_type = double

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
coverage
3+
dist
4+
local-examples
5+
other

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "kentcdodds/test"
3+
}

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.idea
2+
*.log
3+
*.iml
4+
*.DS_Store
5+
6+
node_modules
7+
coverage
8+
nohup.out
9+
dist
10+
11+
*.ignored.*
12+
*.ignored/
13+
*.ignored

.npmignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.idea
2+
node_modules
3+
coverage
4+
local-examples
5+
demo
6+
.editorconfig
7+
.gitignore
8+
.travis.yml
9+
CONTRIBUTING.md
10+
karma.conf.js
11+
webpack.config.js
12+
other

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
sudo: false
2+
language: node_js
3+
cache:
4+
directories:
5+
- node_modules
6+
branches:
7+
only:
8+
- master
9+
notifications:
10+
email: false
11+
node_js:
12+
- iojs
13+
before_install:
14+
- npm i -g npm@^2.0.0
15+
before_script:
16+
- npm prune
17+
script:
18+
- npm run eslint
19+
- npm t
20+
- npm run check-coverage
21+
after_success:
22+
- npm run report-coverage
23+
- npm run semantic-release

CODE_OF_CONDUCT.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributor Code of Conduct
2+
3+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4+
5+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6+
7+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8+
9+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10+
11+
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
12+
13+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
14+
15+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.1.0, available at [http://contributor-covenant.org/version/1/1/0/](http://contributor-covenant.org/version/1/1/0/)

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# cross-env
2+
3+
Status:
4+
[![npm version](https://img.shields.io/npm/v/cross-env.svg?style=flat-square)](https://www.npmjs.org/package/cross-env)
5+
[![npm downloads](https://img.shields.io/npm/dm/cross-env.svg?style=flat-square)](http://npm-stat.com/charts.html?package=cross-env&from=2015-09-01)
6+
[![Build Status](https://img.shields.io/travis/kentcdodds/cross-env.svg?style=flat-square)](https://travis-ci.org/kentcdodds/cross-env)
7+
[![Code Coverage](https://img.shields.io/codecov/c/github/kentcdodds/cross-env.svg?style=flat-square)](https://codecov.io/github/kentcdodds/cross-env)
8+
9+
This micro-lib allows you to provide a script which sets an environment using unix style and have it work on windows too
10+
11+
## Usage
12+
13+
I use this in my npm scripts:
14+
15+
```json
16+
{
17+
"scripts": {
18+
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
19+
}
20+
}
21+
```
22+
23+
Ultimately, the command that is executed (using `spawn`) is:
24+
25+
```
26+
webpack --config build/webpack.config.js
27+
```
28+
29+
The `NODE_ENV` environment variable will be set by `cross-env`
30+
31+
32+
## Why?
33+
34+
Windows will choke when you set environment variables with `NODE_ENV=production` like that. This makes it so you can
35+
have a single command without worrying about setting the environment variable properly for the platform. Just set it
36+
like you would if it's running on a unix system, and `cross-env` will take care of setting it properly.
37+
38+
## Known limitations
39+
40+
If you plan to do something like this:
41+
42+
```
43+
cross-env FOO=bar && echo $FOO
44+
```
45+
46+
And expect it to output `bar` you're going to be sad, for two reasons:
47+
48+
1. Technically, those will run as two separate commands, so even though `FOO` will properly be set to `bar` in the first command, the `echo $FOO` will not.
49+
2. When `echo $FOO` runs, the `$FOO` variable is replaced with the variable value, before it's even passed to `cross-env` (though, as indicated in #1, that doesn't happen anyway)
50+
51+
The main use case for this package is to simply run another script which will (itself) respond to the environment
52+
variable. These limitations are not a problem in that scenario (like in the example).
53+
54+
## LICENSE
55+
56+
MIT

bin/cross-env.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../dist')(process.argv.slice(2));

other/src.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.test.js

other/src.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "kentcdodds",
3+
"env": {
4+
"browser": false
5+
}
6+
}

package.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "cross-env",
3+
"version": "0.0.0-semanticallly-released.0",
4+
"description": "Run commands that set environment variables across platforms",
5+
"main": "src/index.js",
6+
"bin": {
7+
"cross-env": "bin/cross-env.js"
8+
},
9+
"scripts": {
10+
"start": "npm run test:watch",
11+
"prebuild": "trash dist && mkdir dist",
12+
"build": "cd src && babel index.js -d ../dist && cd ..",
13+
"commit": "git-cz",
14+
"eslint": "eslint src/ -c other/src.eslintrc --ignore-path other/src.eslintignore && eslint src/*.test.js",
15+
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
16+
"report-coverage": "cat ./coverage/lcov.info | codecov",
17+
"test:watch": "mocha src/*.test.js -w --compilers js:babel/register",
18+
"test": "istanbul cover -x *.test.js _mocha -- -R spec src/index.test.js --compilers js:babel/register",
19+
"prepublish": "npm run build",
20+
"postpublish": "publish-latest",
21+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
22+
},
23+
"repository": {
24+
"type": "git",
25+
"url": "https://github.com/kentcdodds/cross-env.git"
26+
},
27+
"keywords": [
28+
"environment variables",
29+
"cross platform"
30+
],
31+
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
32+
"license": "MIT",
33+
"bugs": {
34+
"url": "https://github.com/kentcdodds/cross-env/issues"
35+
},
36+
"homepage": "https://github.com/kentcdodds/cross-env#readme",
37+
"devDependencies": {
38+
"babel": "5.8.23",
39+
"chai": "3.3.0",
40+
"codecov.io": "0.1.6",
41+
"commitizen": "1.0.5",
42+
"cz-conventional-changelog": "1.1.2",
43+
"eslint": "1.5.1",
44+
"eslint-config-kentcdodds": "4.0.1",
45+
"eslint-plugin-mocha": "1.0.0",
46+
"ghooks": "0.3.2",
47+
"istanbul": "0.3.21",
48+
"mocha": "2.3.3",
49+
"proxyquire": "1.7.2",
50+
"semantic-release": "4.3.5",
51+
"sinon": "1.17.1",
52+
"sinon-chai": "2.8.0",
53+
"trash": "2.0.0",
54+
"validate-commit-msg": "1.0.0"
55+
},
56+
"config": {
57+
"ghooks": {
58+
"commit-msg": "./node_modules/.bin/validate-commit-msg && npm run eslint && npm t && npm run check-coverage && echo 'pre-commit checks good 👍'"
59+
}
60+
},
61+
"czConfig": {
62+
"path": "node_modules/cz-conventional-changelog/"
63+
},
64+
"dependencies": {
65+
"add-to-path": "1.1.2"
66+
}
67+
}

src/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {spawn} from 'child_process';
2+
import getPathVar from 'add-to-path/dist/get-path-var';
3+
export default crossEnv;
4+
5+
const envSetterRegex = /(\w+)=(\w+)/;
6+
7+
function crossEnv(args) {
8+
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args);
9+
if (command) {
10+
return spawn(command, commandArgs, {stdio: 'inherit', env});
11+
}
12+
}
13+
14+
function getCommandArgsAndEnvVars(args) {
15+
let command;
16+
const envVars = {[getPathVar()]: process.env[getPathVar()]};
17+
const commandArgs = args.slice();
18+
while (commandArgs.length) {
19+
const shifted = commandArgs.shift();
20+
const match = envSetterRegex.exec(shifted);
21+
if (match) {
22+
envVars[match[1]] = match[2];
23+
} else {
24+
command = shifted;
25+
break;
26+
}
27+
}
28+
return [command, commandArgs, envVars];
29+
}

src/index.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import chai from 'chai';
2+
import sinonChai from 'sinon-chai';
3+
import sinon from 'sinon';
4+
import proxyquire from 'proxyquire';
5+
import getPathVar from 'add-to-path/dist/get-path-var';
6+
chai.use(sinonChai);
7+
8+
const {expect} = chai;
9+
const proxied = {
10+
child_process: { // eslint-disable-line camelcase
11+
spawn: sinon.spy(() => 'spawn-returned')
12+
}
13+
};
14+
15+
const crossEnv = proxyquire('./index', proxied);
16+
17+
describe(`cross-env`, () => {
18+
19+
beforeEach(() => {
20+
proxied.child_process.spawn.reset();
21+
});
22+
23+
it(`should set environment variables and run the remaining command`, () => {
24+
testEnvSetting('FOO_ENV=production');
25+
});
26+
27+
it(`should handle multiple env variables`, () => {
28+
testEnvSetting('FOO_ENV=production', 'bar_env=dev');
29+
});
30+
31+
it(`should do nothing given no command`, () => {
32+
crossEnv([]);
33+
expect(proxied.child_process.spawn).to.have.not.been.called;
34+
});
35+
36+
function testEnvSetting(...envSettings) {
37+
const ret = crossEnv([...envSettings, 'echo', 'hello world']);
38+
const env = {[getPathVar()]: process.env[getPathVar()]};
39+
envSettings.forEach(setting => {
40+
const [prop, val] = setting.split('=');
41+
env[prop] = val;
42+
});
43+
44+
expect(ret, 'returns what spawn returns').to.equal('spawn-returned');
45+
expect(proxied.child_process.spawn).to.have.been.calledOnce;
46+
expect(proxied.child_process.spawn).to.have.been.calledWith(
47+
'echo', ['hello world'], {stdio: 'inherit', env}
48+
);
49+
}
50+
});

0 commit comments

Comments
 (0)