Skip to content

Instantly share code, notes, and snippets.

@domjtalbot
Created December 15, 2016 16:13
Show Gist options
  • Save domjtalbot/b5a90051271793095f2f106ef91997ab to your computer and use it in GitHub Desktop.
Save domjtalbot/b5a90051271793095f2f106ef91997ab to your computer and use it in GitHub Desktop.
Convert scss comments to json using sassdoc
import { magenta, yellow, red } from 'ansicolors';
import { writeFile } from 'fs';
import glob from 'glob';
import Ora from 'ora';
import { resolve, basename } from 'path';
import { parse } from 'sassdoc';
import '../../Scripts/env.es6';
import { Debug, Error } from '../../Scripts/debug.es6';
const namespace = 'scss2json';
const debug = new Debug(namespace);
const error = new Error(namespace);
const scssCommentsInput = process.env.SCSSCOMMENTSINPUT;
const jsonOutputPath = process.env.SCSSCOMMENTSOUTPUTPATH;
const jsonOutputName = basename(jsonOutputPath);
const jsonOutputDir = jsonOutputPath.replace(jsonOutputName, '');
const jsonOutput = resolve(__dirname, `../../${jsonOutputPath}`);
debug(yellow(`Scss comments being compiled to ${jsonOutputName}`));
debug(yellow(scssCommentsInput));
const parseScss = async (file) => {
const parseScssSpinner = new Ora('Parsing scss files');
parseScssSpinner.start();
const output = await parse(file, {
verbose: true,
});
parseScssSpinner.succeed();
return output;
};
const writeFileAsync = (outputPath, outputContent) => (
new Promise((res, reject) => {
const writeFileSpinner = new Ora('Writing comments JSON to file');
writeFileSpinner.start();
writeFile(outputPath, JSON.stringify(outputContent), (writeError) => {
if (writeError) {
writeFileSpinner.fail();
reject(new Error(error(red(`writeError: ${writeError}`))));
} else {
writeFileSpinner.succeed();
res(true);
}
});
})
);
(async function js2json() {
const scssCommentsJSON = await parseScss(scssCommentsInput);
if (scssCommentsJSON !== null) {
await writeFileAsync(jsonOutput, scssCommentsJSON);
debug(green('Done.'));
} else {
debug(red('No comments found.'));
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment