Lint: check githooks folder

This commit is contained in:
Ivo Julca
2016-03-05 22:10:32 -05:00
parent c73ef9ed39
commit 0f5810a054
4 changed files with 112 additions and 74 deletions

34
githooks/.eslintrc.js Normal file
View File

@@ -0,0 +1,34 @@
'use strict';
const baseRules = Object.assign({}, require('./../.eslintrc.js').rules);
module.exports = {
"root": true,
"env": {
"node": true,
},
"rules": Object.assign(baseRules, {
"comma-dangle": [2, "always-multiline"],
"eqeqeq": 2,
"no-floating-decimal": 2,
"no-new": 2,
"no-redeclare": 2,
"radix": [1, "as-needed"],
"strict": [2, "global"],
"no-unused-vars": [1, {"args": "none"}],
"no-use-before-define": [2, "nofunc"],
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {"before": true, "after": true}],
"new-cap": [2, {"newIsCap": true, "capIsNew": false}],
"no-var": 2,
"padded-blocks": [2, "never"],
}),
"ecmaFeatures": {
"arrowFunctions": true,
"blockBindings": true,
"classes": true,
"forOf": true,
"octalLiterals": true,
"spread": true,
},
};

View File

@@ -10,7 +10,7 @@ const dataDir = path.resolve(rootDir, 'data');
process.stdout.write("Syncing data from Git repository... ");
child_process.execSync('git pull', {
cwd: path.resolve(dataDir, 'Pokemon-Showdown')
cwd: path.resolve(dataDir, 'Pokemon-Showdown'),
});
console.log("DONE");
@@ -23,11 +23,12 @@ Tools.includeMods();
console.log("DONE");
function es3stringify(obj) {
var buf = JSON.stringify(obj);
let buf = JSON.stringify(obj);
buf = buf.replace(/\"([A-Za-z][A-Za-z0-9]*)\"\:/g, '$1:');
buf = buf.replace(/return\:/g, '"return":').replace(/new\:/g, '"new":').replace(/delete\:/g, '"delete":');
return buf;
}
function requireNoCache(pathSpec) {
delete require.cache[require.resolve(pathSpec)];
return require(pathSpec);
@@ -40,7 +41,7 @@ function requireNoCache(pathSpec) {
{
process.stdout.write("Building `data/search-index.js`... ");
var index = [];
let index = [];
index = index.concat(Object.keys(Tools.data.Pokedex).map(x => x + ' pokemon'));
index = index.concat(Object.keys(Tools.data.Movedex).map(x => x + ' move'));
@@ -130,17 +131,17 @@ function requireNoCache(pathSpec) {
});
let BattleSearchIndexOffset = BattleSearchIndex.map((entry, i) => {
var id = entry[0];
var name = '';
const id = entry[0];
let name = '';
switch (entry[1]) {
case 'pokemon': name = Tools.getTemplate(id).species; break;
case 'move': name = Tools.getMove(id).name; break;
case 'item': name = Tools.getItem(id).name; break;
case 'ability': name = Tools.getAbility(id).name; break;
}
var res = '';
var nonAlnum = 0;
for (var i = 0, j = 0; i < id.length; i++, j++) {
let res = '';
let nonAlnum = 0;
for (let i = 0, j = 0; i < id.length; i++, j++) {
while (!/[a-zA-Z0-9]/.test(name[j])) {
j++;
nonAlnum++;
@@ -160,7 +161,7 @@ function requireNoCache(pathSpec) {
BattleSearchCountIndex[type + ' pokemon'] = Object.keys(Tools.data.Pokedex).filter(id => (Tools.data.Pokedex[id].types.indexOf(type) >= 0)).length;
}
var buf = '// automatically built with githooks/build-indexes\n\n';
let buf = '// automatically built with githooks/build-indexes\n\n';
buf += 'exports.BattleSearchIndex = ' + JSON.stringify(BattleSearchIndex) + ';\n\n';
@@ -201,7 +202,7 @@ process.stdout.write("Building `data/teambuilder-tables.js`... ");
if (template.gen > genNum) continue;
const tier = (() => {
if (isDoubles) {
var banlist = Tools.getFormat('doublesou').banlist;
let banlist = Tools.getFormat('doublesou').banlist;
if (banlist.indexOf(template.species) >= 0 || banlist.indexOf(template.baseSpecies) >= 0) {
return "DUber";
}
@@ -552,7 +553,7 @@ process.stdout.write("Building `data/pokedex.js`... ");
if (formatsEntry.unreleasedHidden) entry.unreleasedHidden = formatsEntry.unreleasedHidden;
}
}
var buf = 'exports.BattlePokedex = ' + es3stringify(Pokedex) + ';';
const buf = 'exports.BattlePokedex = ' + es3stringify(Pokedex) + ';';
fs.writeFileSync(path.resolve(dataDir, 'pokedex.js'), buf);
}
@@ -566,7 +567,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const Movedex = requireNoCache('../data/Pokemon-Showdown/data/moves.js').BattleMovedex;
var buf = 'exports.BattleMovedex = ' + es3stringify(Movedex) + ';';
const buf = 'exports.BattleMovedex = ' + es3stringify(Movedex) + ';';
fs.writeFileSync(path.resolve(dataDir, 'moves.js'), buf);
}
@@ -576,7 +577,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const Items = requireNoCache('../data/Pokemon-Showdown/data/items.js').BattleItems;
var buf = 'exports.BattleItems = ' + es3stringify(Items) + ';';
const buf = 'exports.BattleItems = ' + es3stringify(Items) + ';';
fs.writeFileSync(path.resolve(dataDir, 'items.js'), buf);
}
@@ -586,7 +587,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const Abilities = requireNoCache('../data/Pokemon-Showdown/data/abilities.js').BattleAbilities;
var buf = 'exports.BattleAbilities = ' + es3stringify(Abilities) + ';';
const buf = 'exports.BattleAbilities = ' + es3stringify(Abilities) + ';';
fs.writeFileSync(path.resolve(dataDir, 'abilities.js'), buf);
}
@@ -596,7 +597,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const TypeChart = requireNoCache('../data/Pokemon-Showdown/data/typechart.js').BattleTypeChart;
var buf = 'exports.BattleTypeChart = ' + es3stringify(TypeChart) + ';';
const buf = 'exports.BattleTypeChart = ' + es3stringify(TypeChart) + ';';
fs.writeFileSync(path.resolve(dataDir, 'typechart.js'), buf);
}
@@ -606,7 +607,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const Aliases = requireNoCache('../data/Pokemon-Showdown/data/aliases.js').BattleAliases;
var buf = 'exports.BattleAliases = ' + es3stringify(Aliases) + ';';
const buf = 'exports.BattleAliases = ' + es3stringify(Aliases) + ';';
fs.writeFileSync(path.resolve(dataDir, 'aliases.js'), buf);
}
@@ -616,7 +617,7 @@ process.stdout.write("Building `data/moves,items,abilities,typechart,learnsets.j
{
const Learnsets = requireNoCache('../data/Pokemon-Showdown/data/learnsets.js').BattleLearnsets;
var buf = 'exports.BattleLearnsets = ' + es3stringify(Learnsets) + ';';
const buf = 'exports.BattleLearnsets = ' + es3stringify(Learnsets) + ';';
fs.writeFileSync(path.resolve(dataDir, 'learnsets.js'), buf);
}

View File

@@ -9,29 +9,32 @@
* post-commit, post-checkout, post-merge, post-rewrite
*/
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var exec = require('child_process').exec;
var sugar = require('sugar');
"use strict";
var thisFile = __filename;
var thisDir = __dirname;
var rootDir = path.resolve(thisDir, '..');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const exec = require('child_process').exec;
require('sugar');
const thisFile = __filename;
const thisDir = __dirname;
const rootDir = path.resolve(thisDir, '..');
const Tools = require('../data/Pokemon-Showdown/tools').includeData();
const toId = Tools.getId;
function updateIndex () {
var indexContents = fs.readFileSync(path.resolve(rootDir, 'index.template.html'), { encoding: 'utf8' });
function updateIndex() {
let indexContents = fs.readFileSync(path.resolve(rootDir, 'index.template.html'), {encoding: 'utf8'});
// add hashes to js and css files
process.stdout.write("Updating hashes... ");
indexContents = indexContents.replace(/(src|href)="\/(.*?)\?[a-z0-9]*?"/g, function (a, b, c) {
var hash = Math.random(); // just in case creating the hash fails
let hash = Math.random(); // just in case creating the hash fails
try {
var filename = c.replace('/play.pokemonshowdown.com/', '');
var fstr = fs.readFileSync(path.resolve(rootDir, filename), { encoding: 'utf8' });
const filename = c.replace('/play.pokemonshowdown.com/', '');
const fstr = fs.readFileSync(path.resolve(rootDir, filename), {encoding: 'utf8'});
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
@@ -42,7 +45,7 @@ function updateIndex () {
// add news
process.stdout.write("Updating news... ");
exec('php ' + path.resolve(thisDir, 'news-data.php'), function (error, stdout, stderr) {
var newsData = [0, '[failed to retrieve news]'];
let newsData = [0, '[failed to retrieve news]'];
if (!error && !stderr) {
try {
newsData = JSON.parse(stdout);
@@ -63,48 +66,48 @@ function updateIndex () {
});
}
function updateLearnsets (callback) {
var reservedKeywords = ['return']; // `return` is the only ES3+ reserved keyword that (currently) raises conflicts
var numberRegExp = new RegExp('^[0-9]*');
var reservedRegExp = new RegExp('^(' + reservedKeywords.join('|') + ')$', 'g');
var alphabetize = function (a, b) {return a.localeCompare(b);};
var getLsetGen = function (lset) {return lset.charAt(0);};
function updateLearnsets(callback) {
const reservedKeywords = ['return']; // `return` is the only ES3+ reserved keyword that (currently) raises conflicts
const numberRegExp = new RegExp('^[0-9]*');
const reservedRegExp = new RegExp('^(' + reservedKeywords.join('|') + ')$', 'g');
const alphabetize = (a, b) => a.localeCompare(b);
const getLsetGen = lset => lset.charAt(0);
var padNumString = function (str) {
const padNumString = function (str) {
switch (str.length) {
case 1: return '00' + str;
case 2: return '0' + str;
case 3: return '' + str;
}
};
var inLearnset = function (lset, learnset) {
var secondChar = lset.charAt(1);
const inLearnset = function (lset, learnset) {
const secondChar = lset.charAt(1);
if (secondChar !== 'L') return learnset.indexOf(lset) >= 0;
var firstFragment = lset.substr(0, 2);
var levelFragment = lset.substring(2, 5);
var paddedLevel = padNumString(levelFragment);
for (var i = 0, len = learnset.length; i < len; i++) {
const firstFragment = lset.substr(0, 2);
const levelFragment = lset.substring(2, 5);
const paddedLevel = padNumString(levelFragment);
for (let i = 0, len = learnset.length; i < len; i++) {
if (learnset[i].substring(0, 2) !== firstFragment) continue;
// ignore PSdex starter moves sorting data
if (paddedLevel === padNumString(learnset[i].substring(2, 5))) return true;
}
return false;
};
var formatLset = function (lset) {
var secondChar = lset.charAt(1);
const formatLset = function (lset) {
const secondChar = lset.charAt(1);
if (secondChar !== 'L') return lset;
var firstFragment = lset.substr(0, 2);
var levelFragment = lset.substr(2).match(numberRegExp)[0];
var sortFragment = lset.substr(2 + levelFragment.length);
const firstFragment = lset.substr(0, 2);
const levelFragment = lset.substr(2).match(numberRegExp)[0];
const sortFragment = lset.substr(2 + levelFragment.length);
return firstFragment + padNumString(levelFragment) + sortFragment;
};
const Pokedex = Tools.data.Pokedex;
const Learnsets = Tools.data.Learnsets;
var newLearnsetsG6 = {};
var oldLearnsetsG6;
const newLearnsetsG6 = {};
let oldLearnsetsG6;
try {
oldLearnsetsG6 = require(path.join(rootDir, 'data', 'learnsets-g6.js')).BattleLearnsets;
} catch (err) {
@@ -112,40 +115,40 @@ function updateLearnsets (callback) {
oldLearnsetsG6 = {};
}
for (var speciesid in Learnsets) {
for (const speciesid in Learnsets) {
if (!oldLearnsetsG6[speciesid] || !oldLearnsetsG6[speciesid].learnset) {
console.log("NEW ENTRY at learnsets-g6.js: " + Pokedex[speciesid].species + ".");
oldLearnsetsG6[speciesid] = {learnset: {}};
}
}
for (var speciesid in oldLearnsetsG6) {
for (const speciesid in oldLearnsetsG6) {
if (!oldLearnsetsG6[speciesid] || !oldLearnsetsG6[speciesid].learnset) return callback(new TypeError("Invalid `learnsets-g6.js` entry for " + speciesid + "."));
if (!Learnsets[speciesid]) {
console.log("REMOVED ENTRY at learnsets-g6.js: " + (Pokedex[speciesid] ? Pokedex[speciesid].species : speciesid) + ".");
continue;
}
var newLearnset = {};
var oldLearnset = oldLearnsetsG6[speciesid].learnset;
var fullLearnset = Learnsets[speciesid].learnset;
const newLearnset = {};
const oldLearnset = oldLearnsetsG6[speciesid].learnset;
const fullLearnset = Learnsets[speciesid].learnset;
if (!fullLearnset) return callback(new TypeError("Invalid data at `learnsets.js` for " + speciesid + "."));
// copy, but ignore moves removed in main file
for (var moveid in oldLearnset) {
for (const moveid in oldLearnset) {
if (!Array.isArray(oldLearnset[moveid])) return callback(new TypeError("Invalid data at `learnsets-g6.js` for " + speciesid + ":" + moveid + "."));
if (!fullLearnset[moveid]) continue;
newLearnset[moveid] = [];
for (var i = 0, len = oldLearnset[moveid].length; i < len; i++) {
for (let i = 0, len = oldLearnset[moveid].length; i < len; i++) {
if (!inLearnset(oldLearnset[moveid][i], fullLearnset[moveid])) continue;
newLearnset[moveid].push(oldLearnset[moveid][i]);
}
}
for (var moveid in fullLearnset) {
for (const moveid in fullLearnset) {
if (!Array.isArray(fullLearnset[moveid])) return callback(new TypeError("Invalid data at `learnsets.js` for " + speciesid + ":" + moveid + "."));
if (!newLearnset[moveid]) newLearnset[moveid] = [];
newLearnset[moveid] = newLearnset[moveid].map(formatLset);
for (var i = 0, len = fullLearnset[moveid].length; i < len; i++) {
for (let i = 0, len = fullLearnset[moveid].length; i < len; i++) {
if (getLsetGen(fullLearnset[moveid][i]) !== '6') continue;
if (inLearnset(fullLearnset[moveid][i], newLearnset[moveid])) continue;
newLearnset[moveid].push(formatLset(fullLearnset[moveid][i]));
@@ -156,8 +159,8 @@ function updateLearnsets (callback) {
newLearnsetsG6[speciesid] = {learnset: newLearnset};
}
var buf = [];
var pokemonList = Object.values(Pokedex).sort(function (a, b) {
const buf = [];
const pokemonList = Object.values(Pokedex).sort(function (a, b) {
// Missingno. goes first (zeroth); afterwards, CAP in descending dex order (increasingly negative)
// Finally, standard Pokémon in ascending dex order
if (a.num <= 0 && b.num > 0) return -1;
@@ -166,22 +169,22 @@ function updateLearnsets (callback) {
return a.num - b.num;
}).map('species').map(toId);
for (var i = 0, len = pokemonList.length; i < len; i++) {
var entry = newLearnsetsG6[pokemonList[i]];
for (let i = 0, len = pokemonList.length; i < len; i++) {
const entry = newLearnsetsG6[pokemonList[i]];
if (!entry || !entry.learnset) continue;
var lsetSerialized = '{' + Object.keys(entry.learnset).sort(alphabetize).map(function (moveid) {
const lsetSerialized = '{' + Object.keys(entry.learnset).sort(alphabetize).map(function (moveid) {
return moveid.replace(reservedRegExp, '"$1"') + ':' + JSON.stringify(entry.learnset[moveid]);
}).join(',') + '}';
buf.push(pokemonList[i] + ':{learnset:' + lsetSerialized + '}');
}
var writeStream = fs.createWriteStream(path.join(rootDir, 'data', 'learnsets-g6.js')).on('error', callback);
const writeStream = fs.createWriteStream(path.join(rootDir, 'data', 'learnsets-g6.js')).on('error', callback);
writeStream.write('exports.BattleLearnsets = {\n\t' + buf.join(',\n\t') + '\n};\n');
writeStream.end(callback);
}
var pendingFiles = 0;
var indexStats, updateStats, indexMTime, updateMTime;
let pendingFiles = 0;
let indexStats, updateStats, indexMTime, updateMTime;
try {
indexStats = fs.statSync(path.join(rootDir, 'index.html'));
indexMTime = indexStats.mtime.getTime();
@@ -201,9 +204,9 @@ try {
process.stdout.write("Updating file `data/learnsets-g6`... ");
pendingFiles++;
var learnsetsStats;
var learnsetsG6Stats;
var learnsetsG6ToUpdate = true;
let learnsetsStats;
let learnsetsG6Stats;
let learnsetsG6ToUpdate = true;
try {
learnsetsStats = fs.statSync(path.join(rootDir, 'data', 'learnsets.js'));
@@ -233,7 +236,7 @@ if (learnsetsG6ToUpdate && (!indexStats || !learnsetsG6Stats || indexMTime < upd
updateLearnsets(function (err) {
if (err) {
var stack = err.stack || '';
let stack = err.stack || '';
stack = "File `data/learnsets-g6` failed to update.\n" + stack;
console.error(stack);
} else {

View File

@@ -9,8 +9,8 @@
"url": "https://github.com/Zarel/Pokemon-Showdown-Client.git"
},
"scripts": {
"test": "eslint --config=.eslintrc.js --cache --cache-file=eslint-cache/base js/*.js data/*.js",
"fix": "eslint --config=.eslintrc.js --fix js/*.js data/*.js"
"test": "eslint --config=.eslintrc.js --cache --cache-file=eslint-cache/base js/*.js data/*.js && eslint --config=githooks/.eslintrc.js --cache --cache-file=eslint-cache/build githooks/update githooks/build-indexes",
"fix": "eslint --config=.eslintrc.js --fix js/*.js data/*.js && eslint --config=githooks/.eslintrc.js --fix githooks/update githooks/build-indexes"
},
"dependencies": {},
"devDependencies": {