Skip to content

Commit

Permalink
Add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfr committed Mar 6, 2019
1 parent 08ce959 commit e5ad76e
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 1 deletion.
18 changes: 18 additions & 0 deletions content/linter/buttons/buttons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function ($) {
const errors = [];
let allButtonsHaveType = true;

$('button').each(function (index, el) {
const $el = $(el);

if (!$el.attr('type')) {
allButtonsHaveType = false;
}
});

if (!allButtonsHaveType) {
errors.push('Buttons need a `type` attribute.');
}

return errors;
};
5 changes: 5 additions & 0 deletions core/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ module.exports = {
moduleTemplates: path.join(contentPath, 'templates/modules/**/*.pug'),
patterns: path.join(contentPath, 'templates/_patterns/'),
components: path.join(contentPath, 'templates/_components/'),
allComponents: path.join(contentPath, 'templates/_components/**/*.pug'),
data: path.join(contentPath, 'data/*')
},
linter: {
path: path.join(contentPath, 'linter/'),
tests: path.join(contentPath, 'linter/**/*.js'),
},
js: {
entryFile: path.join(contentPath, 'js/index.js')
},
Expand Down
50 changes: 50 additions & 0 deletions core/tasks/linter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const pug = require('pug');
const cheerio = require('cheerio');
const chalk = require('chalk');

const locals = require('../templates/locals');
const paths = require('../paths');

const allComponentPaths = glob.sync(paths.content.templates.allComponents);
const linterPaths = glob.sync(paths.content.linter.tests);

module.exports = function (done) {
console.log('Running linters on Pug templates within components...\n');

allComponentPaths.forEach(function (templatePath) {

const fakeIcon = 'include ../../../../core/templates/mixins/icon\n'

const pugContent = fakeIcon + fs.readFileSync(templatePath, 'utf8');

const compiledHtml = pug.compile(pugContent, {
pretty: true,
basedir: '/content',
filename: templatePath
})(locals.getDefaultLocals());

const $ = cheerio.load(compiledHtml);

linterPaths.forEach(function (linterPath) {
const linter = require(path.join('../../', linterPath));

const linterResult = linter($);

if (linterResult && linterResult.length > 0) {
console.log(chalk.underline.bold(templatePath));

linterResult.forEach(function (result) {
console.log('\t' + chalk.red(result));
});

console.log('\n');
}
});
});

console.log('Done linting Pug files!');
done();
};
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const copy = require('./core/tasks/copy');
const watch = require('./core/tasks/watch');
const server = require('./core/tasks/server');
const iconFont = require('./core/tasks/icon-font');

const linter = require('./core/tasks/linter');
const config = require('./bedrock.config');

gulp.task('templates:clean', templates.clean);
Expand All @@ -24,6 +24,7 @@ gulp.task('copy:resources', copy.resources);
gulp.task('copy:compiledToDist', copy.compiledToDist);
gulp.task('bundle', bundle);
gulp.task('icon-font', iconFont);
gulp.task('lint', linter);

gulp.task('templates:compile', config.styleguide ?
['templates:compile:content', 'templates:compile:styleguide', 'templates:compile:docs'] :
Expand Down
124 changes: 124 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"browserify": "^16.2.3",
"cached-path-relative": ">=1.0.2",
"chalk": "^1.1.3",
"cheerio": "^1.0.0-rc.2",
"clipboard": "^1.5.3",
"codemirror": "^5.18.2",
"del": "^2.0.2",
Expand Down

0 comments on commit e5ad76e

Please sign in to comment.