'use strict'; // Canonical path provides a consistent path (i.e. always forward slashes) across different OSes var path = require('canonical-path'); var Q = require('q'); var _ = require('lodash'); var jsdom = require("jsdom"); var fs = require("fs-extra"); var globby = require('globby'); var fileTranslator = require('./translator/fileTranslator'); var indexHtmlRules = require('./translator/rules/indexHtml'); var systemjsConfigExtrasRules = require('./translator/rules/systemjsConfigExtras'); var regionExtractor = require('../transforms/examples-package/services/region-parser'); class PlunkerBuilder { constructor(basePath, destPath, options) { this.basePath = basePath; this.destPath = destPath; this.options = options; this.boilerplate = path.join(__dirname, '../examples/shared/boilerplate'); this.copyrights = {}; this._buildCopyrightStrings(); } buildPlunkers() { this._getPlunkerFiles(); var errFn = this.options.errFn || function(e) { console.log(e); }; var plunkerPaths = path.join(this.basePath, '**/*plnkr.json'); var fileNames = globby.sync(plunkerPaths, { ignore: ['**/node_modules/**'] }); fileNames.forEach((configFileName) => { try { this._buildPlunkerFrom(configFileName); } catch (e) { errFn(e); } }); } _addPlunkerFiles(postData) { this.options.addField(postData, 'systemjs.config.js', this.systemjsConfig); this.options.addField(postData, 'systemjs-angular-loader.js', this.systemjsModulePlugin); } _buildCopyrightStrings() { var copyright = 'Copyright 2017 Google Inc. All Rights Reserved.\n' + 'Use of this source code is governed by an MIT-style license that\n' + 'can be found in the LICENSE file at http://angular.io/license'; var pad = '\n\n'; this.copyrights.jsCss = `${pad}/*\n${copyright}\n*/`; this.copyrights.html = `${pad}`; } // Build plunker from JSON configuration file (e.g., plnkr.json): // all properties are optional // files: string[] - array of globs - defaults to all js, ts, html, json, css and md files (with certain files removed) // description: string - description of this plunker - defaults to the title in the index.html page. // tags: string[] - optional array of plunker tags (for searchability) // main: string - name of file that will become index.html in the plunker - defaults to index.html // open: string - name of file to display within the plunker as in "open": "app/app.module.ts" _buildPlunkerFrom(configFileName) { // replace ending 'plnkr.json' with 'plnkr.no-link.html' to create output file name; var outputFileName = `${this.options.plunkerFileName}.no-link.html`; outputFileName = configFileName.replace(/plnkr\.json$/, outputFileName); var altFileName; if (this.destPath && this.destPath.length > 0) { var partPath = path.dirname(path.relative(this.basePath, outputFileName)); var altFileName = path.join(this.destPath, partPath, path.basename(outputFileName)).replace('.no-link.', '.'); } try { var config = this._initConfigAndCollectFileNames(configFileName); var postData = this._createPostData(config); this._addPlunkerFiles(postData); var html = this._createPlunkerHtml(config, postData); if (this.options.writeNoLink) { fs.writeFileSync(outputFileName, html, 'utf-8'); } if (altFileName) { var altDirName = path.dirname(altFileName); fs.ensureDirSync(altDirName); fs.writeFileSync(altFileName, html, 'utf-8'); } } catch (e) { // if we fail delete the outputFile if it exists because it is an old one. if (this._existsSync(outputFileName)) { fs.unlinkSync(outputFileName); } if (altFileName && this._existsSync(altFileName)) { fs.unlinkSync(altFileName); } throw e; } } _createBasePlunkerHtml(config, embedded) { var open = ''; if (config.open) { open = embedded ? `&show=${config.open}` : `&open=${config.open}`; } var action = `${this.options.url}${open}`; var html = '
'; html += `' html += ''; html += ''; return html; } _createPostData(config) { var postData = {}; config.fileNames.forEach((fileName) => { var content; var extn = path.extname(fileName); if (extn == '.png') { content = this._encodeBase64(fileName); fileName = fileName.substr(0, fileName.length - 4) + '.base64.png' } else if (-1 < fileName.indexOf('systemjs.config.extras')) { content = this._getSystemjsConfigExtras(config); } else { content = fs.readFileSync(fileName, 'utf-8'); } if (extn == '.js' || extn == '.ts' || extn == '.css') { content = content + this.copyrights.jsCss; } else if (extn == '.html') { content = content + this.copyrights.html; } // var escapedValue = escapeHtml(content); var relativeFileName = path.relative(config.basePath, fileName); if (relativeFileName == config.main) { relativeFileName = 'index.html'; } if (relativeFileName == 'index.html') { content = fileTranslator.translate(content, indexHtmlRules); if (config.description == null) { // set config.description to title from index.html var matches = /