angular/aio/content/examples/cli-builder/src/my-builder.ts
Doug Parker e0381a87c9 docs: update CLI builder to be platform agnostic (#42371)
Fixes #35373.

This changes the example from "run an arbitrary process" to "copy a file". This should make it a bit easier to follow, require less background knowledge to understand, and not use any platform-specific commands that won't work for Windows users.

The most glaring issue with this change is that this doc does not explictly specify how to build and run a builder. I've updated some of the files to hint at this a bit more (such as the `"implementation": "./dist/my-builder.js"`), but another pass is required to figure out the best way to compile a builder and how we want to structure this example to best communicate that.

PR Close #42371
2021-06-01 10:13:02 -07:00

44 lines
1.3 KiB
TypeScript

// #docplaster
// #docregion builder, builder-skeleton, handling-output, progress-reporting
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { JsonObject } from '@angular-devkit/core';
// #enddocregion builder-skeleton
import { promises as fs } from 'fs';
// #docregion builder-skeleton
interface Options extends JsonObject {
source: string;
destination: string;
}
export default createBuilder(copyFileBuilder);
async function copyFileBuilder(
options: Options,
context: BuilderContext,
): Promise<BuilderOutput> {
// #enddocregion builder, builder-skeleton, handling-output
// #docregion report-status
context.reportStatus(`Copying ${options.source} to ${options.destination}.`);
// #docregion builder, handling-output
try {
await fs.copyFile(options.source, options.destination);
} catch (err) {
// #enddocregion builder
context.logger.error('Failed to copy file.');
// #docregion builder
return {
success: false,
error: err.message,
};
}
// #enddocregion builder, handling-output
context.reportStatus('Done.');
// #docregion builder, handling-output
return { success: true };
// #enddocregion report-status
// #docregion builder-skeleton
}
// #enddocregion builder, builder-skeleton, handling-output, progress-reporting