Last active
April 26, 2017 23:49
-
-
Save ryanve/94b203c887950eb9e786d348286d2840 to your computer and use it in GitHub Desktop.
Get CSS selectors from file(s) as array(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var manifest = require('./manifest') | |
manifest.selectors('./node_modules/basscss/css/basscss.css').then(function(selectors) { | |
console.log(JSON.stringify(selectors, null, 2)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs') | |
var css = require('css') | |
function manifest(file) { | |
return new Promise(function(resolve, reject) { | |
fs.readFile(file, function(error, buffer) { | |
error ? reject(error) : resolve(css.parse(String(buffer), { | |
source: file | |
}).stylesheet) | |
}) | |
}) | |
} | |
function rules(file) { | |
return new Promise(function(resolve, reject) { | |
manifest(file).then(function(stylesheet) { | |
resolve(stylesheet.rules) | |
}).catch(reject) | |
}) | |
} | |
function flattener(method, key) { | |
return function(file) { | |
return new Promise(function(resolve, reject) { | |
manifest[method](file).then(function(results) { | |
resolve(results.reduce(function(flat, r) { | |
return r[key] == null ? flat : flat.concat(r[key]) | |
}, [])) | |
}).catch(reject) | |
}) | |
} | |
} | |
manifest.rules = rules | |
manifest.selectors = flattener('rules', 'selectors') | |
manifest.declarations = flattener('rules', 'declarations') | |
module.exports = manifest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment