From 2b34e6b1bd53c692df089ee76aaf49de04637e18 Mon Sep 17 00:00:00 2001 From: fewieden Date: Wed, 15 Feb 2017 23:46:21 +0100 Subject: [PATCH] add linter --- .codeclimate.yml | 26 +++++++ .editorconfig | 13 ++++ .eslintrc | 17 ++++ .mdlrc | 2 + .stylelintrc | 6 ++ .travis.yml | 11 +++ MMM-ScottishPremierLeague.css | 8 +- MMM-ScottishPremierLeague.js | 141 +++++++++++++++++----------------- README.md | 23 +++--- node_helper.js | 36 +++++---- package.json | 12 ++- 11 files changed, 197 insertions(+), 98 deletions(-) create mode 100644 .codeclimate.yml create mode 100644 .editorconfig create mode 100644 .eslintrc create mode 100644 .mdlrc create mode 100644 .stylelintrc create mode 100644 .travis.yml diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..3be21a6 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,26 @@ +engines: + stylelint: + enabled: true + duplication: + enabled: true + config: + languages: + - javascript + eslint: + enabled: true + channel: "eslint-3" + checks: + import/no-unresolved: + enabled: false + fixme: + enabled: true + markdownlint: + enabled: true +ratings: + paths: + - "**.css" + - "**.js" + - "**.md" +exclude_paths: [ + "node_modules/**/*" +] \ No newline at end of file diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..15d51b0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_style = space +indent_size = 4 + +[{*.json, *.yml}] +indent_style = space +indent_size = 2 diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..db007e4 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,17 @@ +{ + "extends": "airbnb-base", + "rules": { + "comma-dangle": 0, + "indent": [2, 4], + "max-len": [2, 120, { "ignoreStrings": true }], + "radix": [2, "as-needed"] + }, + "settings": { + "import/core-modules": [ "node_helper" ] + }, + "env": { + "browser": true, + "node": true, + "es6": true + } +} \ No newline at end of file diff --git a/.mdlrc b/.mdlrc new file mode 100644 index 0000000..e0ea98e --- /dev/null +++ b/.mdlrc @@ -0,0 +1,2 @@ +all +rules "~MD013", "~MD033" \ No newline at end of file diff --git a/.stylelintrc b/.stylelintrc new file mode 100644 index 0000000..423d85a --- /dev/null +++ b/.stylelintrc @@ -0,0 +1,6 @@ +{ + "extends": "stylelint-config-standard", + "rules": { + "indentation": 4 + } +} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b89f2aa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: node_js +node_js: + - "stable" + - "7" + - "6" + - "5" +script: + - npm run lint +cache: + directories: + - node_modules diff --git a/MMM-ScottishPremierLeague.css b/MMM-ScottishPremierLeague.css index 2311e46..4d58bd5 100644 --- a/MMM-ScottishPremierLeague.css +++ b/MMM-ScottishPremierLeague.css @@ -1,11 +1,11 @@ .MMM-ScottishPremierLeague .name { - text-align: left; + text-align: left; } .MMM-ScottishPremierLeague .table { - border-spacing: 2px; + border-spacing: 2px; } .MMM-ScottishPremierLeague .centered { - text-align: center; -} \ No newline at end of file + text-align: center; +} diff --git a/MMM-ScottishPremierLeague.js b/MMM-ScottishPremierLeague.js index 9cdf65f..89974ee 100644 --- a/MMM-ScottishPremierLeague.js +++ b/MMM-ScottishPremierLeague.js @@ -1,3 +1,5 @@ +/* global Module Log */ + /* Magic Mirror * Module: MMM-ScottishPremierLeague * @@ -6,130 +8,131 @@ * MIT Licensed. */ -Module.register("MMM-ScottishPremierLeague",{ +Module.register('MMM-ScottishPremierLeague', { defaults: { api_key: false, focus_on: false, max_teams: false, - updateInterval: 180*60*1000, + updateInterval: 180 * 60 * 1000, season: '1617' }, loading: true, - start: function() { - Log.info("Starting module: " + this.name); + start() { + Log.info(`Starting module: ${this.name}`); this.sendSocketNotification('CONFIG', this.config); }, - socketNotificationReceived: function(notification, payload){ - if(notification === 'DATA'){ + socketNotificationReceived(notification, payload) { + if (notification === 'DATA') { this.standing = payload; this.loading = (!this.standing); this.updateDom(300); } }, - getStyles: function() { - return ["font-awesome.css", "MMM-ScottishPremierLeague.css"]; + getStyles() { + return ['font-awesome.css', 'MMM-ScottishPremierLeague.css']; }, - getTranslations: function() { + getTranslations() { return { - en: "translations/en.json", - de: "translations/de.json" + en: 'translations/en.json', + de: 'translations/de.json' }; }, - getDom: function() { - var wrapper = document.createElement("div"); + getDom() { + const wrapper = document.createElement('div'); if (this.loading || !this.standing) { - var title = document.createElement("header"); + const title = document.createElement('header'); title.innerHTML = this.name; wrapper.appendChild(title); - var subtitle = document.createElement("div"); - subtitle.classList.add("small", "dimmed", "light"); - subtitle.innerHTML = (this.loading) ? this.translate("LOADING") : this.translate("NO_DATA_AVAILABLE"); + const subtitle = document.createElement('div'); + subtitle.classList.add('small', 'dimmed', 'light'); + subtitle.innerHTML = (this.loading) ? this.translate('LOADING') : this.translate('NO_DATA_AVAILABLE'); wrapper.appendChild(subtitle); return wrapper; } - if(this.standing){ + if (this.standing) { // League header - var title = document.createElement("header"); - title.innerHTML = "Scottish Premier League"; + const title = document.createElement('header'); + title.innerHTML = 'Scottish Premier League'; wrapper.appendChild(title); // Standings container - var table = document.createElement('table'); + const table = document.createElement('table'); table.classList.add('xsmall', 'table'); // Standings header row - var row = document.createElement('tr'); - row.classList.add('row'); + const headerRow = document.createElement('tr'); + headerRow.classList.add('row'); - var position = document.createElement('th'); - row.appendChild(position); + const position = document.createElement('th'); + headerRow.appendChild(position); - var name = document.createElement('th'); - name.classList.add('name'); - name.innerHTML = this.translate("TEAM"); - row.appendChild(name); + const nameLabel = document.createElement('th'); + nameLabel.classList.add('name'); + nameLabel.innerHTML = this.translate('TEAM'); + headerRow.appendChild(nameLabel); - var pointsLabel = document.createElement('th'); + const pointsLabel = document.createElement('th'); pointsLabel.classList.add('centered'); - var points = document.createElement('i'); - points.classList.add('fa', 'fa-line-chart'); - pointsLabel.appendChild(points); - row.appendChild(pointsLabel); + const pointsIcon = document.createElement('i'); + pointsIcon.classList.add('fa', 'fa-line-chart'); + pointsLabel.appendChild(pointsIcon); + headerRow.appendChild(pointsLabel); - var goalsLabel = document.createElement('th'); + const goalsLabel = document.createElement('th'); goalsLabel.classList.add('centered'); - var goals = document.createElement('i'); - goals.classList.add('fa', 'fa-soccer-ball-o'); - goalsLabel.appendChild(goals); - row.appendChild(goalsLabel); + const goalsIcon = document.createElement('i'); + goalsIcon.classList.add('fa', 'fa-soccer-ball-o'); + goalsLabel.appendChild(goalsIcon); + headerRow.appendChild(goalsLabel); - table.appendChild(row); + table.appendChild(headerRow); // Get First and Last teams to display in standings - var focusTeamIndex, firstTeam, lastTeam; + let focusTeamIndex; + let firstTeam; + let lastTeam; /* focus_on for current league is set */ - if(this.config.focus_on){ + if (this.config.focus_on) { /* focus_on TOP */ - if(this.config.focus_on === 'TOP'){ + if (this.config.focus_on === 'TOP') { focusTeamIndex = -1; firstTeam = 0; - lastTeam = (this.config.max_teams && this.config.max_teams <= this.standing.length) ? this.config.max_teams : this.standing.length; - } - /* focus_on BOTTOM */ - else if(this.config.focus_on === 'BOTTOM'){ + lastTeam = (this.config.max_teams && this.config.max_teams <= this.standing.length) ? + this.config.max_teams : this.standing.length; + } else if (this.config.focus_on === 'BOTTOM') { focusTeamIndex = -1; - firstTeam = (this.config.max_teams && this.config.max_teams <= this.standing.length) ? this.standing.length - this.config.max_teams : 0; + firstTeam = (this.config.max_teams && this.config.max_teams <= this.standing.length) ? + this.standing.length - this.config.max_teams : 0; lastTeam = this.standing.length; - } - /* focus_on specific team */ - else { - for(var i = 0; i < this.standing.length; i++){ + } else { + for (let i = 0; i < this.standing.length; i += 1) { /* focus_on is teamName */ - if(this.standing[i].Team[0] === this.config.focus_on){ + if (this.standing[i].Team[0] === this.config.focus_on) { focusTeamIndex = i; /* max_teams is set */ - if(this.config.max_teams){ - var before = parseInt(this.config.max_teams / 2); + if (this.config.max_teams) { + const before = parseInt(this.config.max_teams / 2); firstTeam = focusTeamIndex - before >= 0 ? focusTeamIndex - before : 0; /* index for lastTeam is in range */ - if(firstTeam + this.config.max_teams <= this.standing.length){ - lastTeam = firstTeam + this.config.max_teams; + if (firstTeam + this.config.max_teams <= this.standing.length) { + lastTeam = firstTeam + this.config.max_teams; } else { lastTeam = this.standing.length; - firstTeam = lastTeam - this.config.max_teams >= 0 ? lastTeam - this.config.max_teams : 0; + firstTeam = lastTeam - this.config.max_teams >= 0 ? + lastTeam - this.config.max_teams : 0; } } else { firstTeam = 0; @@ -146,36 +149,36 @@ Module.register("MMM-ScottishPremierLeague",{ } // Render Team Rows - for(var i = firstTeam; i < lastTeam; i++){ - var row = document.createElement('tr'); - if(i === focusTeamIndex){ + for (let i = firstTeam; i < lastTeam; i += 1) { + const row = document.createElement('tr'); + if (i === focusTeamIndex) { row.classList.add('bright'); } - var pos = document.createElement('td'); + const pos = document.createElement('td'); pos.innerHTML = i + 1; row.appendChild(pos); - var name = document.createElement('td'); + const name = document.createElement('td'); name.classList.add('name'); name.innerHTML = this.standing[i].Team[0]; row.appendChild(name); - var points = document.createElement('td'); + const points = document.createElement('td'); points.innerHTML = this.standing[i].Points[0]; points.classList.add('centered'); row.appendChild(points); - var goals = document.createElement('td'); + const goals = document.createElement('td'); goals.innerHTML = this.standing[i].Goal_Difference[0]; goals.classList.add('centered'); row.appendChild(goals); // Create fade in/out effect. if (this.config.max_teams && focusTeamIndex >= 0) { - if (i != focusTeamIndex) { - var currentStep = Math.abs(i - focusTeamIndex); - row.style.opacity = 1 - (1 / this.config.max_teams * currentStep); + if (i !== focusTeamIndex) { + const currentStep = Math.abs(i - focusTeamIndex); + row.style.opacity = 1 - ((1 / this.config.max_teams) * currentStep); } } @@ -185,4 +188,4 @@ Module.register("MMM-ScottishPremierLeague",{ } return wrapper; } -}); \ No newline at end of file +}); diff --git a/README.md b/README.md index 34a2f69..8b909ad 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# MMM-ScottishPremierLeague +# MMM-ScottishPremierLeague [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://raw.githubusercontent.com/fewieden/MMM-ScottishPremierLeague/master/LICENSE) [![Build Status](https://travis-ci.org/fewieden/MMM-ScottishPremierLeague.svg?branch=master)](https://travis-ci.org/fewieden/MMM-ScottishPremierLeague) [![Code Climate](https://codeclimate.com/github/fewieden/MMM-ScottishPremierLeague/badges/gpa.svg?style=flat)](https://codeclimate.com/github/fewieden/MMM-ScottishPremierLeague) [![Known Vulnerabilities](https://snyk.io/test/github/fewieden/mmm-scottishpremierleague/badge.svg)](https://snyk.io/test/github/fewieden/mmm-scottishpremierleague) + Scottish Premier League Standings Module for MagicMirror2 ## Example @@ -6,14 +7,16 @@ Scottish Premier League Standings Module for MagicMirror2 ![](.github/example.jpg) ## Dependencies - * An installation of [MagicMirror2](https://github.com/MichMich/MagicMirror) - * npm - * [request](https://www.npmjs.com/package/request) - * [xml2js](https://www.npmjs.com/package/xml2js) + +* An installation of [MagicMirror2](https://github.com/MichMich/MagicMirror) +* npm +* [request](https://www.npmjs.com/package/request) +* [xml2js](https://www.npmjs.com/package/xml2js) ## Installation - 1. Clone this repo into `~/MagicMirror/modules` directory. - 2. Configure your `~/MagicMirror/config/config.js`: + +1. Clone this repo into `~/MagicMirror/modules` directory. +1. Configure your `~/MagicMirror/config/config.js`: ``` { @@ -25,13 +28,15 @@ Scottish Premier League Standings Module for MagicMirror2 } } ``` - 3. Run command `npm install` in `~/MagicMirror/modules/MMM-ScottishPremierLeague` directory. + +1. Run command `npm install --productive` in `~/MagicMirror/modules/MMM-ScottishPremierLeague` directory. ## Config Options + | **Option** | **Default** | **Description** | | --- | --- | --- | | `api_key` | REQUIRED | Get a free api key for the Scottish Premier League [here](http://www.xmlsoccer.com/Register.aspx). | | `season` | `'1617'` | Which season should be displayed. | | `focus_on` | `false` | Which team should the standings focus on e.g. "Rangers", "TOP" or "BOTTOM". Omit this option or set to false to show the full league table. | | `max_teams` | `false` | How many teams should be displayed. Omit this option or set to false to show the full league table. | -| `updateInterval` | `10800000` (3 hours) | How often should the data be fetched | \ No newline at end of file +| `updateInterval` | `10800000` (3 hours) | How often should the data be fetched | diff --git a/node_helper.js b/node_helper.js index 14daeb4..e92c1a8 100644 --- a/node_helper.js +++ b/node_helper.js @@ -6,21 +6,23 @@ * MIT Licensed. */ +/* eslint-env node */ +/* eslint-disable no-console */ + const request = require('request'); const parser = require('xml2js').parseString; -const fs = require('fs'); -const NodeHelper = require("node_helper"); +const NodeHelper = require('node_helper'); module.exports = NodeHelper.create({ baseUrl: 'http://www.xmlsoccer.com/FootballDataDemo.asmx/GetLeagueStandingsBySeason?', - start: function() { - console.log("Starting module: " + this.name); + start() { + console.log(`Starting module: ${this.name}`); }, - socketNotificationReceived: function(notification, payload) { - if (notification === "CONFIG") { + socketNotificationReceived(notification, payload) { + if (notification === 'CONFIG') { this.config = payload; this.getData(); setInterval(() => { @@ -29,22 +31,26 @@ module.exports = NodeHelper.create({ } }, - getData: function() { - request({url: this.baseUrl + 'ApiKey=' + this.config.api_key + '&league=3&seasonDateString=' + this.config.season}, (error, response, body) => { + getData() { + request( + { + url: `${this.baseUrl}ApiKey=${this.config.api_key}&league=3&seasonDateString=${this.config.season}` + }, (error, response, body) => { if (response.statusCode === 200) { parser(body, (err, result) => { - if(err) { + if (err) { console.log(err); - } else if(result.hasOwnProperty("XMLSOCCER.COM") && result["XMLSOCCER.COM"].hasOwnProperty("TeamLeagueStanding")){ - this.standing = result["XMLSOCCER.COM"].TeamLeagueStanding; - this.sendSocketNotification("DATA", this.standing); + } else if (Object.prototype.hasOwnProperty.call(result, 'XMLSOCCER.COM') && + Object.prototype.hasOwnProperty.call(result['XMLSOCCER.COM'], 'TeamLeagueStanding')) { + this.standing = result['XMLSOCCER.COM'].TeamLeagueStanding; + this.sendSocketNotification('DATA', this.standing); } else { - console.log("MMM-ScottishPremierLeague: Error no data"); + console.log('MMM-ScottishPremierLeague: Error no data'); } }); } else { - console.log("MMM-ScottishPremierLeague: Error getting data " + response.statusCode); + console.log(`MMM-ScottishPremierLeague: Error getting data ${response.statusCode}`); } }); } -}); \ No newline at end of file +}); diff --git a/package.json b/package.json index ca88cb9..0839be2 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "mmm-scottishpremierleague", "version": "1.0.0", "description": "Scottish Premier League Standings Module for MagicMirror2", + "scripts": { + "lint": "./node_modules/.bin/eslint . && ./node_modules/.bin/stylelint ." + }, "repository": { "type": "git", "url": "git+https://github.com/fewieden/MMM-ScottishPremierLeague.git" @@ -16,8 +19,15 @@ "url": "https://github.com/fewieden/MMM-ScottishPremierLeague/issues" }, "homepage": "https://github.com/fewieden/MMM-ScottishPremierLeague#readme", + "devDependencies": { + "eslint": "^3.14.1", + "eslint-config-airbnb-base": "^11.0.1", + "eslint-plugin-import": "^2.2.0", + "stylelint": "^7.8.0", + "stylelint-config-standard": "^16.0.0" + }, "dependencies": { "request": "^2.74.0", "xml2js": "^0.4.17" } -} \ No newline at end of file +}