Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get info about stylesheets
'use strict';
var fs = require('fs');
var parse = require('css').parse;
var path = require('path');
var files = []; // Add your files here
var filePaths = files.map(function(file) {
return path.join(__dirname, file);
});
console.log(filePaths);
Promise.all(filePaths.map(function(file) {
return Promise.all([
Promise.resolve(file),
new Promise(function(resolve, reject) {
fs.stat(file, function(err, stat) {
if (err) {
return reject(err);
}
resolve(stat.size);
});
}),
new Promise(function(resolve, reject) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
return reject(err);
}
var rules;
var rulesCount;
try {
rules = parse(data).stylesheet.rules;
rulesCount = rules.reduce(function(total, node) {
if (
node.type === 'rule' &&
Array.isArray(node.selectors)
) {
total += node.selectors.length;
}
return total
}, 0);
resolve(rulesCount);
} catch(error) {
resolve('?');
// reject(error);
}
});
}),
]);
}))
.then(function(results) {
results.forEach(function(result) {
console.log('' + result[1] + '\t' + result[2]);
});
})
.catch(function(error) {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.