Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created October 31, 2015 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/c3785c6c53cfee060319 to your computer and use it in GitHub Desktop.
Save swashcap/c3785c6c53cfee060319 to your computer and use it in GitHub Desktop.
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