confederate
Advanced tools
Comparing version 0.8.9 to 0.9.1
{ | ||
"name": "confederate", | ||
"version": "0.8.9", | ||
"description": "Narrow road conf-loader suitable for confederates of apps (for instance suits of microserviceses)", | ||
"main": "dist-cjs/confederate.js", | ||
"module": "dist-esmod/confederate.js", | ||
"types": "dist-esmod/confederate.d.ts", | ||
"version": "0.9.1", | ||
"description": "Narrow road conf-loader suitable for confederates of apps (for instance suits of microserviceses). Dead simple story.", | ||
"main": "dist/confederate.js", | ||
"types": "dist/confederate.d.ts", | ||
"scripts": { | ||
@@ -12,13 +11,13 @@ "prepublish": "make", | ||
}, | ||
"author": "Oscar Campbell / Wace-P Dev Team", | ||
"author": "Oscar Campbell / Wace Dev Team", | ||
"license": "ISC", | ||
"dependencies": { | ||
"chalk": "^2.3.0", | ||
"cabler": "^0.8.1" | ||
"cabler": "^0.8.1", | ||
"chalk": "^2.4.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^8.9.1", | ||
"@types/chalk": "^2.2.0", | ||
"typescript": "^2.7.1" | ||
"@types/node": "^8.10.45", | ||
"typescript": "^3.3.4000" | ||
} | ||
} |
@@ -7,49 +7,54 @@ import * as fs from "fs" | ||
export function Confederate(appName: string, confFilePath?: string) { | ||
export function Confederate(app_name: string, conf_path?: string): JsonObject { | ||
const args = process.argv.slice(2) | ||
const maybeFirstArg: string = args[0] || "" | ||
const maybe_first_arg: string = args[0] || "" | ||
// Run-time configured takes precedence over defaulted in function-args | ||
if (maybeFirstArg.startsWith("--conf")) { | ||
if (maybeFirstArg.length === 6) { | ||
confFilePath = args[1] | ||
if (maybe_first_arg.startsWith("--conf")) { | ||
if (maybe_first_arg.length === 6) { | ||
conf_path = args[1] | ||
} else { | ||
confFilePath = maybeFirstArg.split("=")[1] | ||
conf_path = maybe_first_arg.split("=")[1] | ||
} | ||
} else if (process.env.CONF) { | ||
confFilePath = process.env.CONF | ||
conf_path = process.env.CONF | ||
} | ||
if (!conf_path) { | ||
log.die( | ||
'No configuration path has been passed for this instance of "' + | ||
app_name + | ||
'".\n' + | ||
"Use `app --conf the/path`, `app --conf=the/path` or `CONF=the/path app`" | ||
) | ||
return {} // typescript still doesn't get "never" | ||
} | ||
if (!confFilePath) { | ||
log.die("No configuration path has been passed for this instance. Use `cmd --conf the/path` or `CONF=the/path cmd`") | ||
} else { | ||
// The else clause is needed because process.exit() doesn't seem to be | ||
// typed as NoReturn, or TypeScript inference is just too shallow, don't | ||
// know which | ||
try { | ||
const stat = fs.statSync(confFilePath) | ||
let conf: JsonObject = {} | ||
if (stat.isDirectory()) { | ||
try { | ||
conf = loadConfFile(confFilePath + "/defaults.conf.json", true) | ||
} catch (e) { | ||
// loadConfFile logs the an error | ||
process.exit(1) | ||
} | ||
mergeHash(conf, loadConfFile(confFilePath + "/" + appName + ".conf.json", false)) | ||
} else { | ||
try { | ||
conf = loadConfFile(confFilePath, true) | ||
} catch (e) { | ||
log.die("Errored: ", e) | ||
} | ||
// The else clause is needed because process.exit() doesn't seem to be | ||
// typed as NoReturn, or TypeScript inference is just too shallow, don't | ||
// know which | ||
try { | ||
const stat = fs.statSync(conf_path) | ||
let conf: JsonObject = {} | ||
if (stat.isDirectory()) { | ||
try { | ||
conf = load_conf_file(conf_path + "/defaults.conf.json", true) | ||
} catch (e) { | ||
// load_conf_file logs the an error | ||
process.exit(1) | ||
} | ||
return conf | ||
} catch (e) { | ||
log.die("Error when checking conf-path '" + confFilePath + ": ", e) | ||
merge_hash( | ||
conf, | ||
load_conf_file(conf_path + "/" + app_name + ".conf.json", false) | ||
) | ||
} else { | ||
try { | ||
conf = load_conf_file(conf_path, true) | ||
} catch (e) { | ||
log.die("Errored: ", e) | ||
} | ||
} | ||
return conf | ||
} catch (e) { | ||
log.die("Error when checking conf-path '" + conf_path + ": ", e) | ||
} | ||
@@ -61,11 +66,16 @@ | ||
function loadConfFile(confFilePath: string, requireConf = false): JsonObject { | ||
let confDoc = "{}" | ||
function load_conf_file(conf_path: string, required_conf = false): JsonObject { | ||
let conf_doc = "{}" | ||
try { | ||
// Checking for existance first is an anti-pattern, so we don't | ||
confDoc = fs.readFileSync(confFilePath).toString() | ||
conf_doc = fs.readFileSync(conf_path).toString() | ||
} catch (e) { | ||
if (requireConf) { | ||
log.e("Configuration file not found at '" + confFilePath + "', double check stuff!", e) | ||
if (required_conf) { | ||
log.e( | ||
"Configuration file not found at '" + | ||
conf_path + | ||
"', double check stuff!", | ||
e | ||
) | ||
throw e | ||
@@ -76,5 +86,13 @@ } | ||
try { | ||
return JSON.parse(confDoc) | ||
return JSON.parse(conf_doc) | ||
} catch (e) { | ||
log.e("Configuration file '" + confFilePath + "' didn't parse as correct JSON - double check it!", e, "\n", "conf document:", confDoc) | ||
log.e( | ||
"Configuration file '" + | ||
conf_path + | ||
"' didn't parse as correct JSON - double check it!", | ||
e, | ||
"\n", | ||
"conf document:", | ||
conf_doc | ||
) | ||
throw e | ||
@@ -84,3 +102,3 @@ } | ||
function mergeHash(obj: JsonObject, src: JsonObject): JsonObject { | ||
function merge_hash(obj: JsonObject, src: JsonObject): JsonObject { | ||
const own = {}.hasOwnProperty | ||
@@ -87,0 +105,0 @@ for (const key in src) { |
@@ -11,3 +11,3 @@ { | ||
"declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
// "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
"sourceMap": false, /* Generates corresponding '.map' file. */ | ||
// "outFile": "./", /* Concatenate and emit output to single file. */ | ||
@@ -54,4 +54,4 @@ // "outDir": "./", /* Redirect output structure to the directory. */ | ||
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ | ||
"inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ | ||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ | ||
"inlineSourceMap": false, /* Emit a single file with source maps instead of having a separate file. */ | ||
"inlineSources": false, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ | ||
@@ -58,0 +58,0 @@ /* Experimental Options */ |
Sorry, the diff of this file is not supported yet
16
362
18525
Updatedchalk@^2.4.1