Skip to content

Commit ae8aa26

Browse files
committed
first commit
0 parents  commit ae8aa26

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# envify #
2+
3+
Selectively replace Node-style environment variables with plain strings.
4+
Available as a standalone CLI tool and a
5+
[Browserify](http://browserify.org) v2 transform.
6+
7+
Works best in combination with [uglifyify](http://github.com/hughsk/uglifyify).
8+
9+
## Installation ##
10+
11+
If you're using the module with Browserify:
12+
13+
``` bash
14+
npm install envify browserify
15+
```
16+
17+
Or, for the CLI:
18+
19+
``` bash
20+
sudo npm install -g envify
21+
```
22+
23+
## Usage ##
24+
25+
envify will replace your environment variable checks with ordinary strings -
26+
only the variables you use will be included, so you don't have to worry about,
27+
say, `AWS_SECRET_KEY` leaking through either. Take this example script:
28+
29+
``` javascript
30+
if (process.env.NODE_ENV === "development") {
31+
console.log('development only')
32+
}
33+
```
34+
35+
After running it through envify with `NODE_ENV` set to `production`, you'll
36+
get this:
37+
38+
``` javascript
39+
if ("production" === "development") {
40+
console.log('development only')
41+
}
42+
```
43+
44+
By running this through a good minifier (e.g.
45+
[UglifyJS2](https://github.com/mishoo/UglifyJS)), the above code would be
46+
stripped out completely.
47+
48+
## CLI Usage ##
49+
50+
With browserify:
51+
52+
``` bash
53+
browserify index.js -t envify > bundle.js
54+
```
55+
56+
Or standalone:
57+
58+
``` bash
59+
envify index.js > bundle.js
60+
```
61+
62+
## Module Usage ##
63+
64+
**require('envify')**
65+
66+
Returns a transform stream that updates based on the Node process'
67+
`process.env` object.
68+
69+
**require('envify/custom')([environment])**
70+
71+
If you want to stay away from your environment variables, you can supply
72+
your own object to use in its place:
73+
74+
``` javascript
75+
var browserify = require('browserify')
76+
, envify = require('envify/custom')
77+
, fs = require('fs')
78+
79+
var bundle = browserify('main.js')
80+
, output = fs.createWriteStream('bundle.js')
81+
82+
b.transform(envify({
83+
NODE_ENV: 'development'
84+
}))
85+
b.bundle().pipe(output)
86+
```

bin/envify

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
3+
var envify = require('../')
4+
, fs = require('fs')
5+
6+
if (process.argv[2]) {
7+
fs.createReadStream(process.argv[2], { encoding: 'utf8' })
8+
.pipe(envify(process.argv[2]))
9+
.pipe(process.stdout)
10+
} else {
11+
process.stdin.resume()
12+
process.stdin
13+
.pipe(envify(__filename))
14+
.pipe(process.stdout)
15+
}
16+
17+

custom.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var through = require('through')
2+
, falafel = require('falafel')
3+
4+
module.exports = function(env) {
5+
env = env || process.env || {}
6+
return envify
7+
8+
function envify(file) {
9+
var buffer = ''
10+
11+
return through(function(data) {
12+
buffer += data
13+
}, function processFile() {
14+
var output = falafel(buffer, function(node) {
15+
if (!(
16+
node.type === 'Identifier' &&
17+
node.name === 'process' &&
18+
node.parent &&
19+
node.parent.property &&
20+
node.parent.property.type === 'Identifier' &&
21+
node.parent.property.name === 'env' &&
22+
node.parent.parent &&
23+
node.parent.parent.property &&
24+
( node.parent.parent.property.name ||
25+
node.parent.parent.property.value ) &&
26+
( node.parent.parent.parent
27+
? node.parent.parent.parent.type !== 'AssignmentExpression'
28+
: true )
29+
)) return
30+
31+
var key = node.parent.parent.property.name ||
32+
node.parent.parent.property.value
33+
34+
if (!(key in env)) return
35+
36+
node.parent.parent.update(
37+
env[key] ? JSON.stringify(env[key]) :
38+
env[key] === false ? 'false' :
39+
env[key] === null ? 'null' :
40+
env[key] === '' ? '""' :
41+
env[key] === 0 ? '0' :
42+
'undefined'
43+
)
44+
})
45+
46+
this.queue(String(output))
47+
this.queue(null)
48+
})
49+
}
50+
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./custom')(process.env)

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "envify",
3+
"version": "0.0.1",
4+
"description": "Selectively replace Node-style environment variables with plain strings.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"bin": {
10+
"envify": "bin/envify"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/hughsk/envify.git"
15+
},
16+
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com/)",
17+
"license": "MIT",
18+
"devDependencies": {
19+
"tape": "~0.3.3"
20+
},
21+
"dependencies": {
22+
"through": "~2.3.4",
23+
"falafel": "~0.2.1"
24+
},
25+
"keywords": [
26+
"environment",
27+
"variables",
28+
"browserify",
29+
"configuration"
30+
]
31+
}

test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var envify = require('./custom')
2+
, test = require('tape')
3+
, fs = require('fs')
4+
5+
test('Replaces environment variables', function(t) {
6+
var buffer = ''
7+
var stream = envify({
8+
LOREM: 'ipsum'
9+
, HELLO: 'world'
10+
})
11+
12+
stream()
13+
.on('data', function(d) { buffer += d })
14+
.on('end', function() {
15+
t.notEqual(-1, buffer.indexOf('ipsum'))
16+
t.notEqual(-1, buffer.indexOf('world'))
17+
t.end()
18+
})
19+
.end([
20+
'process.env.LOREM'
21+
, 'process.env.HELLO'
22+
].join('\n'))
23+
})
24+
25+
test('Ignores assignments', function(t) {
26+
var buffer = ''
27+
var stream = envify({
28+
LOREM: 'ipsum'
29+
, HELLO: 'world'
30+
, UP: 'down'
31+
})
32+
33+
stream()
34+
.on('data', function(d) { buffer += d })
35+
.on('end', function() {
36+
t.notEqual(-1, buffer.indexOf('world'))
37+
t.notEqual(-1, buffer.indexOf('lorem'))
38+
t.notEqual(-1, buffer.indexOf('process.env["LOREM"]'))
39+
t.notEqual(-1, buffer.indexOf('process.env["HELLO"]'))
40+
t.notEqual(-1, buffer.indexOf('down'))
41+
t.equal(-1, buffer.indexOf('process.env.UP'))
42+
t.end()
43+
})
44+
.end([
45+
'process.env["LOREM"] += "lorem"'
46+
, 'process.env["HELLO"] = process.env["HELLO"] || "world"'
47+
, 'process.env.UP'
48+
].join('\n'))
49+
})

0 commit comments

Comments
 (0)