Finish unused config (barely)

This commit is contained in:
Dan Mindru 2019-09-12 21:05:46 +02:00
parent 885e9e0903
commit cea149d1ce
9 changed files with 775 additions and 1627 deletions

2268
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,13 +23,13 @@
}, },
"homepage": "https://github.com/danmindru/responsive-html-email-signature#readme", "homepage": "https://github.com/danmindru/responsive-html-email-signature#readme",
"scripts": { "scripts": {
"start": "gulp", "start": "node ./node_modules/.bin/gulp",
"once": "gulp run-pipeline", "once": "node ./node_modules/.bin/gulp run-pipeline",
"deploy": "npm run test && cp -r dist demo && git push origin `git subtree split --prefix demo develop`:gh-pages --force", "deploy": "npm run test && cp -r dist demo && git push origin `git subtree split --prefix demo develop`:gh-pages --force",
"test": "npm run once && node ./node_modules/.bin/ava", "test": "npm run once && node ./node_modules/.bin/ava",
"test:watch": "npm run once && node ./node_modules/.bin/ava --watch", "test:watch": "npm run once && node ./node_modules/.bin/ava --watch",
"format": "prettier {tasks,tests}/**/*.js gulpfile.js .eslintrc.js --write", "format": "node ./node_modules/.bin/prettier {tasks,tests}/**/*.js gulpfile.js .eslintrc.js --write",
"lint": "eslint ./**/*.js gulpfile.js" "lint": "node ./node_modules/.bin/eslint ./**/*.js gulpfile.js"
}, },
"dependencies": { "dependencies": {
"autoprefixer": "^9.6.1", "autoprefixer": "^9.6.1",
@ -75,12 +75,12 @@
"husky": { "husky": {
"hooks": { "hooks": {
"pre-push": "npm run test", "pre-push": "npm run test",
"pre-commit": "npm run lint && ./node_modules/.bin/pretty-quick --staged --pattern ./**/*.js" "pre-commit": "npm run lint && node ./node_modules/.bin/pretty-quick --staged --pattern ./**/*.js"
} }
}, },
"ava": { "ava": {
"helpers": [ "helpers": [
"**/util.js" "**/util.js"
] ]
} }
} }

View File

@ -1,11 +1,9 @@
const gulp = require('gulp'); const gulp = require('gulp');
const fs = require('fs');
const chalk = require('chalk'); const chalk = require('chalk');
const { getConfigsForDir, getFilePathsForDir, log } = require('./util/util'); const { getConfigsForDir, getFilePathsForDir, getHtmlTemplatesFromFilelist, log } = require('./util/util');
const OUTPUT_KEYWORD = '@echo'; const OUTPUT_KEYWORD = '@echo';
// todo: needs a proper refactor.
function checkForUnusedTask(options) { function checkForUnusedTask(options) {
gulp.task('check-for-unused', async done => { gulp.task('check-for-unused', async done => {
const configs = getConfigsForDir(options.workingDir, options.configurationFile); const configs = getConfigsForDir(options.workingDir, options.configurationFile);
@ -16,19 +14,29 @@ function checkForUnusedTask(options) {
}); });
} }
// todo: find configs by id instead of using the index? /**
* Outputs warnings for unused items.
*
* @param { Array<Array<string>> } unusedItems
* @param { Array<object> } configs
*/
const outputWarningsForUnusedItems = (unusedItems, configs) => { const outputWarningsForUnusedItems = (unusedItems, configs) => {
const find = OUTPUT_KEYWORD; const find = OUTPUT_KEYWORD;
const regex = new RegExp(find, 'g'); const regex = new RegExp(find, 'g');
unusedItems.forEach((unusedInConfigs, index) => { unusedItems.forEach((unusedInConfigs, index) => {
const { dir } = configs[index]; const { dir } = configs[index];
unusedInConfigs.forEach((unusedInConfItems, index) => { unusedInConfigs.forEach(unusedInConfItems => {
log.warn( const unusedItemsToLog = unusedInConfItems.filter(item => item !== `${OUTPUT_KEYWORD} id`);
`${unusedInConfItems.length} unused properties in ${dir}: ${unusedInConfItems
.reduce((acc, cur) => (acc ? `${acc}, ${chalk.white(cur)}` : chalk.white(cur)), '') if (unusedItemsToLog.length) {
.replace(regex, '')}` log.warn(
); `${unusedItemsToLog.length} unused properties in ${dir}: ${unusedItemsToLog
.reduce((acc, cur) => (acc ? `${acc}, ${chalk.white(cur)}` : chalk.white(cur)), '')
.replace(regex, '')}`
);
}
}); });
}); });
}; };
@ -39,49 +47,27 @@ const outputWarningsForUnusedItems = (unusedItems, configs) => {
* @param { string } rootDir * @param { string } rootDir
* @param { Array } configs Array of configs. * @param { Array } configs Array of configs.
*/ */
const checkForUnusedItemsInConfigs = (rootDir, configs) => { const checkForUnusedItemsInConfigs = (rootDir, configs) =>
return Promise.all( Promise.all(
configs.map(async ({ dir, confItems }) => { configs.map(async ({ dir, confItems }) => {
return Promise.all( return Promise.all(
confItems.map(async confItem => { confItems.map(async confItem => {
const definedStrings = Object.keys(confItem).map(key => `${OUTPUT_KEYWORD} ${key}`); const definedStrings = Object.keys(confItem).map(key => `${OUTPUT_KEYWORD} ${key}`);
const cwd = `${rootDir}/${dir}`; const cwd = `${rootDir}/${dir}`;
const files = await getFilePathsForDir(cwd); const files = await getFilePathsForDir(cwd);
const htmlTemplates = await self.getHtmlTemplatesFromFilelist(files); const htmlTemplates = await getHtmlTemplatesFromFilelist(files);
const concatenatedTemplates = htmlTemplates.join(''); const concatenatedTemplates = htmlTemplates.join('');
return definedStrings.filter(str => concatenatedTemplates.includes(str)); return definedStrings.filter(str => !concatenatedTemplates.includes(str));
}) })
); );
}) })
); );
};
// todo: should be util, so should the one in build.js
const getHtmlTemplatesFromFilelist = filelist => {
return Promise.all(
filelist
.filter(file => !!file.match(/.*\.html/) && !file.match(/.*\.inc*\.html/))
.map(
htmlTemplate =>
new Promise((resolve, reject) => {
fs.readFile(htmlTemplate, 'utf8', (error, data) => {
if (error) {
reject(error);
}
resolve(data);
});
})
)
);
};
const self = { const self = {
checkForUnusedTask, checkForUnusedTask,
outputWarningsForUnusedItems, outputWarningsForUnusedItems,
checkForUnusedItemsInConfigs, checkForUnusedItemsInConfigs
getHtmlTemplatesFromFilelist
}; };
module.exports = self; module.exports = self;

View File

@ -43,7 +43,6 @@ const getConfigsForDir = (rootDir, configFileName) => {
.filter(config => config); .filter(config => config);
}; };
// todo test
/** /**
* Given a directory, gets all file paths in it. * Given a directory, gets all file paths in it.
* *
@ -68,6 +67,30 @@ const getFilePathsForDir = dir => {
}); });
}; };
/**
* Gets an array of html files in a filelist.
*
* @param { Array } filelist
*/
const getHtmlTemplatesFromFilelist = filelist => {
return Promise.all(
filelist
.filter(file => file.match(/.*\.html/) || file.match(/.*\.inc*\.html/))
.map(
htmlTemplate =>
new Promise((resolve, reject) => {
fs.readFile(htmlTemplate, 'utf8', (error, data) => {
if (error) {
reject(error);
}
resolve(data);
});
})
)
);
};
const log = { const log = {
warn: (...messages) => { warn: (...messages) => {
console.warn('🔵 ', chalk.yellow(messages)); console.warn('🔵 ', chalk.yellow(messages));
@ -85,7 +108,8 @@ const log = {
const self = { const self = {
log, log,
getConfigsForDir, getConfigsForDir,
getFilePathsForDir getFilePathsForDir,
getHtmlTemplatesFromFilelist
}; };
module.exports = self; module.exports = self;

View File

@ -1,5 +1,4 @@
const test = require('ava'); const test = require('ava');
const path = require('path');
const { readFileSync } = require('../util'); const { readFileSync } = require('../util');
test('dark signature output', async t => { test('dark signature output', async t => {

View File

@ -1,11 +0,0 @@
{
"id": "dark",
"signature": "Best regards,",
"name": "The dark mail team",
"contactMain": "Call <a href='tel:004580100100'><span>(45) 80100100</span></a> or email us at",
"contactMail": "info@dark.dk",
"slogan": "LED Pylon. LED Wall. Digital Signage.",
"logoUrl": "/assets/dark.png",
"logoAlt": "dark logo",
"website": "http://dark.dk"
}

View File

@ -1,13 +0,0 @@
const test = require('ava');
const { checkForUnusedItemsInConfigs } = require('../../tasks/check-for-unused.js');
const { readFileSync } = require('../util');
test('getting templates from filelist', async t => {
const configs = [
{ confItems: [readFileSync('tests/sample/configs/config-with-unused.json')], dir: 'dark' }
];
const expect = [[[[]]]];
const result = [await checkForUnusedItemsInConfigs('tests/sample', configs)];
t.deepEqual(result, expect);
});

View File

@ -1,3 +0,0 @@
const test = require('ava');
test('noop', async t => { t.deepEqual('', ''); });

View File

@ -1,5 +1,5 @@
const fs = require('fs'); const fs = require('fs');
module.exports = { module.exports = {
readFileSync: path => fs.readFileSync(('./', path), 'utf8') readFileSync: path => fs.readFileSync(('./', path), 'utf8')
} };