@code_monk/json2bash
Advanced tools
Comparing version 1.0.7 to 1.0.8
86
index.js
@@ -7,6 +7,53 @@ | ||
const bashify = (keybits,k,v) => { | ||
const defaults = { | ||
"quote_character": "\'", | ||
"omit_variable_names_beginning_with_underscore": true, | ||
"disallow_two_underscores_in_a_row": true, | ||
"replace_illegal_chars_with": '', | ||
"arrays_become": String, | ||
"posix_mode": false | ||
}; | ||
const bashify = (keybits,k,v,opts) => { | ||
let r = [], r2 = []; | ||
// populate opts with default values, if they were not explicitely set | ||
if (opts === null || (typeof opts !== 'object')) { | ||
opts = defaults; | ||
} else { | ||
opts = Object.assign(defaults,opts); | ||
} | ||
// remove illegal chars | ||
const asciiify = (key) => { | ||
let pattern = /[\W]/gi; | ||
let replaceWith = ''; | ||
return key.replace(pattern,replaceWith); | ||
}; | ||
// glue together deep arrays with underscores. | ||
// convert seperator-like chars to underscores | ||
const normalize = (arrayOfKeys) => { | ||
return arrayOfKeys.join('_').toUpperCase().replace(/[\-\s]+/g,'_'); | ||
}; | ||
const escapeChars = (str) => { | ||
let pat = new RegExp(opts.quote_character,'gi'); | ||
return str.replace(pat,'\\$&'); | ||
}; | ||
const shellExpand = (str) => { | ||
// perform shell expansion | ||
// @note: we are only supporting expanding the tilde to $HOME for now | ||
// to support more obscure expansions: | ||
// https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html#Shell-Expansions | ||
let r = str.replace('~',process.env.HOME); | ||
return r; | ||
}; | ||
// filter out falsey keys | ||
keybits = keybits.filter(v => { if (v === 0) return true; return (v); }); | ||
switch (typeof v) { | ||
@@ -17,4 +64,4 @@ case 'number': | ||
r = { | ||
"key": keybits.join('_').toUpperCase().replace(/\-/g,'_'), | ||
"value": '"' + v + '"' | ||
"key": asciiify(normalize(keybits)), | ||
"value": opts.quote_character + escapeChars(shellExpand(v)) + opts.quote_character | ||
} | ||
@@ -24,4 +71,4 @@ break; | ||
r = { | ||
"key": keybits.join('_').toUpperCase().replace(/\-/g,'_'), | ||
"value": '"' + v.replace('~',process.env.HOME) + '"' | ||
"key": asciiify(normalize(keybits)), | ||
"value": opts.quote_character + escapeChars(shellExpand(v)) + opts.quote_character | ||
} | ||
@@ -33,3 +80,3 @@ break; | ||
r = { | ||
"key": keybits.join('_').toUpperCase().replace(/\-/g,'_'), | ||
"key": asciiify(normalize(keybits)), | ||
"value": "" | ||
@@ -69,7 +116,24 @@ } | ||
module.exports = (jsonfile,prefix) => { | ||
let o = bashify([prefix],'jsonfile',jsonfile).map(kv => { | ||
return 'export ' + kv.key + '=' + kv.value; | ||
}); | ||
return o.join('\n'); | ||
module.exports = (jsonfile,prefix,opts) => { | ||
let data = bashify([prefix],'jsonfile',jsonfile,opts); | ||
return { | ||
export() { | ||
let o = data.map(kv => { | ||
return 'export ' + kv.key + '=' + kv.value; | ||
}); | ||
return o.join('\n'); | ||
}, | ||
declare() { | ||
let o = data.map(kv => { | ||
return kv.key + '=' + kv.value; | ||
}); | ||
return o.join('\n'); | ||
}, | ||
oneLine() { | ||
let o = data.map(kv => { | ||
return kv.key + '=' + kv.value; | ||
}); | ||
return o.join(' '); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "@code_monk/json2bash", | ||
"version": "1.0.7", | ||
"description": "Allows you to import json files (such as package.json) into bash scripts.", | ||
"version": "1.0.8", | ||
"description": "convert json files (such as package.json) into bash variables.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6649
121