Jason Jean e06822da7e
chore(repo): update prettier to v2 (#2934)
this is just for the repo, and not the workspace

Co-authored-by: Rares Matei <matei.rar@gmail.com>
2020-04-29 01:09:37 -04:00

90 lines
2.7 KiB
TypeScript

import {
BuilderContext,
BuilderOutput,
createBuilder,
} from '@angular-devkit/architect';
import { JsonObject } from '@angular-devkit/core';
import * as ng from '@angular/compiler-cli';
import { resolve } from 'path';
import { from, Observable, of } from 'rxjs';
import { mapTo, switchMap, tap } from 'rxjs/operators';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
updatePaths,
} from '@nrwl/workspace/src/utils/buildable-libs-utils';
import { createProjectGraph } from '@nrwl/workspace/src/core/project-graph';
export interface BuildAngularLibraryBuilderOptions {
/**
* The file path for the ng-packagr configuration file, relative to the current workspace.
*/
project: string;
/**
* The full path for the TypeScript configuration file, relative to the current workspace.
*/
tsConfig?: string;
/**
* Run build when files change.
*/
watch?: boolean;
}
async function initializeNgPackagr(
options: BuildAngularLibraryBuilderOptions & JsonObject,
context: BuilderContext,
projectDependencies: DependentBuildableProjectNode[]
): Promise<import('ng-packagr').NgPackagr> {
const packager = (await import('ng-packagr')).ngPackagr();
packager.forProject(resolve(context.workspaceRoot, options.project));
if (options.tsConfig) {
// read the tsconfig and modify its path in memory to
// pass it on to ngpackagr
const parsedTSConfig = ng.readConfiguration(options.tsConfig);
updatePaths(projectDependencies, parsedTSConfig.options.paths);
packager.withTsConfig(parsedTSConfig);
}
return packager;
}
export function run(
options: BuildAngularLibraryBuilderOptions & JsonObject,
context: BuilderContext
): Observable<BuilderOutput> {
const projGraph = createProjectGraph();
const { target, dependencies } = calculateProjectDependencies(
projGraph,
context
);
return of(checkDependentProjectsHaveBeenBuilt(context, dependencies)).pipe(
switchMap((result) => {
if (result) {
return from(initializeNgPackagr(options, context, dependencies)).pipe(
switchMap((packager) =>
options.watch ? packager.watch() : packager.build()
),
tap(() => {
if (dependencies.length > 0) {
updateBuildableProjectPackageJsonDependencies(
context,
target,
dependencies
);
}
}),
mapTo({ success: true })
);
} else {
// just pass on the result
return of({ success: false });
}
})
);
}
export default createBuilder<Record<string, string> & any>(run);