Skip to content

Commit

Permalink
move code from spasdk/tasks repo and rework
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPark committed Jun 7, 2018
0 parents commit 8d1bc46
Show file tree
Hide file tree
Showing 8 changed files with 270 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
107 changes: 107 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/

'use strict';

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


function watch ( config, done ) {
var path = require('path'),
chokidar = require('chokidar'),
CLIEngine = require('eslint').CLIEngine,
failCount = {},
engine, watcher;

function handler ( name ) {
var report = engine.executeOnFiles([name]);

report.results.forEach(function ( result ) {
result.messages.forEach(function ( message ) {
log.fail(
'%s %s [%s:%s] %s',
log.colors.bold(path.relative('.', result.filePath)),
message.message,
message.line,
message.column,
log.colors.grey(message.ruleId)
);
});

// no more errors?
if ( result.messages.length === 0 && failCount[result.filePath] > 0 ) {
log.info('%s fixed', log.colors.bold(path.relative('.', result.filePath)));
}

// remember each check errors amount
failCount[result.filePath] = result.messages.length;
});
}

engine = new CLIEngine(config.options);

watcher = chokidar.watch(config.watch, config.watchOptions || runner.watch.config);
watcher
.on('change', handler)
.on('unlink', handler)
.on('add', handler);

return {
engine: engine,
watcher: watcher,
done: done
};
}


function unwatch ( instance ) {
if ( instance ) {
instance.watcher.close();
instance.done();
}
}


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

// sanitize and extend defaults
options = Object.assign(generator.options, options || {});

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

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

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

return tasks;
}


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


// export main actions
generator.methods = {
watch: watch,
unwatch: unwatch
};


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

[![build status](https://img.shields.io/travis/runner/generator-eslint.svg?style=flat-square)](https://travis-ci.org/runner/generator-eslint)
[![npm version](https://img.shields.io/npm/v/@runner/generator-eslint.svg?style=flat-square)](https://www.npmjs.com/package/@runner/generator-eslint)
[![dependencies status](https://img.shields.io/david/runner/generator-eslint.svg?style=flat-square)](https://david-dm.org/runner/generator-eslint)
[![devDependencies status](https://img.shields.io/david/dev/runner/generator-eslint.svg?style=flat-square)](https://david-dm.org/runner/generator-eslint?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-eslint)


## Installation ##

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


## Usage ##

Add to the scope:

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

Add generated tasks to the runner instance:

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

runner.tasks(
generator({
watch: ['src/js/**/*.js']
})
);
```

The following tasks will become available:

Task name | Description
----------------|-------------
eslint:config | prints the current configuration used for generated tasks
eslint:watch | starts file changes monitoring, prints warnings on errors
eslint:unwatch | stops monitoring

Generator configuration object:

Name | Description
--------------|-------------
watch | file, dir, glob, or array passed to `watch` in [chokidar](https://www.npmjs.com/package/chokidar#api)
watchOptions | optional watcher config object [parameters](https://www.npmjs.com/package/chokidar#api)
options | optional config object passed to [ESLint CLIEngine](https://eslint.org/docs/developer-guide/nodejs-api#cliengine)

Additional generator options:

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

So it's possible to change generated tasks names:

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

This will create the following tasks:

* lint:config:develop
* lint:watch:develop
* lint:unwatch:develop


## Contribution ##

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


## License ##

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

0 comments on commit 8d1bc46

Please sign in to comment.