UNPKG

@tririga/tri-bundler

Version:

A tool for bundling Polymer 3 TRIRIGA UX views.

48 lines (38 loc) 1.28 kB
"use strict"; const fs = require("fs"); const utils = require("../utils.js"); function ComponentExporter(componentPath, outputDir, tririga) { this.componentPath = componentPath; var lastPartIndex = componentPath.lastIndexOf("/") + 1; this.componentContext = componentPath.slice(0, lastPartIndex); this.outputDir = outputDir; this.tririga = tririga; } ComponentExporter.prototype.export = function(callback) { this.tririga.getComponentResource(this.componentPath, (error, componentSource) => { if (error) { console.warn("[warn] Error retrieving component source [componentPath=" + this.componentPath + "]. "); callback([]); return; } // Make sure all directories are created. var dirs = this.componentPath.split("/"); dirs[0] = this.outputDir + dirs[0]; for (var i = 1; i < dirs.length; i++) { dirs[i] = dirs[i - 1] + "/" + dirs[i]; } for (var i = 0; i < dirs.length - 1; i++) { if (!fs.existsSync(dirs[i])) { fs.mkdirSync(dirs[i]); } } // Write the component fs.writeFileSync(this.outputDir + this.componentPath, componentSource); var componentPaths = utils.getComponentUrls(componentSource, this.componentContext); if (callback) { callback(componentPaths); } }); }; module.exports = ComponentExporter;