Last active
February 8, 2017 12:18
-
-
Save henrahmagix/2ababfaaa55aa69c57daa376b2bf6fb9 to your computer and use it in GitHub Desktop.
Find all top-level selectors, at least with a class included. Useful for finding style files to remove after removing HTML.
This file contains hidden or 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
'use strict'; | |
var fs = require('fs'); | |
var path = require('path'); | |
// npm install these. | |
var glob = require('glob'); | |
var gonzales = require('gonzales-pe'); | |
var infile = 'sass/**/*.sass'; | |
var output = []; | |
var parseToOutput = function (infile) { | |
if (output.length) { | |
output.push(''); | |
} | |
// Cross-syntax compliant comment to label blocks of output by filename. | |
output.push(`/* ${infile} */`); | |
var input = fs.readFileSync(infile).toString(); | |
console.log('parsing', infile); | |
// Syntax is computed from the filename. | |
var syntax = path.extname(infile).replace(/^\./, ''); | |
// tabSize cannot be computed: must be set. | |
var tabSize = 4; | |
var parseTree = gonzales.parse(input, {syntax, tabSize}); | |
var getTopLevelSelectors = function (tree) { | |
var result = []; | |
tree.forEach('ruleset', ruleset => { | |
ruleset.forEach('selector', selector => { | |
// If the top-level is a tagName, include the child selector | |
// to document more specificity. | |
if (selector.get(0).is('typeSelector')) { | |
ruleset.forEach('block', node => { | |
var nested = getTopLevelSelectors(node); | |
if (nested.length) { | |
nested.forEach(nestedResult => { | |
var joiner = ' '; | |
var nestedString = nestedResult; | |
if (nestedResult.startsWith('&')) { | |
// parentSelector so join it to the parent. | |
joiner = ''; | |
nestedString = nestedResult.replace(/^&/, ''); | |
} | |
result.push(selector.toString() + joiner + nestedString); | |
}); | |
} | |
}); | |
} else { | |
result.push(selector.toString()); | |
} | |
}); | |
}); | |
return result; | |
}; | |
output = output.concat(getTopLevelSelectors(parseTree)); | |
}; | |
glob.sync(infile).forEach(parseToOutput); | |
// Input is sass, so output can be sass and will be readable as such. | |
// Otherwise output as plaintext. | |
fs.writeFileSync('cssresult.sass', output.join('\n')); |
This file contains hidden or 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
// sass/_theme-default.sass | |
// sass/components/_breadcrumbs.sass | |
.breadcrumbs | |
// sass/components/_buttons.sass | |
button:not(.foo-view-button):disabled | |
button:not(.foo-view-button).disabled | |
button:not(.foo-view-button) .inline-svg | |
.button | |
button.button-solid | |
// sass/components/_carousel.sass | |
.homepage-inner-content | |
.carousel | |
.carousel-inner | |
.carousel-control | |
.carousel-indicators | |
.carousel-caption | |
// sass/components/_datepicker-popup.sass | |
.popup-datepicker | |
// sass/components/_datepicker.sass | |
.datepicker | |
.popup-datepicker | |
// sass/components/_footer.sass | |
.site-footer | |
.brand-logo | |
.copyright | |
.footer-links | |
// sass/components/_forms.sass | |
%text-input-style | |
form .control-group | |
form .form-group | |
form .control-group | |
form .form-group | |
form .required-field | |
form .grouped-select-wrapper | |
form .help-text | |
form .form-group.error .help-text | |
form.sms-password-reset-form | |
.form-actions | |
.form-errors .error | |
.errors.help-block | |
.form-errors | |
.checkbox | |
.terms | |
.datepicker-input-wrapper | |
.flexible-layout | |
.item-view ul.styled-results | |
.form-section | |
.form-main | |
.form-two-col | |
.required-notice | |
.dashboard-page | |
.item-view | |
.list-view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment