├── .eslintrc ├── .eslintignore ├── other ├── src.eslintrc ├── test.eslintrc └── common.eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── src ├── index.js └── index.test.js ├── contributing.json ├── CODE_OF_CONDUCT.md ├── package.json └── README.md /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "kentcdodds/test" 3 | } 4 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | dist 4 | local-examples 5 | other 6 | -------------------------------------------------------------------------------- /other/src.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["kentcdodds", "./common.eslintrc"] 3 | } 4 | -------------------------------------------------------------------------------- /other/test.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["kentcdodds/test-angular", "./common.eslintrc"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.log 3 | *.iml 4 | *.DS_Store 5 | 6 | node_modules 7 | coverage 8 | dist 9 | nohup.out 10 | 11 | *.ignored.* 12 | *.ignored/ 13 | *.ignored 14 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 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 13 | -------------------------------------------------------------------------------- /other/common.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "no-invalid-this": 0, // meh... 4 | "func-names": 0, // I wish, but doing this right now would be a royal pain 5 | "new-cap": [ 6 | 2, 7 | { 8 | "newIsCap": true, 9 | "capIsNew": true 10 | } 11 | ], 12 | "max-params": [2, 10], 13 | "max-statements": [2, 30], // TODO bring this down 14 | }, 15 | "globals": { 16 | "VERSION": false 17 | }, 18 | } 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | notifications: 7 | email: false 8 | node_js: 9 | - '4' 10 | before_install: 11 | - npm i -g npm@^3.0.0 12 | before_script: 13 | - npm prune 14 | script: 15 | - npm run lint 16 | - npm t 17 | - npm run check-coverage 18 | after_success: 19 | - npm run report-coverage 20 | - npm run semantic-release 21 | branches: 22 | only: 23 | - master 24 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = setEnv 2 | 3 | function setEnv (options) { 4 | options = options || {} 5 | var argv = options.argv || process.argv 6 | var env = options.env || process.env 7 | var prefix = (options.prefix || '--set-env') + '-' 8 | 9 | argv 10 | .filter(function (a) { return a.indexOf(prefix) === 0 }) 11 | .map(function (a) { return a.substring(prefix.length).split('=') }) 12 | .forEach(function (a) { env[a[0]] = parseToPrimitive(a[1]) }) 13 | } 14 | 15 | function parseToPrimitive (value) { 16 | try { 17 | return JSON.parse(value) 18 | } catch (e) { 19 | return value.toString() 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/index.test.js: -------------------------------------------------------------------------------- 1 | /* global describe, beforeEach, it */ 2 | import {expect} from 'chai' 3 | import setEnv from './index' 4 | 5 | describe(`setEnv`, () => { 6 | let env, argv 7 | beforeEach(() => { 8 | env = {} 9 | argv = getDefaultArgv() 10 | }) 11 | 12 | it(`set variables on the given env`, () => { 13 | setEnv({argv, env}) 14 | envHasDefaults(env) 15 | }) 16 | 17 | it(`should allow for a different prefix`, () => { 18 | const prefix = '--something-else-entirely' 19 | argv = getDefaultArgv(prefix) 20 | setEnv({argv, env, prefix}) 21 | }) 22 | }) 23 | 24 | function getDefaultArgv (prefix = '--set-env') { 25 | return [ 26 | 'node', 27 | 'something', 28 | '--some-other-thing', 29 | `${prefix}-NODE_ENV=test`, 30 | `${prefix}-COVERAGE=true`, 31 | `${prefix}-BUILD_NUMBER=234`, 32 | 'some additional things' 33 | ] 34 | } 35 | 36 | function envHasDefaults (env) { 37 | expect(env.NODE_ENV).to.equal('test') 38 | expect(env.COVERAGE).to.equal(true) 39 | expect(env.BUILD_NUMBER).to.equal(234) 40 | } 41 | 42 | -------------------------------------------------------------------------------- /contributing.json: -------------------------------------------------------------------------------- 1 | // https://gitmagic.io/rules 2 | { 3 | "commit": { 4 | "subject_cannot_be_empty": true, 5 | "subject_must_be_longer_than": 4, 6 | "subject_must_be_shorter_than": 101, 7 | "subject_lines_must_be_shorter_than": 51, 8 | "subject_must_be_single_line": true, 9 | "subject_must_be_in_tense": "imperative", 10 | "subject_must_start_with_case": "lower", 11 | "subject_must_not_end_with_dot": true, 12 | 13 | "body_lines_must_be_shorter_than": 73 14 | }, 15 | "pull_request": { 16 | "subject_cannot_be_empty": true, 17 | "subject_must_be_longer_than": 4, 18 | "subject_must_be_shorter_than": 101, 19 | "subject_must_be_in_tense": "imperative", 20 | "subject_must_start_with_case": "upper", 21 | "subject_must_not_end_with_dot": true, 22 | 23 | "body_cannot_be_empty": true, 24 | "body_must_include_verification_steps": true, 25 | "body_must_include_screenshot": ["html", "css"] 26 | }, 27 | "issue": { 28 | "subject_cannot_be_empty": true, 29 | "subject_must_be_longer_than": 4, 30 | "subject_must_be_shorter_than": 101, 31 | "subject_must_be_in_tense": "imperative", 32 | "subject_must_start_with_case": "upper", 33 | "subject_must_not_end_with_dot": true, 34 | 35 | "body_cannot_be_empty": true, 36 | "body_must_include_reproduction_steps": true 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 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/) 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "argv-set-env", 3 | "description": "Use in your config files to set environment variables cross-platform", 4 | "main": "src/index.js", 5 | "scripts": { 6 | "commit": "git-cz", 7 | "lint": "standard src/**/*.js", 8 | "start": "npm run test:watch -s", 9 | "test": "istanbul cover -x *.test.js _mocha -- -R spec src/index.test.js --compilers js:babel/register", 10 | "test:watch": "mocha -R spec src/index.test.js -w --compilers js:babel/register", 11 | "check-coverage": "istanbul check-coverage --lines 100 --functions 100 --branches 62", 12 | "report-coverage": "cat ./coverage/lcov.info | codecov", 13 | "semantic-release": "semantic-release pre && npm publish && semantic-release post" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/kentcdodds/argv-set-env.git" 18 | }, 19 | "keywords": [ 20 | "set-env", 21 | "environment variables", 22 | "cross platform" 23 | ], 24 | "author": "Kent C. Dodds (http://kentcdodds.com/)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/kentcdodds/argv-set-env/issues" 28 | }, 29 | "homepage": "https://github.com/kentcdodds/argv-set-env#readme", 30 | "devDependencies": { 31 | "babel": "5.8.23", 32 | "chai": "3.3.0", 33 | "codecov": "1.0.1", 34 | "commitizen": "2.1.0", 35 | "cz-conventional-changelog": "1.1.4", 36 | "estraverse": "4.1.1", 37 | "ghooks": "0.3.2", 38 | "istanbul": "0.4.0", 39 | "mocha": "2.3.3", 40 | "semantic-release": "^4.3.5", 41 | "standard": "5.3.1", 42 | "validate-commit-msg": "1.0.0" 43 | }, 44 | "czConfig": { 45 | "path": "node_modules/cz-conventional-changelog" 46 | }, 47 | "config": { 48 | "ghooks": { 49 | "commit-msg": "./node_modules/.bin/validate-commit-msg && npm run lint && npm t && npm run check-coverage" 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | You might actually want to look at my other utility [cross-env](http://npm.im/cross-env). It's probably what you're really looking for :-) 2 | 3 | # argv-set-env 4 | 5 | Status: 6 | [![npm version](https://img.shields.io/npm/v/argv-set-env.svg?style=flat-square)](https://www.npmjs.org/package/argv-set-env) 7 | [![npm downloads](https://img.shields.io/npm/dm/argv-set-env.svg?style=flat-square)](http://npm-stat.com/charts.html?package=argv-set-env&from=2015-09-01) 8 | [![Build Status](https://img.shields.io/travis/kentcdodds/argv-set-env.svg?style=flat-square)](https://travis-ci.org/kentcdodds/argv-set-env) 9 | [![Code Coverage](https://img.shields.io/codecov/c/github/kentcdodds/argv-set-env.svg?style=flat-square)](https://codecov.io/github/kentcdodds/argv-set-env) 10 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) 11 | 12 | Use in your config files to set environment variables cross-platform. 13 | 14 | ## Usage 15 | 16 | I use this with my npm scripts: 17 | 18 | ```javascript 19 | { 20 | "scripts": { 21 | "build": "webpack --set-env-NODE_ENV=production", 22 | "test": "karma start --set-env-COVERAGE=true --set-env-NODE_ENV=test" 23 | } 24 | } 25 | ``` 26 | 27 | And then in my `webpack.config.js` file, at the very top I do: 28 | 29 | ```javascript 30 | require('argv-set-env')() 31 | ``` 32 | 33 | Which in the case of `build` would set `NODE_ENV` to the string `'production'` and in the case of 34 | `test` it would set `NODE_ENV` to the string `'test'` and `COVERAGE` to the boolean `true`. 35 | 36 | ### Options 37 | 38 | You can optionally pass `options`: 39 | 40 | ```javascript 41 | require('argv-set-env')({ 42 | // I'm setting these to the defaults here 43 | // you could set them to whatever you want 44 | env: process.env, // the object to have variables set on 45 | argv: process.argv, // the array of arguments which have the arguments defined 46 | prefix: '--set-env', // the prefix used in the array of arguments defining variables to set 47 | }) 48 | ``` 49 | 50 | ## LICENSE 51 | 52 | MIT 53 | 54 | --------------------------------------------------------------------------------