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

Commit

Permalink
feat(lib): Initial commit! ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Oct 1, 2015
0 parents commit afaae55
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# all files
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
max_line_length = 120

[*.js]
quote_type = single
curly_bracket_next_line = false
spaces_around_operators = true
spaces_around_brackets = inside
indent_brace_style = BSD KNF

# HTML
[*.html]
quote_type = double
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
coverage
dist
local-examples
other
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "kentcdodds/test"
}
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.idea
*.log
*.iml
*.DS_Store

node_modules
coverage
nohup.out
dist

*.ignored.*
*.ignored/
*.ignored
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
node_modules
coverage
local-examples
demo
.editorconfig
.gitignore
.travis.yml
CONTRIBUTING.md
karma.conf.js
webpack.config.js
other
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
branches:
only:
- master
notifications:
email: false
node_js:
- iojs
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
script:
- npm run eslint
- npm t
- npm run check-coverage
after_success:
- npm run report-coverage
- npm run semantic-release
15 changes: 15 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributor Code of Conduct

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.

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.

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.

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.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

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/)
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# cross-env

Status:
[![npm version](https://img.shields.io/npm/v/cross-env.svg?style=flat-square)](https://www.npmjs.org/package/cross-env)
[![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)
[![Build Status](https://img.shields.io/travis/kentcdodds/cross-env.svg?style=flat-square)](https://travis-ci.org/kentcdodds/cross-env)
[![Code Coverage](https://img.shields.io/codecov/c/github/kentcdodds/cross-env.svg?style=flat-square)](https://codecov.io/github/kentcdodds/cross-env)

This micro-lib allows you to provide a script which sets an environment using unix style and have it work on windows too

## Usage

I use this in my npm scripts:

```json
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}
```

Ultimately, the command that is executed (using `spawn`) is:

```
webpack --config build/webpack.config.js
```

The `NODE_ENV` environment variable will be set by `cross-env`


## Why?

Windows will choke when you set environment variables with `NODE_ENV=production` like that. This makes it so you can
have a single command without worrying about setting the environment variable properly for the platform. Just set it
like you would if it's running on a unix system, and `cross-env` will take care of setting it properly.

## Known limitations

If you plan to do something like this:

```
cross-env FOO=bar && echo $FOO
```

And expect it to output `bar` you're going to be sad, for two reasons:

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.
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)

The main use case for this package is to simply run another script which will (itself) respond to the environment
variable. These limitations are not a problem in that scenario (like in the example).

## LICENSE

MIT
3 changes: 3 additions & 0 deletions bin/cross-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist')(process.argv.slice(2));
1 change: 1 addition & 0 deletions other/src.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.test.js
6 changes: 6 additions & 0 deletions other/src.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "kentcdodds",
"env": {
"browser": false
}
}
67 changes: 67 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "cross-env",
"version": "0.0.0-semanticallly-released.0",
"description": "Run commands that set environment variables across platforms",
"main": "src/index.js",
"bin": {
"cross-env": "bin/cross-env.js"
},
"scripts": {
"start": "npm run test:watch",
"prebuild": "trash dist && mkdir dist",
"build": "cd src && babel index.js -d ../dist && cd ..",
"commit": "git-cz",
"eslint": "eslint src/ -c other/src.eslintrc --ignore-path other/src.eslintignore && eslint src/*.test.js",
"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 _mocha -- -R spec src/index.test.js --compilers js:babel/register",
"prepublish": "npm run build",
"postpublish": "publish-latest",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "https://github.com/kentcdodds/cross-env.git"
},
"keywords": [
"environment variables",
"cross platform"
],
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/kentcdodds/cross-env/issues"
},
"homepage": "https://github.com/kentcdodds/cross-env#readme",
"devDependencies": {
"babel": "5.8.23",
"chai": "3.3.0",
"codecov.io": "0.1.6",
"commitizen": "1.0.5",
"cz-conventional-changelog": "1.1.2",
"eslint": "1.5.1",
"eslint-config-kentcdodds": "4.0.1",
"eslint-plugin-mocha": "1.0.0",
"ghooks": "0.3.2",
"istanbul": "0.3.21",
"mocha": "2.3.3",
"proxyquire": "1.7.2",
"semantic-release": "4.3.5",
"sinon": "1.17.1",
"sinon-chai": "2.8.0",
"trash": "2.0.0",
"validate-commit-msg": "1.0.0"
},
"config": {
"ghooks": {
"commit-msg": "./node_modules/.bin/validate-commit-msg && npm run eslint && npm t && npm run check-coverage && echo 'pre-commit checks good 👍'"
}
},
"czConfig": {
"path": "node_modules/cz-conventional-changelog/"
},
"dependencies": {
"add-to-path": "1.1.2"
}
}
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {spawn} from 'child_process';
import getPathVar from 'add-to-path/dist/get-path-var';
export default crossEnv;

const envSetterRegex = /(\w+)=(\w+)/;

function crossEnv(args) {
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args);
if (command) {
return spawn(command, commandArgs, {stdio: 'inherit', env});
}
}

function getCommandArgsAndEnvVars(args) {
let command;
const envVars = {[getPathVar()]: process.env[getPathVar()]};
const commandArgs = args.slice();
while (commandArgs.length) {
const shifted = commandArgs.shift();
const match = envSetterRegex.exec(shifted);
if (match) {
envVars[match[1]] = match[2];
} else {
command = shifted;
break;
}
}
return [command, commandArgs, envVars];
}
50 changes: 50 additions & 0 deletions src/index.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 sinon from 'sinon';
import proxyquire from 'proxyquire';
import getPathVar from 'add-to-path/dist/get-path-var';
chai.use(sinonChai);

const {expect} = chai;
const proxied = {
child_process: { // eslint-disable-line camelcase
spawn: sinon.spy(() => 'spawn-returned')
}
};

const crossEnv = proxyquire('./index', proxied);

describe(`cross-env`, () => {

beforeEach(() => {
proxied.child_process.spawn.reset();
});

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

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

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

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

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

0 comments on commit afaae55

Please sign in to comment.