utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const architect_1 = require("@angular-devkit/architect");
  11. const node_1 = require("@angular-devkit/architect/node");
  12. const testing_1 = require("@angular-devkit/architect/testing");
  13. const core_1 = require("@angular-devkit/core");
  14. exports.ivyEnabled = process.argv.includes('--ivy');
  15. const devkitRoot = core_1.normalize(global._DevKitRoot); // tslint:disable-line:no-any
  16. exports.workspaceRoot = core_1.join(devkitRoot, `tests/angular_devkit/build_angular/hello-world-app${exports.ivyEnabled ? '-ivy' : ''}/`);
  17. exports.host = new testing_1.TestProjectHost(exports.workspaceRoot);
  18. exports.outputPath = core_1.normalize('dist');
  19. exports.browserTargetSpec = { project: 'app', target: 'build' };
  20. exports.devServerTargetSpec = { project: 'app', target: 'serve' };
  21. exports.extractI18nTargetSpec = { project: 'app', target: 'extract-i18n' };
  22. exports.karmaTargetSpec = { project: 'app', target: 'test' };
  23. exports.tslintTargetSpec = { project: 'app', target: 'lint' };
  24. exports.protractorTargetSpec = { project: 'app-e2e', target: 'e2e' };
  25. async function createArchitect(workspaceRoot) {
  26. const registry = new core_1.schema.CoreSchemaRegistry();
  27. registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
  28. const workspaceSysPath = core_1.getSystemPath(workspaceRoot);
  29. const workspace = await core_1.experimental.workspace.Workspace.fromPath(exports.host, exports.host.root(), registry);
  30. const architectHost = new testing_1.TestingArchitectHost(workspaceSysPath, workspaceSysPath, new node_1.WorkspaceNodeModulesArchitectHost(workspace, workspaceSysPath));
  31. const architect = new architect_1.Architect(architectHost, registry);
  32. return {
  33. workspace,
  34. architectHost,
  35. architect,
  36. };
  37. }
  38. exports.createArchitect = createArchitect;
  39. async function browserBuild(architect, host, target, overrides, scheduleOptions) {
  40. const run = await architect.scheduleTarget(target, overrides, scheduleOptions);
  41. const output = (await run.result);
  42. expect(output.success).toBe(true);
  43. expect(output.outputPath).not.toBeUndefined();
  44. const outputPath = core_1.normalize(output.outputPath);
  45. const fileNames = await host.list(outputPath).toPromise();
  46. const files = fileNames.reduce((acc, path) => {
  47. let cache = null;
  48. Object.defineProperty(acc, path, {
  49. enumerable: true,
  50. get() {
  51. if (cache) {
  52. return cache;
  53. }
  54. if (!fileNames.includes(path)) {
  55. return Promise.reject('No file named ' + path);
  56. }
  57. cache = host
  58. .read(core_1.join(outputPath, path))
  59. .toPromise()
  60. .then(content => core_1.virtualFs.fileBufferToString(content));
  61. return cache;
  62. },
  63. });
  64. return acc;
  65. }, {});
  66. await run.stop();
  67. return {
  68. output,
  69. files,
  70. };
  71. }
  72. exports.browserBuild = browserBuild;
  73. exports.lazyModuleFiles = {
  74. 'src/app/lazy/lazy-routing.module.ts': `
  75. import { NgModule } from '@angular/core';
  76. import { Routes, RouterModule } from '@angular/router';
  77. const routes: Routes = [];
  78. @NgModule({
  79. imports: [RouterModule.forChild(routes)],
  80. exports: [RouterModule]
  81. })
  82. export class LazyRoutingModule { }
  83. `,
  84. 'src/app/lazy/lazy.module.ts': `
  85. import { NgModule } from '@angular/core';
  86. import { CommonModule } from '@angular/common';
  87. import { LazyRoutingModule } from './lazy-routing.module';
  88. @NgModule({
  89. imports: [
  90. CommonModule,
  91. LazyRoutingModule
  92. ],
  93. declarations: []
  94. })
  95. export class LazyModule { }
  96. `,
  97. };
  98. exports.lazyModuleStringImport = {
  99. 'src/app/app.module.ts': `
  100. import { BrowserModule } from '@angular/platform-browser';
  101. import { NgModule } from '@angular/core';
  102. import { AppComponent } from './app.component';
  103. import { RouterModule } from '@angular/router';
  104. @NgModule({
  105. declarations: [
  106. AppComponent
  107. ],
  108. imports: [
  109. BrowserModule,
  110. RouterModule.forRoot([
  111. { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' }
  112. ])
  113. ],
  114. providers: [],
  115. bootstrap: [AppComponent]
  116. })
  117. export class AppModule { }
  118. `,
  119. };
  120. exports.lazyModuleFnImport = {
  121. 'src/app/app.module.ts': exports.lazyModuleStringImport['src/app/app.module.ts'].replace(`loadChildren: './lazy/lazy.module#LazyModule'`, `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`),
  122. };