Finish unused config (barely)

This commit is contained in:
Dan Mindru 2019-09-12 21:05:46 +02:00
parent f298f8bd85
commit 9610d244dd
9 changed files with 56 additions and 13974 deletions

13900
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -28,8 +28,8 @@
"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:watch": "npm run once && node ./node_modules/.bin/ava --watch",
"format": "prettier {tasks,tests}/**/*.js gulpfile.js .eslintrc.js --write",
"lint": "eslint ./**/*.js gulpfile.js"
"format": "node ./node_modules/.bin/prettier {tasks,tests}/**/*.js gulpfile.js .eslintrc.js --write",
"lint": "node ./node_modules/.bin/eslint ./**/*.js gulpfile.js"
},
"dependencies": {
"autoprefixer": "^9.6.1",
@ -75,12 +75,12 @@
"husky": {
"hooks": {
"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": {
"helpers": [
"**/util.js"
]
"**/util.js"
]
}
}

View File

@ -1,11 +1,9 @@
const gulp = require('gulp');
const fs = require('fs');
const chalk = require('chalk');
const { getConfigsForDir, getFilePathsForDir, log } = require('./util/util');
const { getConfigsForDir, getFilePathsForDir, getHtmlTemplatesFromFilelist, log } = require('./util/util');
const OUTPUT_KEYWORD = '@echo';
// todo: needs a proper refactor.
function checkForUnusedTask(options) {
gulp.task('check-for-unused', async done => {
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 find = OUTPUT_KEYWORD;
const regex = new RegExp(find, 'g');
unusedItems.forEach((unusedInConfigs, index) => {
const { dir } = configs[index];
unusedInConfigs.forEach((unusedInConfItems, index) => {
log.warn(
`${unusedInConfItems.length} unused properties in ${dir}: ${unusedInConfItems
.reduce((acc, cur) => (acc ? `${acc}, ${chalk.white(cur)}` : chalk.white(cur)), '')
.replace(regex, '')}`
);
unusedInConfigs.forEach(unusedInConfItems => {
const unusedItemsToLog = unusedInConfItems.filter(item => item !== `${OUTPUT_KEYWORD} id`);
if (unusedItemsToLog.length) {
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 { Array } configs Array of configs.
*/
const checkForUnusedItemsInConfigs = (rootDir, configs) => {
return Promise.all(
const checkForUnusedItemsInConfigs = (rootDir, configs) =>
Promise.all(
configs.map(async ({ dir, confItems }) => {
return Promise.all(
confItems.map(async confItem => {
const definedStrings = Object.keys(confItem).map(key => `${OUTPUT_KEYWORD} ${key}`);
const cwd = `${rootDir}/${dir}`;
const files = await getFilePathsForDir(cwd);
const htmlTemplates = await self.getHtmlTemplatesFromFilelist(files);
const htmlTemplates = await getHtmlTemplatesFromFilelist(files);
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 = {
checkForUnusedTask,
outputWarningsForUnusedItems,
checkForUnusedItemsInConfigs,
getHtmlTemplatesFromFilelist
checkForUnusedItemsInConfigs
};
module.exports = self;

View File

@ -43,7 +43,6 @@ const getConfigsForDir = (rootDir, configFileName) => {
.filter(config => config);
};
// todo test
/**
* 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 = {
warn: (...messages) => {
console.warn('🔵 ', chalk.yellow(messages));
@ -85,7 +108,8 @@ const log = {
const self = {
log,
getConfigsForDir,
getFilePathsForDir
getFilePathsForDir,
getHtmlTemplatesFromFilelist
};
module.exports = self;

View File

@ -1,5 +1,4 @@
const test = require('ava');
const path = require('path');
const { readFileSync } = require('../util');
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');
module.exports = {
readFileSync: path => fs.readFileSync(('./', path), 'utf8')
}
readFileSync: path => fs.readFileSync(('./', path), 'utf8')
};