Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Copied files from rbenv-vars and updated references
  • Loading branch information
OiNutter committed Apr 18, 2014
0 parents commit 38a807a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2014 Will McKenzie

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.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# nodenv-vars

This is a plugin for [nodenv](https://github.com/OiNutter/nodenv)
that lets you set global and project-specific environment variables
before spawning Node processes.

## Installation

To install nodenv-vars, clone this repository into your
`~/.nodenv/plugins` directory. (You'll need a recent version of nodenv
that supports plugin bundles.)

$ mkdir -p ~/.nodenv/plugins
$ cd ~/.nodenv/plugins
$ git clone https://github.com/OiNutter/nodenv-vars.git

## Usage

Define environment variables in an `.nodenv-vars` file in your project,
one variable per line, in the format `VAR=value`. For example:

HUBOT_CAMPFIRE_ACCOUNT=nodenv
HUBOT_CAMPFIRE_TOKEN=somerandomtoken
HUBOT_CAMPFIRE_ROOMS=some,campfire,rooms

You can perform variable substitution with the traditional `$`
syntax. For example, to append to `HUBOT_CAMPFIRE_ROOMS`:

HUBOT_CAMPFIRE_ROOMS=$HUBOT_CAMPFIRE_ROOMS:some,extra,rooms

You may also have conditional variable assignments, such that a
variable will **only** be set if it is not already defined or is blank:

USER_ID?=OiNutter

In the above case, `USER_ID` will only be set if `$USER_ID` is
currently empty (i.e., if `[ -z "$USER_ID" ]` is true).

Spaces are allowed in values; quoting is not necessary. Expansion and
command substitution are not allowed. Lines beginning with `#` or any
lines not in the format VAR=value will be ignored.

Variables specified in the `~/.nodenv/vars` file will be set
first. Then variables specified in `.nodenv-vars` files in any parent
directories of the current directory will be set. Variables from the
`.nodenv-vars` file in the current directory are set last.

Use the `nodenv vars` command to print all environment variables in the
order they'll be set.

## Version History

**1.0.0** (April 18, 2014)

* Initial public release. (Copied from [rbenv-vars](http://github.com/sstephenson/rbenv-vars))

## License

© 2014 Will McKenzie. Released under the MIT license. See
`LICENSE` for details.
64 changes: 64 additions & 0 deletions bin/nodenv-vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash
#
# Usage: nodenv vars
#
# Prints all nodenv-vars environment variables applicable to the
# current working directory in the order they will be set. The output
# format is a script that may be passed to `eval` in a Bourne-
# compatible shell.
#
# For more information on nodenv-vars, see:
# https://github.com/OiNutter/nodenv-vars#readme

set -e
[ -n "$NODENV_DEBUG" ] && set -x

if [ "$1" = "--version" ] || [ "$1" = "-v" ]; then
echo "nodenv-vars 1.2.0"
exit
fi

traverse-nodenv-vars-files() {
local root="$1"
local results=""

while [ -n "$root" ]; do
if [ -e "${root}/.nodenv-vars" ]; then
results="${root}/.nodenv-vars"$'\n'"$results"
fi
root="${root%/*}"
done

if [ -n "$results" ]; then
echo -n "$results"
else
return 1
fi
}

find-nodenv-vars-files() {
if [ -e "${NODENV_ROOT}/vars" ]; then
echo "${NODENV_ROOT}/vars"
fi

traverse-nodenv-vars-files "$NODENV_DIR" ||
[ "$NODENV_DIR" = "$PWD" ] || traverse-nodenv-vars-files "$PWD"
}

sanitize-vars() {
sed \
-e "/^[ "$'\t'"]*[A-Za-z_][0-9A-Za-z_]*?\{0,1\}=/ !d" \
-e "s/'/'\\\\''/g" \
-e "s/\(\\\\\\\$\)/'\\1'/g" \
-e "s/\\\\\\\\/\\\\/g" \
-e "s/\(\\\$[0-9A-Za-z_][0-9A-Za-z_]*\)/'\\1'/g" \
-e "s/\(\\\${[0-9A-Za-z_][0-9A-Za-z_]*}\)/'\\1'/g" \
-e "s/^[ "$'\t'"]*\([A-Za-z_][0-9A-Za-z_]*?\{0,1\}\)=\(.*\)$/export \\1='\\2'/" \
-e "s/export \([A-Za-z_][0-9A-Za-z_]*\)?=/[ -n \"\$\\1\" ] || export \\1=/g"
}

while read file; do
echo "# $file"
{ cat "$file"; echo; } | sanitize-vars
echo
done < <( find-nodenv-vars-files )
4 changes: 4 additions & 0 deletions etc/nodenv.d/exec/nodenv-vars.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

eval "$(nodenv-vars)"

0 comments on commit 38a807a

Please sign in to comment.