Skip to content

Commit

Permalink
move code from node-runner repo, split and rework
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPark committed Jun 12, 2018
0 parents commit 1c5ace5
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/

'use strict';

// public
module.exports = {
// base rules
extends: require.resolve('@cjssdk/eslint-config/.eslintrc.js')
};
13 changes: 13 additions & 0 deletions .github/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Contributing
============

## Issues ##

Try to be the most specific possible. Give your OS, Node.js, application, database and packages info.
Give an example that will help other people to replicate your problem.


## Pull Requests ##

They are always welcomed but with respect to the adopted [code style](https://github.com/DarkPark/jscs) and included [ESLint](http://eslint.org/) rules.
Your patches must pass all the tests. If adding a new feature, don't forget about adding it to documentation (readme.md) and add tests for it.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.eslintrc.js
.github
.idea
.travis.yml
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: node_js
node_js:
- "10"
- "8"
- "6"
cache:
directories:
- node_modules
106 changes: 106 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/

'use strict';

var name = 'repl',
log = require('@runner/logger').wrap(name);


function start ( config, done ) {
var readline = require('readline'),
instance = readline.createInterface(config.readline);

//readline.setPrompt(config.readline.prompt);

instance.on('line', function ( line ) {
if ( line ) {
// silent mode for future
//readline.moveCursor(process.stdout, 0, -1);
//readline.clearLine();

config.runner.run(line);
}
});

instance.on('close', function () {
done();
});

// ctrl+c
instance.on('SIGINT', function onSIGINT () {
/* eslint-disable no-process-exit */
process.exit(0);
});

return instance;
}


function stop ( instance ) {
if ( instance ) {
instance.close();
}
}


function generator ( config, options ) {
var tasks = {},
instance;

// sanitize and extend defaults
config = Object.assign({
readline: {
input: process.stdin,
output: process.stdout,
prompt: '',
historySize: 100,
removeHistoryDuplicates: true,
completer: function ( line ) {
var tasks = Object.keys(config.runner.tasks).sort(),
hits = tasks.filter(function ( task ) {
return task.startsWith(line);
});

// show all completions if none found
return [hits.length ? hits : tasks, line];
}
}
}, config || {});
options = Object.assign(generator.options, options || {});

tasks[options.prefix + 'config' + options.suffix] = function () {
log.inspect(config, log);
};

tasks[options.prefix + 'start' + options.suffix] = function ( done ) {
instance = start(config, done);
};

tasks[options.prefix + 'stop' + options.suffix] = function () {
stop(instance);
instance = null;
};

return tasks;
}


// defaults
generator.options = {
prefix: name + ':',
suffix: ''
};


// export main actions
generator.methods = {
start: start,
stop: stop
};


// public
module.exports = generator;
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@runner/generator-repl",
"version": "1.0.0",
"description": "Tasks generator for REPL.",
"author": {
"name": "Stanislav Kalashnik",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/runner/generator-repl"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"lint": "eslint .",
"test": "npm run lint"
},
"dependencies": {
"@runner/logger": "^1.0.0"
},
"devDependencies": {
"@cjssdk/eslint-config": "^1.1.2",
"eslint": "^4.19.1"
},
"keywords": [
"task",
"runner",
"generator",
"repl"
],
"license": "GPL-3.0"
}
102 changes: 102 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Tasks generator for REPL
========================

[![build status](https://img.shields.io/travis/runner/generator-repl.svg?style=flat-square)](https://travis-ci.org/runner/generator-repl)
[![npm version](https://img.shields.io/npm/v/@runner/generator-repl.svg?style=flat-square)](https://www.npmjs.com/package/@runner/generator-repl)
[![dependencies status](https://img.shields.io/david/runner/generator-repl.svg?style=flat-square)](https://david-dm.org/runner/generator-repl)
[![devDependencies status](https://img.shields.io/david/dev/runner/generator-repl.svg?style=flat-square)](https://david-dm.org/runner/generator-repl?type=dev)
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-blue.svg?style=flat-square)](https://gitter.im/DarkPark/runner)
[![RunKit](https://img.shields.io/badge/RunKit-try-yellow.svg?style=flat-square)](https://npm.runkit.com/@runner/generator-repl)


## Installation ##

```bash
npm install @runner/generator-repl
```


## Usage ##

Add to the scope:

```js
var generator = require('@runner/generator-repl');
```

Generate tasks according to the given config:

```js
var tasks = generator({
runner: runnerInstance,
readline: {
historySize: 200
}
});
```

Add generated tasks to the `runner` instance:

```js
var runner = require('@runner/core');

Object.assign(runner.tasks, tasks);
```

The following tasks will become available:

Task name | Description
---------------|-------------
`repl:config` | prints the current configuration used for generated tasks
`repl:start` | starts repl server
`repl:stop` | stops repl server

Generator accepts two arguments: base configuration and additional options.


### Base configuration ###

It's an object with the following properties:

Name | Description
----------|-------------
runner | existing runner instance
readline | [options](https://nodejs.org/api/readline.html#readline_readline_createinterface_options) passed to the `readline.createInterface` method


### Additional options ###

It's an object with the following properties:

Name | Description
--------|-------------
prefix | an affix placed before a task name (default is `repl:`)
suffix | a string added at the end of a task name (empty by default)

So it's possible to change generated tasks names:

```js
Object.assign(runner.tasks,
generator(config, {
prefix: 'readline:',
suffix: ':develop'
})
);
```

It will add the following tasks:

* `readline:config:develop`
* `readline:start:develop`
* `readline:stop:develop`


## Contribution ##

If you have any problems or suggestions please open an [issue](https://github.com/runner/generator-repl/issues)
according to the contribution [rules](.github/contributing.md).


## License ##

`@runner/generator-repl` is released under the [GPL-3.0 License](http://opensource.org/licenses/GPL-3.0).

0 comments on commit 1c5ace5

Please sign in to comment.