"Fossies" - the Fresh Open Source Software Archive

Member "angular-cli-15.2.4/tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts" (16 Mar 2023, 2790 Bytes) of package /linux/www/angular-cli-15.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import { join } from 'path';
    2 import { getGlobalVariable } from '../../../utils/env';
    3 import { expectFileToExist, readFile, rimraf } from '../../../utils/fs';
    4 import { installWorkspacePackages } from '../../../utils/packages';
    5 import { ng } from '../../../utils/process';
    6 import { updateJsonFile } from '../../../utils/project';
    7 
    8 const snapshots = require('../../../ng-snapshot/package.json');
    9 
   10 export default async function () {
   11   // forcibly remove in case another test doesn't clean itself up
   12   await rimraf('node_modules/@angular/pwa');
   13   await ng('add', '@angular/pwa', '--skip-confirmation');
   14   await expectFileToExist(join(process.cwd(), 'src/manifest.webmanifest'));
   15 
   16   // Angular PWA doesn't install as a dependency
   17   const { dependencies, devDependencies } = JSON.parse(
   18     await readFile(join(process.cwd(), 'package.json')),
   19   );
   20   const hasPWADep = Object.keys({ ...dependencies, ...devDependencies }).some(
   21     (d) => d === '@angular/pwa',
   22   );
   23   if (hasPWADep) {
   24     throw new Error(`Expected 'package.json' not to contain a dependency on '@angular/pwa'.`);
   25   }
   26 
   27   const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
   28   if (isSnapshotBuild) {
   29     let needInstall = false;
   30     await updateJsonFile('package.json', (packageJson) => {
   31       const dependencies = packageJson['dependencies'];
   32       // Iterate over all of the packages to update them to the snapshot version.
   33       for (const [name, version] of Object.entries(snapshots.dependencies)) {
   34         if (name in dependencies && dependencies[name] !== version) {
   35           dependencies[name] = version;
   36           needInstall = true;
   37         }
   38       }
   39     });
   40 
   41     if (needInstall) {
   42       await installWorkspacePackages();
   43     }
   44   }
   45 
   46   // It should generate a SW configuration file (`ngsw.json`).
   47   const workspaceJson = JSON.parse(await readFile('angular.json'));
   48   const outputPath = workspaceJson.projects['test-project'].architect.build.options.outputPath;
   49   const ngswPath = join(process.cwd(), outputPath, 'ngsw.json');
   50 
   51   await ng('build');
   52   await expectFileToExist(ngswPath);
   53 
   54   // It should correctly generate assetGroups and include at least one URL in each group.
   55   const ngswJson = JSON.parse(await readFile(ngswPath));
   56   // @ts-ignore
   57   const assetGroups: any[] = ngswJson.assetGroups.map(({ name, urls }) => ({
   58     name,
   59     urlCount: urls.length,
   60   }));
   61   const emptyAssetGroups = assetGroups.filter(({ urlCount }) => urlCount === 0);
   62 
   63   if (assetGroups.length === 0) {
   64     throw new Error("Expected 'ngsw.json' to contain at least one asset-group.");
   65   }
   66   if (emptyAssetGroups.length > 0) {
   67     throw new Error(
   68       'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' +
   69         emptyAssetGroups.map(({ name }) => name).join(', '),
   70     );
   71   }
   72 }