From afaae5518a03ac098fa773f7c613851b75d98b91 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 1 Oct 2015 17:17:29 -0600 Subject: [PATCH] =?UTF-8?q?feat(lib):=20Initial=20commit!=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 25 ++++++++++++++++ .eslintignore | 5 ++++ .eslintrc | 3 ++ .gitignore | 13 ++++++++ .npmignore | 12 ++++++++ .travis.yml | 23 +++++++++++++++ CODE_OF_CONDUCT.md | 15 ++++++++++ LICENSE | 19 ++++++++++++ README.md | 56 +++++++++++++++++++++++++++++++++++ bin/cross-env.js | 3 ++ other/src.eslintignore | 1 + other/src.eslintrc | 6 ++++ package.json | 67 ++++++++++++++++++++++++++++++++++++++++++ src/index.js | 29 ++++++++++++++++++ src/index.test.js | 50 +++++++++++++++++++++++++++++++ 15 files changed, 327 insertions(+) create mode 100644 .editorconfig create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 LICENSE create mode 100644 README.md create mode 100755 bin/cross-env.js create mode 100644 other/src.eslintignore create mode 100644 other/src.eslintrc create mode 100644 package.json create mode 100644 src/index.js create mode 100644 src/index.test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9e164c3 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..a2b41c8 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +node_modules +coverage +dist +local-examples +other diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..44c2615 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "kentcdodds/test" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e473dc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.idea +*.log +*.iml +*.DS_Store + +node_modules +coverage +nohup.out +dist + +*.ignored.* +*.ignored/ +*.ignored diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..78b0f20 --- /dev/null +++ b/.npmignore @@ -0,0 +1,12 @@ +.idea +node_modules +coverage +local-examples +demo +.editorconfig +.gitignore +.travis.yml +CONTRIBUTING.md +karma.conf.js +webpack.config.js +other diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a919f61 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..c2ebaab --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -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/) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d13cc4b --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..40b8914 --- /dev/null +++ b/README.md @@ -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 diff --git a/bin/cross-env.js b/bin/cross-env.js new file mode 100755 index 0000000..44eb527 --- /dev/null +++ b/bin/cross-env.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('../dist')(process.argv.slice(2)); diff --git a/other/src.eslintignore b/other/src.eslintignore new file mode 100644 index 0000000..a03c4c0 --- /dev/null +++ b/other/src.eslintignore @@ -0,0 +1 @@ +*.test.js diff --git a/other/src.eslintrc b/other/src.eslintrc new file mode 100644 index 0000000..086cd9e --- /dev/null +++ b/other/src.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "kentcdodds", + "env": { + "browser": false + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..66dad76 --- /dev/null +++ b/package.json @@ -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 (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" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..97babb8 --- /dev/null +++ b/src/index.js @@ -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]; +} diff --git a/src/index.test.js b/src/index.test.js new file mode 100644 index 0000000..620b5e1 --- /dev/null +++ b/src/index.test.js @@ -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} + ); + } +});