diff --git a/tools/plunker-builder/plunkerBuilder.js b/tools/plunker-builder/plunkerBuilder.js deleted file mode 100644 index 6e091ba41a..0000000000 --- a/tools/plunker-builder/plunkerBuilder.js +++ /dev/null @@ -1,311 +0,0 @@ -// 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"); -var globby = require('globby'); -var mkdirp = require('mkdirp'); - -var indexHtmlTranslator = require('./indexHtmlTranslator'); -var regionExtractor = require('../doc-shredder/regionExtractor'); -var COPYRIGHT, COPYRIGHT_JS_CSS, COPYRIGHT_HTML; -var README; // content of plunker.README.md for plunkers -var SYSTEMJS_CONFIG; // content of systemjs.config.js for plunkers that use systemjs -var TSCONFIG; // content of tsconfig.json for plunkers that use systemjs - -module.exports = { - buildPlunkers: buildPlunkers -}; - -buildCopyrightStrings(); - -function 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'; - COPYRIGHT_JS_CSS = pad + '/*\n' + COPYRIGHT + '\n*/'; - COPYRIGHT_HTML = pad + '' -} - -function buildPlunkers(basePath, destPath, options) { - getPlunkerFiles(basePath, options); - var errFn = options.errFn || function(e) { console.log(e); }; - var plunkerPaths = path.join(basePath, '**/*plnkr.json'); - var fileNames = globby.sync(plunkerPaths, - { ignore: ['**/node_modules/**', '**/_boilerplate/**'] }); - fileNames.forEach(function(configFileName) { - try { - buildPlunkerFrom(configFileName, basePath, destPath); - } catch (e) { - errFn(e); - } - }); -} - -// config has -// files: [] - optional array of globs - defaults to all js, ts, html, json, css and md files (with certain files removed) -// description: optional string - description of this plunker - defaults to the title in the index.html page. -// tags: [] - optional array of strings -// main: string - filename of what will become index.html in the plunker - defaults to index.html -function buildPlunkerFrom(configFileName, basePath, destPath) { - // replace ending 'plnkr.json' with 'plnkr.no-link.html' to create output file name; - var outputFileName = configFileName.substr(0, configFileName.length - 'plnkr.json'.length) + 'plnkr.no-link.html'; - var altFileName; - if (destPath && destPath.length > 0) { - var partPath = path.dirname(path.relative(basePath, outputFileName)); - var altFileName = path.join(destPath, partPath, path.basename(outputFileName)).replace('.no-link.', '.'); - } - try { - var config = initConfigAndCollectFileNames(configFileName); - var postData = createPostData(config); - addPlunkerFiles(config, postData); - var html = createPlunkerHtml(postData); - fs.writeFileSync(outputFileName, html, 'utf-8'); - if (altFileName) { - var altDirName = path.dirname(altFileName); - if (!fs.existsSync(altDirName)) { - mkdirp.sync(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 (existsSync(outputFileName)) { - fs.unlinkSync(outputFileName); - } - if (altFileName && existsSync(altFileName)) { - fs.unlinkSync(altFileName); - } - throw e; - } -} - -function addPlunkerFiles(config, postData) { - addReadme(config, postData); - if (config.basePath.indexOf('/ts') > -1) { - // uses systemjs.config.js so add plunker version - postData['files[systemjs.config.js]'] = SYSTEMJS_CONFIG; - postData['files[tsconfig.json]'] = TSCONFIG; - } -} - -function addReadme(config, postData) { - var existingFiles = config.fileNames.map(function(file) { - return file.substr(file.lastIndexOf('/') + 1); - }); - - if (existingFiles.indexOf('README.md') === -1) { - var plunkerReadme = README + config.description; - postData['files[README.md]'] = plunkerReadme; - } -} - -function getPlunkerFiles(basePath, options) { - // Assume plunker version is sibling of node_modules version - README = fs.readFileSync(basePath + '/plunker.README.md', 'utf-8'); - var systemJsConfigPath = '/systemjs.config.plunker.js'; - if (options.build) { - systemJsConfigPath = '/systemjs.config.plunker.build.js'; - } - SYSTEMJS_CONFIG = fs.readFileSync(basePath + systemJsConfigPath, 'utf-8'); - SYSTEMJS_CONFIG += COPYRIGHT_JS_CSS; - TSCONFIG = fs.readFileSync(basePath + '/tsconfig.json', 'utf-8'); -} - -function initConfigAndCollectFileNames(configFileName) { - var configDir = path.dirname(configFileName); - var configSrc = fs.readFileSync(configFileName, 'utf-8'); - try { - var config = (configSrc && configSrc.trim().length) ? JSON.parse(configSrc) : {}; - config.basePath = config.basePath ? path.resolve(configDir, config.basePath) : configDir; - } catch (e) { - throw new Error("Plunker config - unable to parse json file: " + configFileName + '\n ' + e); - } - - var defaultIncludes = ['**/*.ts', '**/*.js', '**/*.css', '**/*.html', '**/*.md', '**/*.json', '**/*.png']; - if (config.files) { - if (config.files.length > 0) { - if (config.files[0].substr(0, 1) == '!') { - config.files = defaultIncludes.concat(config.files); - } - } - } else { - config.files = defaultIncludes; - } - var includeSpec = false; - var gpaths = config.files.map(function(fileName) { - fileName = fileName.trim(); - if (fileName.substr(0,1) == '!') { - return "!" + path.join(config.basePath, fileName.substr(1)); - } else { - includeSpec = includeSpec || /.*\.spec.(ts|js)$/.test(fileName); - return path.join(config.basePath, fileName); - } - }); - - // var defaultExcludes = [ '!**/node_modules/**','!**/typings/**','!**/tsconfig.json', '!**/*plnkr.json', '!**/*plnkr.html', '!**/*plnkr.no-link.html' ]; - var defaultExcludes = [ - '!**/tsconfig.json', - '!**/*plnkr.*', - '!**/package.json', - '!**/example-config.json', - '!**/tslint.json', - '!**/systemjs.config.js', - '!**/wallaby.js', - '!**/karma-test-shim.js', - '!**/karma.conf.js' - ]; - // exclude all specs if no spec is mentioned in `files[]` - if (!includeSpec) { - defaultExcludes = defaultExcludes.concat(['!**/*.spec.*','!**/spec.js']); - } - Array.prototype.push.apply(gpaths, defaultExcludes); - - config.fileNames = globby.sync(gpaths, { ignore: ["**/node_modules/**"] }); - - return config; -} - -function createPostData(config) { - var postData = {}; - config.fileNames.forEach(function(fileName) { - var content; - var extn = path.extname(fileName); - if (extn == '.png') { - content = encodeBase64(fileName); - fileName = fileName.substr(0, fileName.length - 4) + '.base64.png' - } else { - content = fs.readFileSync(fileName, 'utf-8'); - } - - if (extn == '.js' || extn == '.ts' || extn == '.css') { - content = content + COPYRIGHT_JS_CSS; - } else if (extn == '.html') { - content = content + COPYRIGHT_HTML; - } - // var escapedValue = escapeHtml(content); - - var relativeFileName = path.relative(config.basePath, fileName); - - if (relativeFileName == config.main) { - relativeFileName = 'index.html'; - } - - if (relativeFileName == 'index.html') { - content = indexHtmlTranslator.translate(content); - if (config.description == null) { - // set config.description to title from index.html - var matches = /