Skip to content

Commit

Permalink
feat: add TypeScript defs
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Nov 20, 2021
1 parent 67b561d commit 4367af0
Show file tree
Hide file tree
Showing 7 changed files with 5,738 additions and 3,339 deletions.
7 changes: 4 additions & 3 deletions .eslintrc
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
}
}
10 changes: 0 additions & 10 deletions .npmignore

This file was deleted.

14 changes: 14 additions & 0 deletions lib/index.d.ts
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};
157 changes: 89 additions & 68 deletions lib/index.js
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);
}

48 changes: 27 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"defaultenv": "lib/index.js"
},
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"lint": "eslint lib test --cache",
"lint:fix": "eslint --fix lib test --cache",
Expand All @@ -19,7 +22,7 @@
"commitmsg": "commitlint -e $GIT_PARAMS",
"precommit": "npm run lint && flow",
"prepush": "npm test",
"prepublishOnly": "npm run lint && flow && npm test",
"prepublishOnly": "npm run lint && flow && tsc && npm test",
"open:coverage": "open coverage/lcov-report/index.html",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
Expand Down Expand Up @@ -49,29 +52,32 @@
},
"homepage": "https://github.com/jcoreio/defaultenv#readme",
"devDependencies": {
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@jedwards1211/commitlint-config": "^1.0.0",
"@jedwards1211/eslint-config-es5": "^1.0.0",
"@jedwards1211/eslint-config-flow": "^1.0.0",
"chai": "^4.1.2",
"codecov": "^3.0.0",
"copy": "^0.3.0",
"eslint": "^3.18.0",
"eslint-plugin-flowtype": "^2.30.4",
"eslint-watch": "^3.0.1",
"flow-bin": "^0.71.0",
"flow-watch": "^1.1.1",
"husky": "^0.14.3",
"@commitlint/cli": "^15.0.0",
"@commitlint/config-conventional": "^15.0.0",
"@jedwards1211/commitlint-config": "^1.0.2",
"@jedwards1211/eslint-config-es5": "^1.0.1",
"@jedwards1211/eslint-config-flow": "^3.0.1",
"chai": "^4.3.4",
"codecov": "^3.8.3",
"copy": "^0.3.2",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-watch": "^8.0.0",
"flow-bin": "^0.165.1",
"flow-watch": "^2.0.0",
"husky": "^7.0.4",
"istanbul": "^0.4.5",
"mocha": "^5.0.0",
"nyc": "^11.4.1",
"rimraf": "^2.6.1",
"semantic-release": "^15.4.0",
"travis-deploy-once": "^4.3.1"
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"semantic-release": "^18.0.0",
"travis-deploy-once": "^5.0.11",
"typescript": "^4.5.2"
},
"dependencies": {
"dotenv": "^4.0.0"
"dotenv": "^10.0.0"
},
"optionalDependencies": {
"@jcoreio/kexec": "^4.0.0"
Expand Down
Loading

0 comments on commit 4367af0

Please sign in to comment.