Skip to content

Commit

Permalink
feat: Add first few stats
Browse files Browse the repository at this point in the history
Overridden elements, overridden components, properties, values
  • Loading branch information
sapegin committed Jul 31, 2020
1 parent b248916 commit de4689a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
37 changes: 34 additions & 3 deletions bin/antbear.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ const path = require('path');
const minimist = require('minimist');
const glob = require('glob');
const kleur = require('kleur');
const { flatMap } = require('lodash');
const { antbear } = require('../src');
const longest = require('longest');
const { flatMap, sortBy } = require('lodash');
const {
getInstances,
getElementsStats,
getComponentsStats,
getPropsStats,
getValuesStats,
} = require('../src');

function getBinaryName() {
const binaryPath = process.env._;
Expand Down Expand Up @@ -41,13 +48,37 @@ function printInstances(instances) {
});
}

function printObject(object, caption) {
const keyColWidth = longest(Object.keys(object)).length;
const valueColWidth = longest(Object.values(object)).length;
const rows = sortBy(Object.entries(object), ([, value]) => value).reverse();

console.log(kleur.underline(caption));
console.log();

rows.forEach(([key, value]) => {
console.log(
kleur.cyan(key.padEnd(keyColWidth)),
' ',
String(value).padStart(valueColWidth)
);
});

console.log();
}

const argv = minimist(process.argv.slice(2));
const patterns = argv._;
const isVerbose = argv.verbose;

if (patterns.length > 0) {
const files = flatMap(patterns, (pattern) => glob.sync(pattern));
const instances = antbear(files);
const instances = getInstances(files);

printObject(getElementsStats(instances), 'Overridden elements');
printObject(getComponentsStats(instances), 'Overridden components');
printObject(getPropsStats(instances), 'Properties');
printObject(getValuesStats(instances), 'Values');

if (isVerbose) {
printInstances(instances);
Expand Down
5 changes: 5 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 @@ -40,6 +40,7 @@
"glob": "^7.1.6",
"kleur": "^4.0.2",
"lodash": "^4.17.19",
"longest": "^2.0.1",
"minimist": "^1.2.5",
"stylis": "^4.0.2"
},
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
const fs = require('fs');
const { flatMap } = require('lodash');
const { parseJavaScript } = require('./util/parseJavaScript');
const {
getElementsStats,
getComponentsStats,
getPropsStats,
getValuesStats,
} = require('../src/util/calculateStats');

module.exports.antbear = function (files) {
function getInstances(files) {
return flatMap(files, (filename) => {
const code = fs.readFileSync(filename, 'utf8');
return parseJavaScript(code, filename);
});
}

module.exports = {
getInstances,
getElementsStats,
getComponentsStats,
getPropsStats,
getValuesStats,
};
24 changes: 24 additions & 0 deletions src/util/__tests__/calculateStats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { getComponentsStats } = require('../calculateStats');

test('getComponentsStats', () => {
const result = getComponentsStats([
{
component: 'Box',
styles: [],
},
{
component: 'div',
styles: [],
},
{
component: 'Box',
styles: [],
},
]);
expect(result).toMatchInlineSnapshot(`
Object {
"Box": 2,
"div": 1,
}
`);
});
33 changes: 33 additions & 0 deletions src/util/calculateStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { countBy, flatMap } = require('lodash');

// TODO: Custom colors
// TODO: Custom spacing

const isComponent = (name) => name.match(/^[A-Z]/);

const getAllStyles = (instances) =>
flatMap(instances, (instance) => instance.styles);

module.exports.getElementsStats = function (instances) {
const elementsOnly = instances.filter(
(instance) => !isComponent(instance.component)
);
return countBy(elementsOnly, (instance) => instance.component);
};

module.exports.getComponentsStats = function (instances) {
const componentsOnly = instances.filter((instance) =>
isComponent(instance.component)
);
return countBy(componentsOnly, (instance) => instance.component);
};

module.exports.getPropsStats = function (instances) {
const styles = getAllStyles(instances);
return countBy(styles, (style) => style.name);
};

module.exports.getValuesStats = function (instances) {
const styles = getAllStyles(instances);
return countBy(styles, (style) => style.value);
};

0 comments on commit de4689a

Please sign in to comment.