# adrep

> Async Dictionary REPL (Adrep) lets you create a mini REPL with custom commands asynchronously.

Latest version **1.0.0** (published 2019-08-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install adrep
pnpm add adrep
yarn add adrep
bun add adrep
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-08-17 |
| First published | 2019-08-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | IceMetalPunk |
| Maintainers | icemetalpunk |
| Keywords | async, asynchronous, repl, custom, inquirer, prompt |

## Links

- npm: https://www.npmjs.com/package/adrep
- Repository: https://github.com/IceMetalPunk/Adrep
- Homepage: https://github.com/IceMetalPunk/Adrep#readme
- Issues: https://github.com/IceMetalPunk/Adrep/issues
- npm.io page: https://npm.io/package/adrep

## Dependencies (1)

- [inquirer](https://npm.io/package/inquirer.md) ^6.5.1

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-08-17

## README

# Adrep
## Asynchronous Dictionary REPL

Adrep allows you to create a "mini-REPL" in your application, which will run custom commands that you specify. It's based on, and dependent upon, [Inquirer](https://www.npmjs.com/package/inquirer).

### Installation
`npm install adrep`

### Usage
Require Adrep in your application normally, then create an instance of it:
```javascript
const Adrep = require('adrep');
const repl = new Adrep();
```

You can create new commands simply by using the `setCommand` method. All commands should return promises; they should resolve on success and reject on failure:
```javascript
repl.setCommand('greet', (name, greeting) => {
    if (name === 'Roger') {
        return Promise.reject('Go home, Roger!');
    }

    console.log(`Well, ${greeting}, ${name}!`);
    
    return Promise.resolve(`Greeted ${name}`);
});

repl.setCommand('increment', n => Promise.resolve(n + 1));
```

[!] Note: every Adrep REPL has a built-in `exit` command that cannot be overwritten. This is to prevent infinite loops. It will call the REPL's `exit` method, but you can call that method as well at any time to stop the loop yourself.

When you're done adding all your commands, you can start your Adrep REPL by calling its `run` method. This method takes three optional parameters: the string to use when prompting the user for a command, a callback to run on every successful command, and a callback to run on every failed command.
```javascript
repl.run('What would you like me to do? ', results => {
    console.log('Success! ', results);
}, error => {
    console.error('Error! ', error);
});
```

Successful results will be an array, where the first entry is the value the command's promise resolved with, the second is the command itself, and the rest are any arguments passed to the command. For instance:
```javascript
What would you like me to do? greet Bob howdy
Well, howdy, Bob!
// Success callback gets ['Greeted Bob', 'greet', 'Bob', 'howdy']
```

A user may include spaces in a single argument by "wrapping it in quotes"; likewise, they may use literal quotes by \\"escaping them\\" wherever needed.

Errors passed to the error callback will be an object with three properties: `type`, `value`, and `toString`.

* The `toString` method simply exists to get a string representation of the cause of the error; it can be useful in console logs, and is automatically called during template literal interpolation.
* The `type` property can be one of three values:
  * `Adrep.ERROR_CODES.ERR_NO_COMMAND`: No command was entered at all.
  * `Adrep.ERROR_CODES.ERR_UNKNOWN_COMMAND`: A command was entered, but it is unknown. 
  * `Adrep.ERROR_CODES.ERR_COMMAND_FAILED`: A known command was entered, but it returned a rejected promise.
* The `value` property is the same as the result of `toString` for all types except `ERR_COMMAND_FAILED`; in that case, the value will be an array, where the first entry is the value the command's promise rejected with, the second is the command, and the rest are any arguments supplied to the command.

For instance:
```javascript
What would you like me to do?
/* Error object is:
{
    type: Adrep.ERROR_CODES.ERR_NO_COMMAND,
    toString: () => ...,
    value: 'No command specified'
}
*/
What would you like me to do? absolutely nothing at all
/* Error object is:
{
    type: Adrep.ERROR_CODES.ERR_UNKNOWN_COMMAND,
    toString: (command) => ...,
    value: 'Unknown command absolutely'
}
*/
What would you like me to do? greet Roger "hi, neighbors!"
/* Error object is:
{
    type: Adrep.ERROR_CODES.ERR_COMMAND_FAILED,
    toString: (command, reason) => ..., // Ultimately: 'Command greet failed with reason: Go home, Roger!'
    value: ['Go home, Roger!', 'greet', 'Roger', 'hi, neighbors!']
}
*/
```

Final note: if you supply a success handler, it will be called for every successful command, even `exit`; you'll probably want to check the results array to make sure you don't try to handle commands that you don't want to.

---
_Source: https://npm.io/package/adrep · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
