Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "repl-story", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Load history into node repl", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -23,14 +23,20 @@ # `repl-story` | ||
`replHistory` supports differents signatures: | ||
```js | ||
replHistory(filename|options, [options]); // -> REPLServer | ||
replHistory(filename, [repl|replServer]); // original signature similar to repl.history | ||
replHistory(options); | ||
replHistory(filename, options); | ||
// -> REPLServer | ||
``` | ||
`replHistory` takes up to two params: | ||
- `filename` or `historyFile`: the path toward the history file. `filename` is mandatory. | ||
Here are the different possible params: | ||
- **`filename` or `historyFile`**: the path toward the history file. `filename` is mandatory. | ||
However you can skip it and provide it via the `options` argument as the `filename` property (you can also use the alias `historyFile`). | ||
- **`replServer` or `repl`**: either the `repl` module, or a `ReplServer`. *[default the `require('repl')` builtin library]* | ||
- a config `options` object, this one is optional, and can replace `filename` param if it contains the filename value. | ||
This is a plain old js object that support the following facultative properties <!-- maybe, mention again filename in the list--> | ||
- `replServer` or `repl`: either the `repl` module, or a `ReplServer`. *[default the `require('repl')` builtin library]* | ||
- an **`options` object**, this give you better control over `repl` and `repl-story` configuration. | ||
This is a plain old js object that support the following properties | ||
- **`filename` or `historyFile`**: if not provided as leading argument, you can embed the file configuration into the option object. | ||
- **`replServer` or `repl`**: either the `repl` module, or a `ReplServer`. *[default the `require('repl')` builtin library]* | ||
- `create` or `noCreate`: whether history file should be created if missing *[default: `create=true`]* | ||
@@ -37,0 +43,0 @@ - `record` or `noRecord`: whether new history should be recorded *[default: `record=true`]* |
@@ -24,3 +24,3 @@ const REPL = require('repl'); | ||
}; | ||
const loadHistoryIntoReplServer = (replServer, filename, options) => { | ||
const loadHistoryIntoReplServer = (replServer, filename) => { | ||
// MAYBE: filter on load? (_.uniq?), and apply ignore filter? | ||
@@ -55,8 +55,21 @@ const history = fs | ||
const replHistory = (...args) => { | ||
const isReplLike = repl => | ||
repl === REPL || repl instanceof REPL.REPLServer || typeof repl.start === 'function'; | ||
// MAYBE: should consider lodash :smirk: | ||
const isPlainObject = value => | ||
value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype; | ||
const resolveOptions = args => { | ||
if (args.length === 0) | ||
throw new Error('Missing options. Provide either an historyFile path or a config object'); | ||
const [standaloneFilename, options = {}] = | ||
args.length === 1 ? (typeof args[0] === 'string' ? [args[0]] : [null, args[0]]) : args; | ||
if (args.length === 1) return isPlainObject(args[0]) ? [undefined, args[0]] : [args[0], {}]; | ||
return isReplLike(args[1]) ? [args[0], {repl: args[1]}] : args; | ||
}; | ||
const replHistory = (...args) => { | ||
const [standaloneFilename, options] = resolveOptions(args); | ||
const { | ||
@@ -75,5 +88,5 @@ replServer = REPL, | ||
if (!filename) throw new Error('You need to provide filename or historyFile'); | ||
if (typeof filename !== 'string') throw new Error('History filename needs to be a string'); | ||
if (repl !== REPL && !(repl instanceof REPL.REPLServer) && typeof repl.start !== 'function') | ||
throw new Error('Unexpected repl/replServer provided'); | ||
if (!isReplLike(repl)) throw new Error('Unexpected repl/replServer provided'); | ||
@@ -96,1 +109,2 @@ const resolvedFilename = filename.replace(/^~/, os.homedir); | ||
module.exports.loadHistoryIntoReplServer; | ||
module.exports._utils = {isPlainObject, isReplLike}; |
8835
86
74