@danmasta/interpolate
Advanced tools
Comparing version 0.0.7 to 0.0.8
#!/usr/bin/env node | ||
const _ = require('lodash'); | ||
const EnvStr = require('../lib/envstr'); | ||
const Envstr = require('../lib/envstr'); | ||
const util = require('../lib/util'); | ||
@@ -17,2 +16,4 @@ | ||
--newline -n - Which character to use as newline delimeter. Default is '\\n' | ||
--include -i - Which keys to include in output: key1,key2 | ||
--exclude -e - Which keys to exclude from output: key3,key4 | ||
--help -h - Show this help message | ||
@@ -33,6 +34,8 @@ | ||
newline: 'n', | ||
include: 'i', | ||
exclude: 'e', | ||
help: 'h' | ||
}); | ||
let envstr = new EnvStr(opts); | ||
let envstr = new Envstr(opts); | ||
@@ -46,2 +49,3 @@ if (opts.help) { | ||
if (opts.string) { | ||
if (opts.json) { | ||
@@ -52,3 +56,5 @@ process.stdout.write(envstr.parseJsonStr(opts.string)); | ||
} | ||
} else if (opts.stdin) { | ||
util.getStdin().then(str => { | ||
@@ -61,2 +67,3 @@ if (opts.json) { | ||
}); | ||
} else { | ||
@@ -63,0 +70,0 @@ process.stdout.write(help); |
#!/usr/bin/env node | ||
const _ = require('lodash'); | ||
const Interpolator = require('../lib/interpolator'); | ||
const Interpolate = require('../lib/interpolate'); | ||
const util = require('../lib/util'); | ||
@@ -46,3 +45,3 @@ | ||
let interpolator = new Interpolator(opts); | ||
let interpolate = new Interpolate(opts); | ||
@@ -56,9 +55,15 @@ if (opts.help) { | ||
if (opts.stdin) { | ||
util.getStdin().then(str => { | ||
process.stdout.write(interpolator.parseStr(str)); | ||
process.stdout.write(interpolate.parseStr(str)); | ||
}); | ||
} else if (opts.string) { | ||
process.stdout.write(interpolator.parseStr(opts.string)); | ||
process.stdout.write(interpolate.parseStr(opts.string)); | ||
} else if (opts.input && opts.output) { | ||
interpolator.parseFile(opts.input, opts.output, opts.src); | ||
interpolate.parseFile(opts.input, opts.output, opts.src); | ||
} else { | ||
@@ -65,0 +70,0 @@ process.stdout.write(help); |
19
index.js
@@ -1,15 +0,6 @@ | ||
const Interpolator = require('./lib/interpolator'); | ||
const Interpolate = require('./lib/interpolate'); | ||
function parse (str, opts) { | ||
let interpolator = new Interpolator(opts); | ||
return interpolator.parseStr(str); | ||
} | ||
function file (opts) { | ||
let interpolator = new Interpolator(opts); | ||
return interpolator.parseFile(opts.input, opts.output, opts.src); | ||
} | ||
exports.Interpolator = Interpolator; | ||
exports.parse = parse; | ||
exports.file = file; | ||
exports = module.exports = Interpolate.parseStrFactory(); | ||
exports.parse = Interpolate.parseStrFactory(); | ||
exports.file = Interpolate.parseFileFactory(); | ||
exports.Interpolate = Interpolate; |
@@ -6,10 +6,24 @@ const _ = require('lodash'); | ||
json: false, | ||
key: null, | ||
newline: '\n' | ||
key: undefined, | ||
newline: '\n', | ||
exclude: undefined, | ||
include: undefined | ||
}; | ||
class EnvStr { | ||
class Envstr { | ||
constructor (opts) { | ||
this.opts = opts = _.defaults(opts, defaults); | ||
opts = _.defaults(opts, defaults); | ||
if (_.isString(opts.exclude)) { | ||
opts.exclude = opts.exclude.split(',').map(str => str.trim()); | ||
} | ||
if (_.isString(opts.include)) { | ||
opts.include = opts.include.split(',').map(str => str.trim()); | ||
} | ||
this.opts = opts; | ||
} | ||
@@ -19,2 +33,14 @@ | ||
if (this.opts.include) { | ||
pairs = _.filter(pairs, pair => { | ||
return this.opts.include.indexOf(pair[0]) > -1; | ||
}); | ||
} | ||
if (this.opts.exclude) { | ||
pairs = _.filter(pairs, pair => { | ||
return this.opts.exclude.indexOf(pair[0]) < 0; | ||
}); | ||
} | ||
return _.join(_.map(pairs, pair => { | ||
@@ -54,3 +80,3 @@ return this.opts.quotes ? `${pair[0]}="${pair[1]}"` : `${pair[0]}=${pair[1]}`; | ||
} catch (err) { | ||
throw new Error('EnvStr json str is not valid json: ' + err.message); | ||
throw new Error('Envstr json str is not valid json: ' + err.message); | ||
} | ||
@@ -64,2 +90,2 @@ | ||
module.exports = EnvStr; | ||
module.exports = Envstr; |
@@ -23,2 +23,3 @@ const minimist = require('minimist'); | ||
let res = {}; | ||
let argv = _.transform(minimist(process.argv.slice(2)), (res, val, key) => { | ||
@@ -42,2 +43,6 @@ res[_.camelCase(key)] = toNativeType(val); | ||
process.stdin.on('data', chunk => { | ||
res += chunk; | ||
}); | ||
process.stdin.on('end', () => { | ||
@@ -47,6 +52,2 @@ resolve(res); | ||
process.stdin.on('data', chunk => { | ||
res += chunk; | ||
}); | ||
}); | ||
@@ -53,0 +54,0 @@ |
{ | ||
"name": "@danmasta/interpolate", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"author": "Daniel Smith <dannmasta@gmail.com>", | ||
"description": "Simple string interpolation helper", | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -23,4 +24,4 @@ "template", | ||
"scripts": { | ||
"test": "mocha tests", | ||
"coverage": "nyc mocha tests" | ||
"test": "./node_modules/.bin/mocha tests", | ||
"coverage": "./node_modules/.bin/nyc --reporter=lcov ./node_modules/.bin/mocha tests" | ||
}, | ||
@@ -27,0 +28,0 @@ "repository": { |
@@ -28,9 +28,9 @@ # Interpolate | ||
-----|-------|------|------------ | ||
`input` | i | *`string`* | Directory or file path to use when reading files | ||
`output` | o | *`string`* | Directory to write parsed files to | ||
`src` | | *`string`* | Glob pattern string to filter input file list, ex: `**/*.yml` | ||
`string` | s | *`string`* | Text string to parse | ||
`stdin` | | *`boolean`* | Read input from stdin | ||
`input` | i | *`string`* | Directory or file path to use when reading files. Default is `undefined` | ||
`output` | o | *`string`* | Directory to write parsed files to. Default is `undefined` | ||
`src` | | *`string`* | Glob pattern string to filter input file list, ex: `**/*.yml`. Default is `undefined` | ||
`string` | s | *`string`* | Text string to parse. Default is `undefined` | ||
`stdin` | | *`boolean`* | Read input from stdin. Default is `false` | ||
`env` | e | *`boolean`* | If true will also interpolate with environment variables. Default is `false` | ||
`params` | p | *`object\|string`* | Object of key,value pairs to use for parameter matching. If string, it should either be a stringified json object, or a comma-separated key,value list: `"key1=1,key2=2"`. Default is `null` | ||
`params` | p | *`object\|string`* | Object of key,value pairs to use for parameter matching. If string, it should either be a stringified json object, or a comma-separated key,value list: `"key1=1,key2=2"`. Default is `undefined` | ||
`warn` | w | *`boolean`* | If true will write a message to `stderr` when a parameter is not found. Default is `true` | ||
@@ -44,6 +44,21 @@ `throw` | t | *`boolean`* | If true will throw an error when a parameter is not found. Default is `false` | ||
-----|------------ | ||
`Interpolator(opts)` | Interpolator class for creating a custom parser instance | ||
`parse(str, opts)` | Parses a string with optional opts | ||
`file({ input, output, src })` | Parses a file or directory based in `opts.input`. Files are parsed then written to `opts.output` | ||
`file({ input, output, src })` | Parses a file or directory based on `opts.input`. Files are parsed then written to `opts.output` | ||
## Envstr | ||
This package includes an extra cli utility called `envstr`. It formats json or table style data into `key=val` pairs that can be easily consumed in bash scripts or exported as environment variables. | ||
### Options | ||
name | alias | type | description | ||
-----|-------|------|------------ | ||
`string` | s | *`string`* | Text string to parse. Default is `undefined` | ||
`stdin` | | *`boolean`* | Read input from stdin. Default is `undefined` | ||
`json` | j | *`boolean`* | Handle input as json. Default is `false` | ||
`key` | k | *`string`* | If input is json, parse data at specified key. Default is `undefined` | ||
`quotes` | q | *`boolean`* | If true add quotes around env values. Default is `false` | ||
`newline` | n | *`string`* | Which character to use as newline delimeter. Default is `'\n'` | ||
`include` | i | *`string`* | Which keys to include in output: `key1,key2` | ||
`exclude` | e | *`string`* | Which keys to exclude from output: `key3,key4` | ||
`help` | h | *`boolean`* | View the cli help menu | ||
## Examples | ||
@@ -57,5 +72,5 @@ Parse a text string | ||
let string = '{{SRC_DIR}} -> {{BUILD_DIR}}'; | ||
let str = '{{SRC_DIR}} -> {{BUILD_DIR}}'; | ||
console.log(interpolate.parse(str, { params })); | ||
console.log(interpolate(str, { params })); | ||
``` | ||
@@ -66,2 +81,6 @@ Parse a directory of files via cli | ||
``` | ||
Convert json data to env str format | ||
```bash | ||
envstr -s '{"KEY1":true,"KEY2":false}' --json --quotes | ||
``` | ||
@@ -68,0 +87,0 @@ ## Testing |
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
16891
336
88