Skip to content

Commit

Permalink
Converting over a lot of files to ES6 (#106)
Browse files Browse the repository at this point in the history
* Adding in forgotten --esw-version command to readme

* Updating simple-success

* Updating simple

* Updated successHelper

* Updating error-warning

* updating arg-parser

* Updating eslint-help.js

* Updating executor

* Updating logger

* Updating logger file from log to logger. derp

* Updating options.js

* Adding references badge

* Adding node requirement in readme
  • Loading branch information
rizowski authored Feb 14, 2017
1 parent 93c0e9d commit 29a9d6e
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 139 deletions.
4 changes: 3 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"plugins": ["add-module-exports"],
"presets": ["es2015-node4", "stage-3", "async-to-bluebird"]
"presets": [
["env", { "targets": { "node": 4 } }]
]
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Build Status](https://travis-ci.org/rizowski/eslint-watch.svg?branch=master)](https://travis-ci.org/rizowski/eslint-watch)
[![Code Climate](https://codeclimate.com/github/rizowski/eslint-watch/badges/gpa.svg)](https://codeclimate.com/github/rizowski/eslint-watch)
[![Test Coverage](https://codeclimate.com/github/rizowski/eslint-watch/badges/coverage.svg)](https://codeclimate.com/github/rizowski/eslint-watch/coverage)
[![Reference Status](https://www.versioneye.com/nodejs/eslint-watch/reference_badge.svg?style=flat-square)](https://www.versioneye.com/nodejs/eslint-watch/references)

Donate:
(Ƀitcoin): `13V7iDxBhnFASw7avGGRk64ExDGTirhx37`
Expand All @@ -16,6 +17,7 @@ Eslint Watch is a simple command line tool that wraps [Eslint](https://www.npmjs
## Requirements
To use this tool we require eslint to be installed on your project. The versions supported are:
- `"eslint": ">=0.19.0 <4.0.0"`
- `node >= 4.0.0`

## Getting started
To run eslint-watch without the global install, make an npm script.
Expand Down Expand Up @@ -48,6 +50,7 @@ Options:
-f, --format String Use a specific output format - default: simple-detail
-w, --watch Enable file watch
--changed Enables single file linting while watch is enabled
--esw-version Prints Eslint-Watch's Version
-c, --config path::String Use configuration from this file or shareable config
--no-eslintrc Disable use of configuration from .eslintrc
--env [String] Specify environments
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
"babel-core": "^6.21.0",
"babel-eslint": "^6.1.2",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-async-to-bluebird": "^1.1.0",
"babel-preset-es2015-node4": "^2.1.1",
"babel-preset-stage-3": "^6.17.0",
"babel-preset-env": "^1.1.8",
"chai": "^3.5.0",
"eslint": "3",
"istanbul": "^0.4.5",
Expand Down
27 changes: 15 additions & 12 deletions src/arg-parser.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
let path = require('path');
let _ = require('lodash');
let logger = require('./log')('arg-parser');
import path from 'path';
import _ from 'lodash';

import Logger from './logger';

const logger = Logger('arg-parser');
logger.debug('Loaded');

let simpleDetail = 'simple-detail';
let formatterPath = 'formatters';
const simpleDetail = 'simple-detail';
const formatterPath = 'formatters';

let defaultPath = './';
let formatKey = '-f';
let keys = {
const defaultPath = './';
const formatKey = '-f';
const keys = {
'-w': true,
'--watch': true,
'--changed': true,
'--esw-version': true
};
let formats = { // still don't like this can cause too much duplication
const formats = {
'simple': true,
'simple-success': true,
'simple-detail': true
};

let getPath = function(options){
const getPath = function getPath(options){
logger.debug('GetPath: %s', options.format);
return path.join(__dirname, formatterPath, options.format);
};

module.exports = {
parse: function (cliArgs, options) {
export default {
parse: function argParser(cliArgs, options) {
let arr = [];
let dirs = options._;
let formatSpecified = false;
Expand Down
2 changes: 1 addition & 1 deletion src/eslint/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { spawnSync } from '../executor';
import Logger from '../log';
import Logger from '../logger';
import settings from '../settings';

const logger = Logger('eslint-cli');
Expand Down
46 changes: 24 additions & 22 deletions src/eslint/help.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
var eslint = require('./cli');
var _ = require('lodash');
var logger = require('../log')('eslint-help');
import _ from 'lodash';

import Logger from '../logger';
import eslint from './cli';

const logger = Logger('eslint-help');
logger.debug('Loaded');

var namedOption = /^--/;
const namedOption = /^--/;

function parseNo(option, str){
if(!str) return;

var cmd = str.replace('--', '');
let cmd = str.replace('--', '');
if(/no-/.test(cmd)){
logger.debug('Parsing no option', str);
cmd = cmd.replace('no-', '');
Expand All @@ -19,7 +22,7 @@ function parseNo(option, str){
}

function parseDouble(arr){
var description = _.without(arr.slice(2),'').join(' ');
let description = _.without(arr.slice(2),'').join(' ');
return {
option: arr[0].replace('--', ''),
type: 'Boolean',
Expand All @@ -33,26 +36,26 @@ function parseRegular(arr){
if(!arr[0]){
return;
}
var optionText = arr[0];
var type = arr[1] || 'Boolean';
var option = {};
let optionText = arr[0];
let type = arr[1] || 'Boolean';
let option = {};
option = parseNo(option, optionText);

option.type = type;

var helpText = _.without(arr, optionText, type, '');
let helpText = _.without(arr, optionText, type, '');

var description = helpText.join(' ');
let description = helpText.join(' ');
if(description){
option.description = description;
}
return option;
}

function parseAlias(arr){
var alias = arr[0];
let alias = arr[0];
logger.debug('Alias found: %s', alias);
var option = parseRegular(_.without(arr, alias));
let option = parseRegular(_.without(arr, alias));

if(alias){
option.alias = alias.replace('-','');
Expand All @@ -61,7 +64,7 @@ function parseAlias(arr){
}

function createOption(arr){
var option;
let option;

if(namedOption.test(arr[0]) && namedOption.test(arr[1])){ // no alias defaulted boolean
option = parseDouble(arr);
Expand All @@ -70,22 +73,22 @@ function createOption(arr){
} else {// aliased or other
option = parseAlias(arr);
}
var isEmpty = _.isEmpty(option);
let isEmpty = _.isEmpty(option);
return isEmpty ? undefined : option;
}

function parseHelp(helpText){
var helpArr = helpText.split('\n');
var newArr = [];
var previousLine = [];
let helpArr = helpText.split('\n');
let newArr = [];
let previousLine = [];
_.each(helpArr, function(row, index){
if(index <= 2){
return;
} else {
var str = row.replace(',', '');
var arr = str.trim().split(' ');
let str = row.replace(',', '');
let arr = str.trim().split(' ');
if(str.indexOf('-') >= 0 && previousLine[0] !== ''){
var option = createOption(arr);
let option = createOption(arr);
if(option && option.option !== 'format' && option.option !== 'help'){
newArr.push(option);
}
Expand All @@ -96,7 +99,6 @@ function parseHelp(helpText){
return newArr;
}

// rewrite in es6 this callback yucky stuff goes away.
export default function eslintHelp(){
logger.debug('Executing help');
const result = eslint(['--help'], { stdio: [ process.stdin, null, process.stderr] });
Expand Down
50 changes: 5 additions & 45 deletions src/executor.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,13 @@
import { spawn, spawnSync } from 'child_process';
var _ = require('lodash');
var Logger = require('./log');
import { spawnSync } from 'child_process';

var logger = Logger('executor');
import Logger from './logger';
const logger = Logger('executor');

module.exports = {
spawn: function(cmd, args, options, callback){
options = _.merge({}, options, {
stdio: [process.stdin, 'pipe', 'pipe']
});
logger.debug('Running: %s', cmd);
logger.debug('args: %o', args);
var data = [];
var error = [];
var crashed = false;
var crashData = null;
var proc = spawn(cmd, args, options);

proc.on('error', function(e){
logger.debug('Process error:', e);
crashed = true;
crashData = e;
});

proc.on('exit', function(code){
logger.debug('External process exited:', code);
callback({
exitCode: code,
fatal: crashed,
cmd: cmd,
args: args,
output: data.join('').trim() || error.join('').trim() || crashData
});
});

proc.stdout.on('data', function(line){
logger.debug('Expected data: %s', line);
data.push(line);
});

proc.stderr.on('data', function(line){
logger.debug('expected error: %s', line);
error.push(line);
});
},
export default {
// https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options
spawnSync: (cmd, args, childOptions) => {
logger.debug(cmd, args);
let child = spawnSync(cmd, args, childOptions);
const child = spawnSync(cmd, args, childOptions);
if(child.error){
logger.debug('Critical error occurred.');
throw new Error(child.stderr.toString());
Expand Down
17 changes: 5 additions & 12 deletions src/formatters/helpers/error-warning.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
var chalk = require('chalk');
var space = ' ';
var divider = '/';
import { red, yellow, white } from 'chalk';

module.exports = function(result){
if(result.errorCount || result.warningCount){
return chalk.red(result.errorCount) + divider +
chalk.yellow(result.warningCount) + space +
chalk.white(result.filePath);
} else {
return chalk.red(result.messages.length) + space +
chalk.white(result.filePath);
}
export default function errorWarning(result){
return result.errorCount || result.warningCount ?
`${red(result.errorCount)}/${yellow(result.warningCount)} ${white(result.filePath)}` :
`${red(result.messages.length)} ${white(result.filePath)}`;
};
15 changes: 8 additions & 7 deletions src/formatters/helpers/success.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var chalk = require('chalk');
var c = require('./characters');
var space = ' ';
var Logger = require('../../log');
var logger = Logger('success-formatter');
import chalk from 'chalk';

import c from './characters';
import Logger from '../../logger';

const logger = Logger('success-formatter');
logger.debug('loaded');

module.exports = function(result){
export default function successHelper(result){
logger.debug(result);
return chalk.green(c.check) + space + chalk.white(result.filePath);
return `${chalk.green(c.check)} ${chalk.white(result.filePath)}`;
};
2 changes: 1 addition & 1 deletion src/formatters/simple-detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chalk from 'chalk';
import table from 'text-table';

import c from './helpers/characters';
import Logger from '../log';
import Logger from '../logger';

const logger = Logger('simple-detail');

Expand Down
23 changes: 11 additions & 12 deletions src/formatters/simple-success.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var success = require('./helpers/success');
var error = require('./helpers/error-warning');
var c = require('./helpers/characters');
import _ from 'lodash';

module.exports = function (results) {
var message = '';
for (var i = 0; i < results.length; i++) {
var result = results[i];
import success from './helpers/success';
import error from './helpers/error-warning';
import c from './helpers/characters';

export default function simpleSuccess(results) {
return _.reduce(results, (message, result) =>{
if (result.errorCount === 0 && result.warningCount === 0) {
message += success(result) + c.endLine;
message += `${success(result)}${c.endLine}`;
} else {
message += error(result);
message += c.endLine;
message += `${error(result)}${c.endLine}`;
}
}
return message;
return message;
}, '');
};
19 changes: 9 additions & 10 deletions src/formatters/simple.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var error = require('./helpers/error-warning');
var c = require('./helpers/characters');
import _ from 'lodash';

module.exports = function (results) {
var message = '';
for (var i = 0; i < results.length; i++) {
var result = results[i];
import error from './helpers/error-warning';
import c from './helpers/characters';

export default function simple(results) {
return _.reduce(results, (str, result) =>{
if (result.errorCount !== 0 || result.warningCount !== 0) {
message += error(result);
message += c.endLine;
str += `${error(result)}${c.endLine}`;
}
}
return message;
return str;
}, '');
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import eslintCli from './eslint/cli';
import helpOptions from './options';
import watcher from './watcher';
import argParser from './arg-parser';
import Logger from './log';
import Logger from './logger';
import pkg from '../package';

const logger = Logger('esw-cli');
Expand Down
4 changes: 2 additions & 2 deletions src/log.js → src/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var debug = require('debug');
import debug from 'debug';

module.exports = function(thing){
export default function createLogger(thing){
return {
log: console.log,
error: console.error,
Expand Down
Loading

0 comments on commit 29a9d6e

Please sign in to comment.