Skip to content

Instantly share code, notes, and snippets.

@barlowm
Created December 31, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barlowm/2c43c2afa3f3676c77bcc8e4c078e38b to your computer and use it in GitHub Desktop.
Save barlowm/2c43c2afa3f3676c77bcc8e4c078e38b to your computer and use it in GitHub Desktop.
script to generate a node-JS .ENV file from a .sample-env file which can be committed to the application repository
One of the problems with most node applications that require a .ENV file (which are most of them), is that a new user typically doesn't have a clue as to what the .ENV file should contain/look like. And based on the 12 something the .ENV file should not be committed to the application repository.
This simple script (which can be added as a stand alone file and run via a npm script), will take a .sample-env file as a "template" and copy it to a .ENV file. The .ENV file can then be edited and customized by the user as they are setting up their environment.
The only module that is required is "FS" (which in most cases has already been listed as a dependancy of the application. If the FS module is not listed as a dependency of the application simply running:
npm install fs --save
from a command line prompt will install the module.
setup.js:
------------------------------------------------------------------------------
'use strict';
var fs = require('fs');
var rs = fs.createReadStream('.sample-env');
rs.on("error", function(err, fd) {
console.log("createReadStream - Error - Can't read from .sample-env file, please create a sample env file with the name '.sample-env'")
})
.pipe(fs.createWriteStream('.env', {flags:"wx"}))
.on("error", function(err, fd) {
console.log("createWriteStream - Error - %s", err.code);
})
.on("finish", function() {
console.log(".ENV file copied from sample");
})
------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment