2021-07-21 20:08:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-07-23 12:32:58 +02:00
|
|
|
const { ESLint } = require('eslint');
|
2021-07-21 20:08:17 +02:00
|
|
|
const componentGenerator = require('./component');
|
|
|
|
|
|
|
|
// This is used to be able to indent block inside Handlebars helpers and improve templates visibility.
|
|
|
|
// It's not very robust, and forces you to use 2 spaces indentation inside for your blocks.
|
|
|
|
// If it become a pain don't hesitate to remove it.
|
|
|
|
const leftShift = str => str.replace(/^ {2}/gm, '');
|
|
|
|
|
2021-07-22 14:10:54 +02:00
|
|
|
const evaluateExpression = (a, operator, b) => {
|
|
|
|
switch (operator) {
|
|
|
|
case '==':
|
|
|
|
return a == b;
|
|
|
|
case '===':
|
|
|
|
return a === b;
|
|
|
|
case '!=':
|
|
|
|
return a != b;
|
|
|
|
case '!==':
|
|
|
|
return a !== b;
|
|
|
|
case '<':
|
|
|
|
return a < b;
|
|
|
|
case '<=':
|
|
|
|
return a <= b;
|
|
|
|
case '>':
|
|
|
|
return a > b;
|
|
|
|
case '>=':
|
|
|
|
return a >= b;
|
|
|
|
case '&&':
|
|
|
|
return a && b;
|
|
|
|
case '||':
|
|
|
|
return a || b;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-21 20:08:17 +02:00
|
|
|
// ! Don't use arrow functions to register Handlebars helpers
|
|
|
|
module.exports = function(
|
|
|
|
/** @type {import('plop').NodePlopAPI} */
|
|
|
|
plop
|
|
|
|
) {
|
2021-07-22 14:10:54 +02:00
|
|
|
plop.setHelper('if', function(/* ...args, options */) {
|
|
|
|
const end = arguments.length - 1;
|
|
|
|
const { fn, inverse } = arguments[end];
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
const condition = arguments[0];
|
|
|
|
return leftShift(condition ? fn(this) : inverse(this));
|
|
|
|
} else {
|
|
|
|
const [a, operator, b] = Array.from(arguments).slice(0, end);
|
|
|
|
return leftShift(evaluateExpression(a, operator, b) ? fn(this) : inverse(this));
|
|
|
|
}
|
2021-07-21 20:08:17 +02:00
|
|
|
});
|
2021-07-22 14:10:54 +02:00
|
|
|
plop.setHelper('unless', function(/* ...args, options */) {
|
2021-07-21 20:08:17 +02:00
|
|
|
const end = arguments.length - 1;
|
|
|
|
const { fn, inverse } = arguments[end];
|
2021-07-22 14:10:54 +02:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
const condition = arguments[0];
|
|
|
|
return leftShift(!condition ? fn(this) : inverse(this));
|
|
|
|
} else {
|
|
|
|
const [a, operator, b] = Array.from(arguments).slice(0, end);
|
|
|
|
return leftShift(!evaluateExpression(a, operator, b) ? fn(this) : inverse(this));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
plop.setHelper('else', function(_, { fn }) {
|
|
|
|
return leftShift(fn(this));
|
2021-07-21 20:08:17 +02:00
|
|
|
});
|
2021-07-23 12:32:58 +02:00
|
|
|
plop.setActionType('lint', async function(answers, config, plopfileApi) {
|
|
|
|
const { files } = config;
|
|
|
|
const patterns = files.map(file => plopfileApi.renderString(file, answers));
|
|
|
|
|
|
|
|
const eslint = new ESLint({ fix: true });
|
|
|
|
const results = await eslint.lintFiles(patterns);
|
|
|
|
await ESLint.outputFixes(results);
|
|
|
|
return 'Linting errors autofixed.';
|
|
|
|
});
|
2021-07-21 20:08:17 +02:00
|
|
|
plop.setGenerator('component', componentGenerator);
|
|
|
|
};
|