# Conflicts: # .nvmrc # aio/content/cli/index.md # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/app-shell.md # aio/content/guide/architecture-components.md # aio/content/guide/architecture-modules.md # aio/content/guide/architecture-services.md # aio/content/guide/architecture.md # aio/content/guide/attribute-directives.md # aio/content/guide/bazel.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/build.md # aio/content/guide/cheatsheet.md # aio/content/guide/cli-builder.md # aio/content/guide/comparing-observables.md # aio/content/guide/complex-animation-sequences.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/creating-libraries.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection-navtree.md # aio/content/guide/dependency-injection-providers.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/deprecations.md # aio/content/guide/displaying-data.md # aio/content/guide/dynamic-component-loader.md # aio/content/guide/dynamic-form.md # aio/content/guide/elements.md # aio/content/guide/entry-components.md # aio/content/guide/feature-modules.md # aio/content/guide/file-structure.md # aio/content/guide/form-validation.md # aio/content/guide/forms-overview.md # aio/content/guide/forms.md # aio/content/guide/frequent-ngmodules.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ivy.md # aio/content/guide/language-service.md # aio/content/guide/lazy-loading-ngmodules.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/module-types.md # aio/content/guide/ngmodule-api.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule-vs-jsmodule.md # aio/content/guide/ngmodules.md # aio/content/guide/npm-packages.md # aio/content/guide/observables-in-angular.md # aio/content/guide/observables.md # aio/content/guide/pipes.md # aio/content/guide/providers.md # aio/content/guide/reactive-forms.md # aio/content/guide/releases.md # aio/content/guide/reusable-animations.md # aio/content/guide/route-animations.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/schematics-authoring.md # aio/content/guide/schematics-for-libraries.md # aio/content/guide/schematics.md # aio/content/guide/security.md # aio/content/guide/service-worker-communications.md # aio/content/guide/service-worker-config.md # aio/content/guide/service-worker-devops.md # aio/content/guide/service-worker-getting-started.md # aio/content/guide/service-worker-intro.md # aio/content/guide/set-document-title.md # aio/content/guide/setup-local.md # aio/content/guide/sharing-ngmodules.md # aio/content/guide/singleton-services.md # aio/content/guide/static-query-migration.md # aio/content/guide/structural-directives.md # aio/content/guide/styleguide.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/transition-and-triggers.md # aio/content/guide/typescript-configuration.md # aio/content/guide/universal.md # aio/content/guide/updating.md # aio/content/guide/upgrade-setup.md # aio/content/guide/upgrade.md # aio/content/guide/user-input.md # aio/content/guide/using-libraries.md # aio/content/guide/visual-studio-2015.md # aio/content/guide/web-worker.md # aio/content/guide/workspace-config.md # aio/content/marketing/events.html # aio/content/marketing/resources.json # aio/content/navigation.json # aio/content/start/data.md # aio/content/start/deployment.md # aio/content/start/forms.md # aio/content/start/index.md # aio/content/start/routing.md # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt0.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/app/app.component.spec.ts # aio/src/app/custom-elements/api/api-list.component.html # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/mode-banner/mode-banner.component.ts # aio/src/app/layout/nav-item/nav-item.component.html # aio/src/app/shared/toc.service.ts # aio/src/styles/0-base/_typography.scss # aio/src/styles/2-modules/_api-pages.scss # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # packages/common/http/src/headers.ts # packages/common/http/src/interceptor.ts # packages/common/http/src/params.ts # packages/common/http/src/response.ts # packages/common/src/common_module.ts # packages/common/src/directives/ng_for_of.ts # packages/common/src/location/location.ts # packages/common/src/pipes/date_pipe.ts # packages/core/src/change_detection/pipe_transform.ts # packages/core/src/di/injectable.ts # packages/core/src/metadata/di.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render3/component_ref.ts # packages/forms/src/directives/reactive_directives/form_control_name.ts # packages/forms/src/form_builder.ts # packages/forms/src/model.ts # packages/forms/src/validators.ts # packages/router/src/config.ts # packages/router/src/directives/router_outlet.ts # packages/router/src/events.ts # packages/router/src/router.ts # packages/router/src/router_module.ts # packages/router/src/router_state.ts # packages/router/src/shared.ts
305 lines
10 KiB
JavaScript
305 lines
10 KiB
JavaScript
'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").JSDOM;
|
|
var fs = require("fs-extra");
|
|
var globby = require('globby');
|
|
|
|
var regionExtractor = require('../transforms/examples-package/services/region-parser');
|
|
|
|
class StackblitzBuilder {
|
|
constructor(basePath, destPath) {
|
|
this.basePath = basePath;
|
|
this.destPath = destPath;
|
|
|
|
// Extract npm package dependencies
|
|
var packageJson = require(path.join(__dirname, '../examples/shared/boilerplate/cli/package.json'));
|
|
this.examplePackageDependencies = packageJson.dependencies;
|
|
|
|
// Add unit test packages from devDependency for unit test examples
|
|
var devDependencies = packageJson.devDependencies;
|
|
this.examplePackageDependencies['jasmine-core'] = devDependencies['jasmine-core'];
|
|
this.examplePackageDependencies['jasmine-marbles'] = devDependencies['jasmine-marbles'];
|
|
|
|
this.copyrights = {};
|
|
|
|
this._buildCopyrightStrings();
|
|
}
|
|
|
|
build() {
|
|
this._checkForOutdatedConfig();
|
|
|
|
// When testing it sometimes helps to look a just one example directory like so:
|
|
// var stackblitzPaths = path.join(this.basePath, '**/testing/*stackblitz.json');
|
|
var stackblitzPaths = path.join(this.basePath, '**/*stackblitz.json');
|
|
var fileNames = globby.sync(stackblitzPaths, { ignore: ['**/node_modules/**'] });
|
|
fileNames.forEach((configFileName) => {
|
|
try {
|
|
// console.log('***'+configFileName)
|
|
this._buildStackblitzFrom(configFileName);
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
});
|
|
}
|
|
|
|
_addDependencies(postData) {
|
|
postData['dependencies'] = JSON.stringify(this.examplePackageDependencies);
|
|
}
|
|
|
|
_buildCopyrightStrings() {
|
|
var copyright = 'Copyright Google LLC. 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}<!-- \n${copyright}\n-->`;
|
|
}
|
|
|
|
// Build stackblitz from JSON configuration file (e.g., stackblitz.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 stackblitz - defaults to the title in the index.html page.
|
|
// tags: string[] - optional array of stackblitz tags (for searchability)
|
|
// main: string - name of file that will become index.html in the stackblitz - defaults to index.html
|
|
// file: string - name of file to display within the stackblitz (e.g. `"file": "app/app.module.ts"`)
|
|
_buildStackblitzFrom(configFileName) {
|
|
// replace ending 'stackblitz.json' with 'stackblitz.no-link.html' to create output file name;
|
|
var outputFileName = `stackblitz.no-link.html`;
|
|
outputFileName = configFileName.replace(/stackblitz\.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, configFileName);
|
|
this._addDependencies(postData);
|
|
var html = this._createStackblitzHtml(config, postData);
|
|
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;
|
|
}
|
|
}
|
|
|
|
_checkForOutdatedConfig() {
|
|
// Ensure that nobody is trying to use the old config filenames (i.e. `plnkr.json`).
|
|
var plunkerPaths = path.join(this.basePath, '**/*plnkr.json');
|
|
var fileNames = globby.sync(plunkerPaths, { ignore: ['**/node_modules/**'] });
|
|
|
|
if (fileNames.length) {
|
|
const readmePath = path.join(__dirname, 'README.md');
|
|
const errorMessage =
|
|
'One or more examples are still trying to use \'plnkr.json\' files for configuring ' +
|
|
'live examples. This is not supported any more. \'stackblitz.json\' should be used ' +
|
|
'instead.\n' +
|
|
`(Slight modifications may be required. See '${readmePath}' for more info.\n\n` +
|
|
fileNames.map(name => `- ${name}`).join('\n');
|
|
|
|
throw Error(errorMessage);
|
|
}
|
|
}
|
|
|
|
_createBaseStackblitzHtml(config) {
|
|
var file = '';
|
|
|
|
// TODO: Doesn't work properly yet
|
|
if (config.file) {
|
|
file = `?file=${config.file}`;
|
|
}
|
|
var action = `https://run.stackblitz.com/api/angular/v1${file}`;
|
|
var html = `<!DOCTYPE html><html lang="en"><body>
|
|
<form id="mainForm" method="post" action="${action}" target="_self"></form>
|
|
<script>
|
|
var embedded = 'ctl=1';
|
|
var isEmbedded = window.location.search.indexOf(embedded) > -1;
|
|
|
|
if (isEmbedded) {
|
|
var form = document.getElementById('mainForm');
|
|
var action = form.action;
|
|
var actionHasParams = action.indexOf('?') > -1;
|
|
var symbol = actionHasParams ? '&' : '?'
|
|
form.action = form.action + symbol + embedded;
|
|
}
|
|
document.getElementById("mainForm").submit();
|
|
</script>
|
|
</body></html>`;
|
|
return html;
|
|
}
|
|
|
|
_createPostData(config, configFileName) {
|
|
var postData = {};
|
|
|
|
// If `config.main` is specified, ensure that it points to an existing file.
|
|
if (config.main && !this._existsSync(path.join(config.basePath, config.main))) {
|
|
throw Error(`The main file ('${config.main}') specified in '${configFileName}' does not exist.`);
|
|
}
|
|
|
|
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 {
|
|
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);
|
|
|
|
// Is the main a custom index-xxx.html file? Rename it
|
|
if (relativeFileName == config.main) {
|
|
relativeFileName = 'src/index.html';
|
|
}
|
|
|
|
// A custom main.ts file? Rename it
|
|
if (/src\/main[-.]\w+\.ts$/.test(relativeFileName)) {
|
|
relativeFileName = 'src/main.ts'
|
|
}
|
|
|
|
if (relativeFileName == 'index.html') {
|
|
if (config.description == null) {
|
|
// set config.description to title from index.html
|
|
var matches = /<title>(.*)<\/title>/.exec(content);
|
|
if (matches) {
|
|
config.description = matches[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
content = regionExtractor()(content, extn.substr(1)).contents;
|
|
|
|
postData[`files[${relativeFileName}]`] = content;
|
|
});
|
|
|
|
var tags = ['angular', 'example'].concat(config.tags || []);
|
|
tags.forEach(function(tag,ix) {
|
|
postData['tags[' + ix + ']'] = tag;
|
|
});
|
|
|
|
postData.description = "Angular Example - " + config.description;
|
|
|
|
return postData;
|
|
}
|
|
|
|
_createStackblitzHtml(config, postData) {
|
|
var baseHtml = this._createBaseStackblitzHtml(config);
|
|
var doc = new JSDOM(baseHtml).window.document;
|
|
var form = doc.querySelector('form');
|
|
_.forEach(postData, (value, key) => {
|
|
var ele = this._htmlToElement(doc, '<input type="hidden" name="' + key + '">');
|
|
ele.setAttribute('value', value);
|
|
form.appendChild(ele)
|
|
});
|
|
var html = doc.documentElement.outerHTML;
|
|
|
|
return html;
|
|
}
|
|
|
|
_encodeBase64(file) {
|
|
// read binary data
|
|
return fs.readFileSync(file, { encoding: 'base64' });
|
|
}
|
|
|
|
_existsSync(filename) {
|
|
try {
|
|
fs.accessSync(filename);
|
|
return true;
|
|
} catch(ex) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
_htmlToElement(document, html) {
|
|
var div = document.createElement('div');
|
|
div.innerHTML = html;
|
|
return div.firstChild;
|
|
}
|
|
|
|
_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 = configDir; // assumes 'stackblitz.json' is at `/src` level.
|
|
} catch (e) {
|
|
throw new Error(`Stackblitz config - unable to parse json file: ${configFileName}\n${e}`);
|
|
}
|
|
|
|
var defaultIncludes = ['**/*.ts', '**/*.js', '**/*.css', '**/*.html', '**/*.md', '**/*.json', '**/*.png', '**/*.svg'];
|
|
var boilerplateIncludes = ['src/environments/*.*', 'angular.json', 'src/polyfills.ts'];
|
|
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;
|
|
}
|
|
config.files = config.files.concat(boilerplateIncludes);
|
|
|
|
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 = [
|
|
'!**/e2e/**/*.*',
|
|
'!**/tsconfig.json',
|
|
'!**/package.json',
|
|
'!**/example-config.json',
|
|
'!**/tslint.json',
|
|
'!**/.editorconfig',
|
|
'!**/wallaby.js',
|
|
'!**/karma-test-shim.js',
|
|
'!**/karma.conf.js',
|
|
'!**/test.ts',
|
|
'!**/tsconfig.app.json',
|
|
'!**/*stackblitz.*'
|
|
];
|
|
|
|
// exclude all specs if no spec is mentioned in `files[]`
|
|
if (!includeSpec) {
|
|
defaultExcludes.push('!**/*.spec.*','!**/spec.js');
|
|
}
|
|
|
|
gpaths.push(...defaultExcludes);
|
|
|
|
config.fileNames = globby.sync(gpaths, { ignore: ["**/node_modules/**"] });
|
|
|
|
return config;
|
|
}
|
|
}
|
|
|
|
module.exports = StackblitzBuilder;
|