|
| 1 | +import { getGlobalVariable } from '../../../utils/env'; |
| 2 | +import { appendToFile, expectFileToMatch, writeMultipleFiles } from '../../../utils/fs'; |
| 3 | +import { installPackage } from '../../../utils/packages'; |
| 4 | +import { ng } from '../../../utils/process'; |
| 5 | +import { updateJsonFile } from '../../../utils/project'; |
| 6 | + |
| 7 | +const snapshots = require('../../../ng-snapshot/package.json'); |
| 8 | + |
| 9 | +export default async function () { |
| 10 | + await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>'); |
| 11 | + await ng('generate', 'app-shell', '--project', 'test-project'); |
| 12 | + |
| 13 | + const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots']; |
| 14 | + if (isSnapshotBuild) { |
| 15 | + const packagesToInstall: string[] = []; |
| 16 | + await updateJsonFile('package.json', (packageJson) => { |
| 17 | + const dependencies = packageJson['dependencies']; |
| 18 | + // Iterate over all of the packages to update them to the snapshot version. |
| 19 | + for (const [name, version] of Object.entries( |
| 20 | + snapshots.dependencies as { [p: string]: string }, |
| 21 | + )) { |
| 22 | + if (name in dependencies && dependencies[name] !== version) { |
| 23 | + packagesToInstall.push(version); |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + for (const pkg of packagesToInstall) { |
| 29 | + await installPackage(pkg); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // TODO(alanagius): update the below once we have a standalone schematic. |
| 34 | + await writeMultipleFiles({ |
| 35 | + 'src/app/app.component.ts': ` |
| 36 | + import { Component } from '@angular/core'; |
| 37 | + import { RouterOutlet } from '@angular/router'; |
| 38 | +
|
| 39 | + @Component({ |
| 40 | + selector: 'app-root', |
| 41 | + standalone: true, |
| 42 | + template: '<router-outlet></router-outlet>', |
| 43 | + imports: [RouterOutlet], |
| 44 | + }) |
| 45 | + export class AppComponent {} |
| 46 | + `, |
| 47 | + 'src/main.ts': ` |
| 48 | + import { bootstrapApplication } from '@angular/platform-browser'; |
| 49 | + import { provideRouter } from '@angular/router'; |
| 50 | +
|
| 51 | + import { AppComponent } from './app/app.component'; |
| 52 | +
|
| 53 | + bootstrapApplication(AppComponent, { |
| 54 | + providers: [ |
| 55 | + provideRouter([]), |
| 56 | + ], |
| 57 | + }); |
| 58 | + `, |
| 59 | + 'src/main.server.ts': ` |
| 60 | + import { importProvidersFrom } from '@angular/core'; |
| 61 | + import { BrowserModule, bootstrapApplication } from '@angular/platform-browser'; |
| 62 | + import { ServerModule } from '@angular/platform-server'; |
| 63 | +
|
| 64 | + import { provideRouter } from '@angular/router'; |
| 65 | +
|
| 66 | + import { AppShellComponent } from './app/app-shell/app-shell.component'; |
| 67 | + import { AppComponent } from './app/app.component'; |
| 68 | +
|
| 69 | + export default () => bootstrapApplication(AppComponent, { |
| 70 | + providers: [ |
| 71 | + importProvidersFrom(BrowserModule.withServerTransition({ appId: 'app' })), |
| 72 | + importProvidersFrom(ServerModule), |
| 73 | + provideRouter([{ path: 'shell', component: AppShellComponent }]), |
| 74 | + ], |
| 75 | + }); |
| 76 | + `, |
| 77 | + }); |
| 78 | + |
| 79 | + await ng('run', 'test-project:app-shell:development'); |
| 80 | + await expectFileToMatch('dist/test-project/browser/index.html', /app-shell works!/); |
| 81 | + |
| 82 | + await ng('run', 'test-project:app-shell'); |
| 83 | + await expectFileToMatch('dist/test-project/browser/index.html', /app-shell works!/); |
| 84 | +} |
0 commit comments