Created
November 8, 2016 09:10
-
-
Save allmarkedup/e3004b8b48946f192856d31ce0311a23 to your computer and use it in GitHub Desktop.
Fractal CLI command to export component templates with path rewrites
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
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