Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "repl-story", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Load history into node repl", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -14,7 +14,5 @@ # `repl-story` | ||
```js | ||
const repl = require('repl'); | ||
const replHistory = require('repl-story'); | ||
const filename = '~/.my.cli' | ||
const replServer = replHistory({repl, filename}); | ||
const replServer = replHistory('~/.my.wonderful.cli'); | ||
// play around in repl | ||
@@ -26,19 +24,46 @@ // you can consult history using command .history | ||
`replHistory(options);` | ||
The two mantary field of options are: | ||
- `replServer` or `repl`: either the `repl` module, or a `ReplServer` | ||
- `filename` or `historyFile`: path toward the history file | ||
```js | ||
replHistory(filename|options, [options]); // -> REPLServer | ||
``` | ||
Here is the other facultative options that can be provided to `replHistory`: | ||
- `prompt`: optional prompt to use if did not provided a `ReplServer` instance | ||
- `create` or `noCreate`: whether history file should be created if missing *[default: `create=true`]* | ||
- `record` or `noRecord`: whether new history should be recorded *[default: `record=true`]* | ||
- `ignore`: an array of values that should not be recorded into history | ||
`replHistory` takes up to two 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`). | ||
`replHistory()` return the [`ReplServer`](https://nodejs.org/api/repl.html#repl_class_replserver) instance you provided, or otherwise the one it started. | ||
- 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]* | ||
- `create` or `noCreate`: whether history file should be created if missing *[default: `create=true`]* | ||
- `record` or `noRecord`: whether new history should be recorded *[default: `record=true`]* | ||
- `ignore`: an array of values that should not be recorded into history | ||
- any other options supported by `repl.start()` if you did not provided a `ReplServer` instance, such as: | ||
- `prompt`: optional prompt to use *[default `'> '`]* | ||
- `input`: Readable stream to read from *[default `'process.stdin`]* | ||
- `output`: Readable stream to write to *[default `process.stdout`]* | ||
- any other [option `repl.start()` supports](https://nodejs.org/api/repl.html#repl_repl_start_options) like `eval`, `writer`, `completer`, `useColors`; `terminal` `replMode` | ||
`replHistory()` return the [`REPLServer`](https://nodejs.org/api/repl.html#repl_class_replserver) instance you provided, or otherwise the one it started. | ||
### 'Complex' Example | ||
Here is an example to illustrate how to configure `repl-story`: | ||
```js | ||
const repl = require('repl'); | ||
const replHistory = require('repl-story'); | ||
const replServer = replHistory({ | ||
repl, | ||
filename: '~/.my.cli', | ||
record: false, // load history but do no record it. (equivalent to 'noRecord: true') | ||
noCreate: true, // disable creation if missing. (equivalent to 'create: false') | ||
prompt: ':> ' // options are forwarded to repl.start() if no provided replServer | ||
}); | ||
``` | ||
## Acknowledgment | ||
This is the adaptation of [repl.history](https://github.com/tmpvar/repl.history) to current node Apis. | ||
This started as the adaptation of [repl.history](https://github.com/tmpvar/repl.history) to current node Apis. | ||
And why story? `repl-history` was already taken :wink: | ||
*And why story?* `repl-history` was already taken :wink: |
@@ -1,2 +0,2 @@ | ||
const {REPLServer} = require('repl'); | ||
const REPL = require('repl'); | ||
const fs = require('fs'); | ||
@@ -7,17 +7,17 @@ const os = require('os'); | ||
const setUpHistory = (replServer, filename, options = {}) => { | ||
const setUpHistory = (replServer, filename, options) => { | ||
loadHistoryIntoReplServer(replServer, filename, options); | ||
if (!options.noRecord) setUpHistoryRecording(replServer, filename, options); | ||
if (options.record) setUpHistoryRecording(replServer, filename, options); | ||
replServer.commands.history = { | ||
replServer.defineCommand('history', { | ||
help: 'Show the history', | ||
action() { | ||
replServer.history.map(historyItem => { | ||
replServer.outputStream.write(historyItem); | ||
replServer.outputStream.write('\n'); | ||
this.history.map(historyItem => { | ||
this.outputStream.write(historyItem); | ||
this.outputStream.write('\n'); | ||
}); | ||
replServer.displayPrompt(); | ||
this.displayPrompt(); | ||
} | ||
}; | ||
}); | ||
return replServer; | ||
@@ -52,14 +52,16 @@ }; | ||
}); | ||
process.on('exit', function() { | ||
fs.closeSync(descriptor); | ||
}); | ||
replServer.on('exit', () => fs.closeSync(descriptor)); | ||
}; | ||
const replHistory = options => { | ||
const replHistory = (...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; | ||
const { | ||
replServer, | ||
replServer = REPL, | ||
repl = replServer, | ||
historyFile, | ||
historyFile = standaloneFilename, | ||
filename = historyFile, | ||
prompt, | ||
noCreate = false, | ||
@@ -70,10 +72,17 @@ create = !noCreate, | ||
ignore | ||
} = options || {}; | ||
if (!repl) throw new Error('You need to provide repl or replServer'); | ||
} = options; | ||
if (!filename) throw new Error('You need to provide filename or historyFile'); | ||
if (repl !== REPL && !(repl instanceof REPL.REPLServer) && typeof repl.start !== 'function') | ||
throw new Error('Unexpected repl/replServer provided'); | ||
const resolvedFilename = filename.replace(/^~/, os.homedir); | ||
if (create && !fs.existsSync(resolvedFilename)) fs.writeFileSync(resolvedFilename, ''); | ||
if (!fs.existsSync(resolvedFilename)) { | ||
if (!create) throw new Error(`Provided filename does not exists and create is disabled`); | ||
fs.writeFileSync(resolvedFilename, ''); | ||
} | ||
const replInstance = repl instanceof REPLServer ? repl : repl.start(prompt); | ||
// Note, passing options enable to forward all options to repl.start | ||
const replInstance = repl instanceof REPL.REPLServer ? repl : repl.start(options); | ||
@@ -80,0 +89,0 @@ return setUpHistory(replInstance, resolvedFilename, {record, ignore}); |
8017
77
68