All files / builder/library builder.ts

91.3% Statements 21/23
60% Branches 6/10
80% Functions 4/5
91.3% Lines 21/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                  1x         1x 1x 1x 1x 1x         1x       1x 1x 1x     1x 1x   1x         1x 1x       1x   1x   1x         1x       1x                 1x  
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
 
import {
  BuilderContext,
  BuilderOutput,
  createBuilder,
} from '@angular-devkit/architect';
import { normalizeCacheOptions } from '@angular-devkit/build-angular/src/utils/normalize-cache';
import { join, resolve } from 'path';
import { Observable, from, of } from 'rxjs';
import { catchError, mapTo, switchMap } from 'rxjs/operators';
import { ngPackagrFactory } from './ng-packagr-factory';
 
/**
 * @experimental Direct usage of this function is considered experimental.
 */
export function execute(
  options: any,
  context: BuilderContext
): Observable<BuilderOutput> {
  return from(
    (async () => {
      const root = context.workspaceRoot;
      let tsConfig: string | undefined;
 
      Eif (options.tsConfig) {
        tsConfig = resolve(root, options.tsConfig);
      }
      const packager = await ngPackagrFactory(
        resolve(root, options.project),
        tsConfig
      );
 
      const projectName = context.target?.project;
      Iif (!projectName) {
        throw new Error('The builder requires a target.');
      }
 
      const metadata = await context.getProjectMetadata(projectName);
      const { enabled: cacheEnabled, path: cacheDirectory } =
        normalizeCacheOptions(metadata, context.workspaceRoot);
 
      const ngPackagrOptions = {
        cacheEnabled,
        cacheDirectory: join(cacheDirectory, 'ng-packagr'),
      };
 
      return { packager, ngPackagrOptions };
    })()
  ).pipe(
    switchMap(({ packager, ngPackagrOptions }) =>
      options.watch
        ? packager.watch(ngPackagrOptions)
        : packager.build(ngPackagrOptions)
    ),
    mapTo({ success: true, workspaceRoot: context.workspaceRoot }),
    catchError((err) => of({ success: false, error: err.message }))
  );
}
 
export default createBuilder<Record<string, string> & any>(execute);