Skip to content

Instantly share code, notes, and snippets.

@allmarkedup
Created November 8, 2016 09:10
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 allmarkedup/e3004b8b48946f192856d31ce0311a23 to your computer and use it in GitHub Desktop.
Save allmarkedup/e3004b8b48946f192856d31ce0311a23 to your computer and use it in GitHub Desktop.
Fractal CLI command to export component templates with path rewrites
const fs = require('fs');
function exportTemplates(args, done) {
const app = this.fractal;
const items = app.components.flattenDeep().toArray();
const jobs = [];
for (const item of items) {
const exportPath = path.join('./', args.options.output || 'exported', `${item.alias || item.handle}${app.get('components.ext')}`);
const job = item.getContent().then(str => {
return str.replace(/\@([0-9a-zA-Z\-\_]*)/g, function(match, handle){
return `${handle}${app.get('components.ext')}`;
});
}).then(str => {
return fs.writeFileSync(exportPath, str);
});
jobs.push(job);
}
return Promise.all(jobs);
}
fractal.cli.command('export', exportTemplates, {
description: 'Export all component templates',
options: [
['-o, --output <output-dir>', 'The directory to export components into, relative to the CWD.'],
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment