Skip to content
Permalink
Browse files

first commit

  • Loading branch information...
hughsk committed Jun 12, 2013
0 parents commit ae8aa26b759cd2115eccbed96f70e7bbdceded97
Showing with 234 additions and 0 deletions.
  1. +86 −0 README.md
  2. +17 −0 bin/envify
  3. +50 −0 custom.js
  4. +1 −0 index.js
  5. +31 −0 package.json
  6. +49 −0 test.js
@@ -0,0 +1,86 @@
# envify #

Selectively replace Node-style environment variables with plain strings.
Available as a standalone CLI tool and a
[Browserify](http://browserify.org) v2 transform.

Works best in combination with [uglifyify](http://github.com/hughsk/uglifyify).

## Installation ##

If you're using the module with Browserify:

``` bash
npm install envify browserify
```

Or, for the CLI:

``` bash
sudo npm install -g envify
```

## Usage ##

envify will replace your environment variable checks with ordinary strings -
only the variables you use will be included, so you don't have to worry about,
say, `AWS_SECRET_KEY` leaking through either. Take this example script:

``` javascript
if (process.env.NODE_ENV === "development") {
console.log('development only')
}
```

After running it through envify with `NODE_ENV` set to `production`, you'll
get this:

``` javascript
if ("production" === "development") {
console.log('development only')
}
```

By running this through a good minifier (e.g.
[UglifyJS2](https://github.com/mishoo/UglifyJS)), the above code would be
stripped out completely.

## CLI Usage ##

With browserify:

``` bash
browserify index.js -t envify > bundle.js
```

Or standalone:

``` bash
envify index.js > bundle.js
```

## Module Usage ##

**require('envify')**

Returns a transform stream that updates based on the Node process'
`process.env` object.

**require('envify/custom')([environment])**

If you want to stay away from your environment variables, you can supply
your own object to use in its place:

``` javascript
var browserify = require('browserify')
, envify = require('envify/custom')
, fs = require('fs')
var bundle = browserify('main.js')
, output = fs.createWriteStream('bundle.js')
b.transform(envify({
NODE_ENV: 'development'
}))
b.bundle().pipe(output)
```
@@ -0,0 +1,17 @@
#!/usr/bin/env node

var envify = require('../')
, fs = require('fs')

if (process.argv[2]) {
fs.createReadStream(process.argv[2], { encoding: 'utf8' })
.pipe(envify(process.argv[2]))
.pipe(process.stdout)
} else {
process.stdin.resume()
process.stdin
.pipe(envify(__filename))
.pipe(process.stdout)
}


@@ -0,0 +1,50 @@
var through = require('through')
, falafel = require('falafel')

module.exports = function(env) {
env = env || process.env || {}
return envify

function envify(file) {
var buffer = ''

return through(function(data) {
buffer += data
}, function processFile() {
var output = falafel(buffer, function(node) {
if (!(
node.type === 'Identifier' &&
node.name === 'process' &&
node.parent &&
node.parent.property &&
node.parent.property.type === 'Identifier' &&
node.parent.property.name === 'env' &&
node.parent.parent &&
node.parent.parent.property &&
( node.parent.parent.property.name ||
node.parent.parent.property.value ) &&
( node.parent.parent.parent
? node.parent.parent.parent.type !== 'AssignmentExpression'
: true )
)) return

var key = node.parent.parent.property.name ||
node.parent.parent.property.value

if (!(key in env)) return

node.parent.parent.update(
env[key] ? JSON.stringify(env[key]) :
env[key] === false ? 'false' :
env[key] === null ? 'null' :
env[key] === '' ? '""' :
env[key] === 0 ? '0' :
'undefined'
)
})

this.queue(String(output))
this.queue(null)
})
}
}
@@ -0,0 +1 @@
module.exports = require('./custom')(process.env)
@@ -0,0 +1,31 @@
{
"name": "envify",
"version": "0.0.1",
"description": "Selectively replace Node-style environment variables with plain strings.",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"bin": {
"envify": "bin/envify"
},
"repository": {
"type": "git",
"url": "git://github.com/hughsk/envify.git"
},
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com/)",
"license": "MIT",
"devDependencies": {
"tape": "~0.3.3"
},
"dependencies": {
"through": "~2.3.4",
"falafel": "~0.2.1"
},
"keywords": [
"environment",
"variables",
"browserify",
"configuration"
]
}
49 test.js
@@ -0,0 +1,49 @@
var envify = require('./custom')
, test = require('tape')
, fs = require('fs')

test('Replaces environment variables', function(t) {
var buffer = ''
var stream = envify({
LOREM: 'ipsum'
, HELLO: 'world'
})

stream()
.on('data', function(d) { buffer += d })
.on('end', function() {
t.notEqual(-1, buffer.indexOf('ipsum'))
t.notEqual(-1, buffer.indexOf('world'))
t.end()
})
.end([
'process.env.LOREM'
, 'process.env.HELLO'
].join('\n'))
})

test('Ignores assignments', function(t) {
var buffer = ''
var stream = envify({
LOREM: 'ipsum'
, HELLO: 'world'
, UP: 'down'
})

stream()
.on('data', function(d) { buffer += d })
.on('end', function() {
t.notEqual(-1, buffer.indexOf('world'))
t.notEqual(-1, buffer.indexOf('lorem'))
t.notEqual(-1, buffer.indexOf('process.env["LOREM"]'))
t.notEqual(-1, buffer.indexOf('process.env["HELLO"]'))
t.notEqual(-1, buffer.indexOf('down'))
t.equal(-1, buffer.indexOf('process.env.UP'))
t.end()
})
.end([
'process.env["LOREM"] += "lorem"'
, 'process.env["HELLO"] = process.env["HELLO"] || "world"'
, 'process.env.UP'
].join('\n'))
})

0 comments on commit ae8aa26

Please sign in to comment.
You can’t perform that action at this time.