-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67b561d
commit 4367af0
Showing
7 changed files
with
5,738 additions
and
3,339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
{ | ||
"extends": [ | ||
"@jedwards1211/eslint-config-es5", "@jedwards1211/eslint-config-flow" | ||
"@jedwards1211/eslint-config-es5", | ||
"@jedwards1211/eslint-config-flow", | ||
"prettier" | ||
], | ||
"ecmaVersion": 5, | ||
"env": { | ||
"es6": false, | ||
"es2017": true, | ||
"node": true | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type Options = { | ||
force?: boolean, | ||
print?: boolean, | ||
noDotenv?: boolean, | ||
noExport?: boolean, | ||
} | ||
|
||
export type EnvFileAPI = { | ||
get: (key: string) => string | null | undefined, | ||
setDefault: (key: string, value: string) => void, | ||
setDefaults: (defaults: {[key: string]: string}) => void, | ||
} | ||
|
||
export default function defaultEnv(envFiles: Array<string>, options?: Options): {[varname: string]: string}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,163 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
"use strict"; | ||
|
||
var fs = require('fs') | ||
var path = require('path') | ||
var dotenv = require('dotenv') | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var dotenv = require("dotenv"); | ||
|
||
function defaultEnv(envFiles, options) { | ||
options = options || {} | ||
var setvars = {} | ||
var file | ||
options = options || {}; | ||
var setvars = {}; | ||
var file; | ||
|
||
function get(key) { | ||
return setvars[key] != null ? setvars[key] : process.env[key] | ||
return setvars[key] != null ? setvars[key] : process.env[key]; | ||
} | ||
function setDefault(key, value) { | ||
if (typeof key !== 'string') { | ||
throw new Error(file + ' is invalid; keys should be strings, got ' + key + ' instead') | ||
if (typeof key !== "string") { | ||
throw new Error( | ||
file + " is invalid; keys should be strings, got " + key + " instead" | ||
); | ||
} | ||
if (typeof value !== 'string') { | ||
throw new Error(file + ' is invalid; value for ' + key + ' should be a string, got ' + value + ' instead') | ||
if (typeof value !== "string") { | ||
throw new Error( | ||
file + | ||
" is invalid; value for " + | ||
key + | ||
" should be a string, got " + | ||
value + | ||
" instead" | ||
); | ||
} | ||
if (!setvars.hasOwnProperty(key) && (!process.env.hasOwnProperty(key) || options.force || options.print)) { | ||
setvars[key] = value | ||
if (!options.noExport) process.env[key] = value | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(setvars, key) && | ||
(!Object.prototype.hasOwnProperty.call(process.env, key) || | ||
options.force || | ||
options.print) | ||
) { | ||
setvars[key] = value; | ||
if (!options.noExport) process.env[key] = value; | ||
} | ||
} | ||
function setDefaults(defaults) { | ||
for (var key in defaults) { | ||
setDefault(key, defaults[key]) | ||
setDefault(key, defaults[key]); | ||
} | ||
} | ||
|
||
var key | ||
var key; | ||
if (!options.noDotenv) { | ||
var parsed = dotenv.config().parsed | ||
setDefaults(parsed) | ||
var parsed = dotenv.config().parsed; | ||
setDefaults(parsed); | ||
} | ||
for (var i = 0; i < envFiles.length; i++) { | ||
file = envFiles[i] | ||
var defaultValues | ||
file = envFiles[i]; | ||
var defaultValues; | ||
if (/\.js(on)?$/.test(file)) { | ||
defaultValues = require(path.resolve(process.cwd(), file)) | ||
defaultValues = require(path.resolve(process.cwd(), file)); | ||
if (defaultValues instanceof Function) { | ||
defaultValues({ | ||
get: get, | ||
setDefault: setDefault, | ||
setDefaults: setDefaults, | ||
}) | ||
}); | ||
} else if (defaultValues instanceof Object) { | ||
setDefaults(defaultValues) | ||
setDefaults(defaultValues); | ||
} else { | ||
throw new Error(file + ' is invalid; it should export an object or a function, got ' + defaultValues + ' instead') | ||
throw new Error( | ||
file + | ||
" is invalid; it should export an object or a function, got " + | ||
defaultValues + | ||
" instead" | ||
); | ||
} | ||
} else { | ||
defaultValues = dotenv.parse(fs.readFileSync(file, 'utf8')) | ||
setDefaults(defaultValues) | ||
defaultValues = dotenv.parse(fs.readFileSync(file, "utf8")); | ||
setDefaults(defaultValues); | ||
} | ||
} | ||
if (options.print) { | ||
for (key in setvars) console.log('export ' + key + '=' + setvars[key]) // eslint-disable-line no-console | ||
for (key in setvars) console.log("export " + key + "=" + setvars[key]); // eslint-disable-line no-console | ||
} | ||
return setvars | ||
return setvars; | ||
} | ||
|
||
function run(command, args) { | ||
try { | ||
var kexec = require('kexec') | ||
kexec(command, args) | ||
var kexec = require("kexec"); | ||
kexec(command, args); | ||
} catch (err) { | ||
if (err.code !== 'MODULE_NOT_FOUND') throw err | ||
if (err.code !== "MODULE_NOT_FOUND") throw err; | ||
|
||
var child_process = require('child_process') | ||
var proc = child_process.spawn(command, args, { stdio: 'inherit' }) | ||
proc.on('exit', function (code, signal) { | ||
process.on('exit', function () { | ||
var child_process = require("child_process"); | ||
var proc = child_process.spawn(command, args, { stdio: "inherit" }); | ||
proc.on("exit", function (code, signal) { | ||
process.on("exit", function () { | ||
if (signal) { | ||
process.kill(process.pid, signal) | ||
process.kill(process.pid, signal); | ||
} else { | ||
process.exit(code) | ||
process.exit(code); | ||
} | ||
}) | ||
}) | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = defaultEnv | ||
module.exports = defaultEnv; | ||
|
||
if (!module.parent) { | ||
var argv = process.argv.slice(2) | ||
var argv = process.argv.slice(2); | ||
|
||
var command, commandArgs, envFiles, myArgs | ||
var command, commandArgs, envFiles, myArgs; | ||
|
||
var dashIndex = argv.indexOf('--') | ||
var dashIndex = argv.indexOf("--"); | ||
if (dashIndex >= 0) { | ||
command = argv[dashIndex + 1] | ||
commandArgs = argv.slice(dashIndex + 2) | ||
command = argv[dashIndex + 1]; | ||
commandArgs = argv.slice(dashIndex + 2); | ||
envFiles = argv.slice(0, dashIndex).filter(function (file) { | ||
return !file.startsWith('-') | ||
}) | ||
return !file.startsWith("-"); | ||
}); | ||
myArgs = argv.slice(0, dashIndex).filter(function (arg) { | ||
return arg.startsWith('-') | ||
}) | ||
return arg.startsWith("-"); | ||
}); | ||
} else { | ||
for (var envFileIndex = 0; envFileIndex < argv.length; envFileIndex++) { | ||
if (!argv[envFileIndex].startsWith('-')) break | ||
if (!argv[envFileIndex].startsWith("-")) break; | ||
} | ||
myArgs = argv.slice(0, envFileIndex) | ||
envFiles = [argv[envFileIndex]] | ||
command = argv[envFileIndex + 1] | ||
commandArgs = argv.slice(envFileIndex + 2) | ||
if (myArgs.indexOf('-p') >= 0 || myArgs.indexOf('--print') >= 0) { | ||
envFiles = argv.slice(envFileIndex) | ||
myArgs = argv.slice(0, envFileIndex); | ||
envFiles = [argv[envFileIndex]]; | ||
command = argv[envFileIndex + 1]; | ||
commandArgs = argv.slice(envFileIndex + 2); | ||
if (myArgs.indexOf("-p") >= 0 || myArgs.indexOf("--print") >= 0) { | ||
envFiles = argv.slice(envFileIndex); | ||
} | ||
} | ||
|
||
var options = { | ||
noDotenv: myArgs.indexOf('--no-dotenv') >= 0, | ||
force: myArgs.indexOf('-f') >= 0 || myArgs.indexOf('--force') >= 0, | ||
print: myArgs.indexOf('-p') >= 0 || myArgs.indexOf('--print') >= 0, | ||
} | ||
noDotenv: myArgs.indexOf("--no-dotenv") >= 0, | ||
force: myArgs.indexOf("-f") >= 0 || myArgs.indexOf("--force") >= 0, | ||
print: myArgs.indexOf("-p") >= 0 || myArgs.indexOf("--print") >= 0, | ||
}; | ||
|
||
if (!command && !options.print) { | ||
/* eslint-disable no-console */ | ||
process.stdout.write(fs.readFileSync(require.resolve('./usage.txt'), 'utf8')) | ||
process.exit(0) | ||
process.stdout.write( | ||
fs.readFileSync(require.resolve("./usage.txt"), "utf8") | ||
); | ||
process.exit(0); | ||
/* eslint-enable no-console */ | ||
} | ||
|
||
try { | ||
defaultEnv(envFiles, options) | ||
defaultEnv(envFiles, options); | ||
} catch (error) { | ||
console.error(error.stack) // eslint-disable-line no-console | ||
process.exit(1) | ||
console.error(error.stack); // eslint-disable-line no-console | ||
process.exit(1); | ||
} | ||
|
||
if (options.print) process.exit(0) | ||
if (options.print) process.exit(0); | ||
|
||
run(command, commandArgs) | ||
run(command, commandArgs); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.