Finish unused config (barely)
This commit is contained in:
parent
24b2389abc
commit
a120430bfd
File diff suppressed because it is too large
Load Diff
|
|
@ -28,8 +28,8 @@
|
||||||
"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,7 +75,7 @@
|
||||||
"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": {
|
||||||
|
|
|
||||||
|
|
@ -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 => {
|
||||||
|
const unusedItemsToLog = unusedInConfItems.filter(item => item !== `${OUTPUT_KEYWORD} id`);
|
||||||
|
|
||||||
|
if (unusedItemsToLog.length) {
|
||||||
log.warn(
|
log.warn(
|
||||||
`${unusedInConfItems.length} unused properties in ${dir}: ${unusedInConfItems
|
`${unusedItemsToLog.length} unused properties in ${dir}: ${unusedItemsToLog
|
||||||
.reduce((acc, cur) => (acc ? `${acc}, ${chalk.white(cur)}` : chalk.white(cur)), '')
|
.reduce((acc, cur) => (acc ? `${acc}, ${chalk.white(cur)}` : chalk.white(cur)), '')
|
||||||
.replace(regex, '')}`
|
.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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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 => {
|
||||||
|
|
@ -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"
|
|
||||||
}
|
|
||||||
|
|
@ -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);
|
|
||||||
});
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
const test = require('ava');
|
|
||||||
|
|
||||||
test('noop', async t => { t.deepEqual('', ''); });
|
|
||||||
|
|
@ -2,4 +2,4 @@ const fs = require('fs');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
readFileSync: path => fs.readFileSync(('./', path), 'utf8')
|
readFileSync: path => fs.readFileSync(('./', path), 'utf8')
|
||||||
}
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue