Skip to content

Commit

Permalink
Merged feature/es5 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGot committed Dec 7, 2016
2 parents adf135a + 4269cce commit adfb10f
Show file tree
Hide file tree
Showing 52 changed files with 3,226 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["latest"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
samples/
test/mocks/
build/
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"globals": {
"module": true,
"inject": true,
"require": true
"require": true,
"process": true
}
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ test/
.travis.yml
yarn.lock
.git*
.babelrc
49 changes: 29 additions & 20 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,43 @@ You can launch linting with the following command:
npm run lint
```

## Building to ES5

Launch the `npm run build` command.

After writing your new reporter, you should build the library to keep the ES5 backward compatibility.

## Writing a reporter

1. First of all, create a file under `reporters/` for your new reporter. It should match the following syntax: **new-reporter.reporter.js**.
1. The new reporter must extends the `Reporter` class and implement `defaultOptions` and `launch` methods
1. Add a new reporter type in the `reporter.enum.js` file

```js
const ReporterType = {
NEWREPORTER: 'newreporter'
}
```
```js
const ReporterType = {
NEWREPORTER: 'newreporter'
}
```
1. Register the reporter in the `ReporterFactory`

```js
switch (type) {
case ReporterType.NEWREPORTER:
opts = ReporterFactory.mergeOptions(options, NewReporter.defaultOptions());
reporter = new NewReporter(opts, projectName);
break;
}
```
1. Export the reporter so that anyone can use it (`index.js`)

```js
module.exports = {
NewReporter: require('./reporters/new-reporter.reporter')
};
```
```js
switch (type) {
case ReporterType.NEWREPORTER:
opts = ReporterFactory.mergeOptions(options, NewReporter.defaultOptions());
reporter = new NewReporter(opts, projectName);
break;
}
```
1. Export the reporter so that anyone can use it (`lib/api.js`)

```js
module.exports = {
NewReporter: require('./reporters/new-reporter.reporter'),
ES5 : {
NewReporter: require('../build/reporters/new-reporter.reporter')
}
};
```
1. Create related tests in `/test/reporters` folder
1. Update README
* **Default options** section
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ You can also set it's property to `false`:
}
```

#### ES5 backward compatibility

To use the CLI with older NodeJS versions, you can use the `--es5` option:

```sh
$ sreporter --es5
```

### NodeJS

You can launch all reporters:
Expand Down Expand Up @@ -139,11 +147,20 @@ cssLintReporter.launch(() => {
});
```

#### ES5 backward compatibility

To use it with older NodeJS versions, you can require the reporters this way:

```js
const SonarWebReporters = require('sonar-web-frontend-reporters').ES5.Reporters;
const CSSLintReporter = require('sonar-web-frontend-reporters').ES5.CSSLintReporter;
```

## Available reporters

* CSSLint
* SASSLint
* SASSLint to SCSSLint (convert SASSLint rules to SCSSLint rules)
* SASSLint to SCSSLint (convert SASSLint rules to SCSSLint rules, because the SonarQube plugin doesn't support SASSLint rules)
* HTMLHint
* ESLint
* ESLint for AngularJS
Expand All @@ -161,7 +178,7 @@ The main reasons for this **3.0** version were that:

> Warning:
>
> 3.x version is compatible with NodeJS version **>= 6.5.0**
> 3.x version is compatible with NodeJS version **>= 6.5.0** with ES6 support and **>= 4.4.5** with ES5 backward compatibility
You can still use the Gulp version by installing `[email protected]`.

Expand Down
54 changes: 9 additions & 45 deletions bin/sreporter
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
#!/usr/bin/env node

/* eslint no-console:off */

const fs = require('fs'),
chalk = require('chalk'),
ReporterType = require('../reporters/reporter.enum'),
ReporterFactory = require('../reporters/reporter.factory');

class CLIEngine {

constructor () {
if (!fs.existsSync('./.sreporterrc')) {
throw new Error('.sreporterrc file is missing');
}

this.options = JSON.parse(fs.readFileSync('./.sreporterrc', 'utf8'));
}

/**
* Launch the Sonar Web Front-End Reporters.
* It automatically launch reporters registered under `ReporterType`.
*/
launchReporters () {
Object.keys(ReporterType).forEach((key) => this.launch(ReporterType[key], this.options));
}

/**
* Launch a reporter.
*
* @param {ReporterType} type Type of the reporter to be launched
* @param {Object} options `.sreporterrc` file content
*/
launch (type, options) {
let opts = options[type],
reporter = ReporterFactory.create(type, opts, options.projectName);

if (opts) {
reporter.launch(() => {
console.info(chalk.green(`\u2714 ${chalk.green.bold(reporter.linterName)} has been generated under ${opts.report}`));
});
} else {
console.info(chalk.gray(`\u002D ${chalk.gray.bold(reporter.linterName)} has been ignored`));
}
}

var es5 = (process.argv.indexOf('--es5') > -1),
CLIEngine;

// Use ES5 backward compatibility ?
if (es5) {
CLIEngine = require('../build/cli');
} else {
CLIEngine = require('../lib/cli');
}

let cli = new CLIEngine();
var cli = new CLIEngine();
cli.launchReporters();
24 changes: 24 additions & 0 deletions build/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = {
Reporters: require('./reporters'),
CSSLintReporter: require('./reporters/csslint.reporter'),
ESLintReporter: require('./reporters/eslint.reporter'),
ESLintAngularReporter: require('./reporters/eslint-angular.reporter'),
HTMLHintReporter: require('./reporters/htmlhint.reporter'),
JSHintReporter: require('./reporters/jshint.reporter'),
TSLintReporter: require('./reporters/tslint.reporter'),
SASSLintReporter: require('./reporters/sasslint.reporter'),
SASSToSCSSLintReporter: require('./reporters/sass-to-scsslint.reporter'),
ES5: {
Reporters: require('../build/reporters'),
CSSLintReporter: require('../build/reporters/csslint.reporter'),
ESLintReporter: require('../build/reporters/eslint.reporter'),
ESLintAngularReporter: require('../build/reporters/eslint-angular.reporter'),
HTMLHintReporter: require('../build/reporters/htmlhint.reporter'),
JSHintReporter: require('../build/reporters/jshint.reporter'),
TSLintReporter: require('../build/reporters/tslint.reporter'),
SASSLintReporter: require('../build/reporters/sasslint.reporter'),
SASSToSCSSLintReporter: require('../build/reporters/sass-to-scsslint.reporter')
}
};
67 changes: 67 additions & 0 deletions build/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/* eslint no-console:off */

var fs = require('fs'),
chalk = require('chalk'),
ReporterType = require('./reporter.enum'),
ReporterFactory = require('./reporter.factory');

var CLIEngine = function () {
function CLIEngine() {
_classCallCheck(this, CLIEngine);

if (!fs.existsSync('./.sreporterrc')) {
throw new Error('.sreporterrc file is missing');
}

this.options = JSON.parse(fs.readFileSync('./.sreporterrc', 'utf8'));
}

/**
* Launch the Sonar Web Front-End Reporters.
* It automatically launch reporters registered under `ReporterType`.
*/


_createClass(CLIEngine, [{
key: 'launchReporters',
value: function launchReporters() {
var _this = this;

Object.keys(ReporterType).forEach(function (key) {
return _this.launch(ReporterType[key], _this.options);
});
}

/**
* Launch a reporter.
*
* @param {ReporterType} type Type of the reporter to be launched
* @param {Object} options `.sreporterrc` file content
*/

}, {
key: 'launch',
value: function launch(type, options) {
var opts = options[type],
reporter = ReporterFactory.create(type, opts, options.projectName);

if (opts) {
reporter.launch(function () {
console.info(chalk.green('\u2714 ' + chalk.green.bold(reporter.linterName) + ' has been generated under ' + opts.report));
});
} else {
console.info(chalk.gray('- ' + chalk.gray.bold(reporter.linterName) + ' has been ignored'));
}
}
}]);

return CLIEngine;
}();

module.exports = CLIEngine;
14 changes: 14 additions & 0 deletions build/reporter.enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var ReporterType = {
CSSLINT: 'csslint',
ESLINT: 'eslint',
ESLINTANGULAR: 'eslintangular',
HTMLHINT: 'htmlhint',
JSHINT: 'jshint',
TSLINT: 'tslint',
SASSLINT: 'sasslint',
SASS_TO_SCSSLINT: 'sass-to-scsslint'
};

module.exports = ReporterType;
Loading

0 comments on commit adfb10f

Please sign in to comment.