Skip to content

Commit

Permalink
feat(generate:entrypoints): Support output filename customization (#2…
Browse files Browse the repository at this point in the history
…0593)

Adds new flags to the `generate:entrypoints` command that allow
customization of the filename used for each `ApiLevel`. These flags are
combined with the existing prefix/suffix flags to build the final file
names. For example, consider the following command:

```
flub generate entrypoints --outDir ./api-report --outFilePrefix {@unscopedPackageName}- --outFileAlpha legacy --outFileBeta b
 --outFilePublic p
```

Running that command on the tree package will output the following
files:

```
INFO:   Generating api-report/tree-p.d.ts
INFO:   Generating api-report/tree-b.d.ts
INFO:   Generating api-report/tree-legacy.d.ts
```
  • Loading branch information
tylerbutler authored Apr 10, 2024
1 parent 3410f15 commit 4e94094
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
7 changes: 5 additions & 2 deletions build-tools/packages/build-cli/docs/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,18 @@ Generates type declaration entrypoints for Fluid Framework API levels (/alpha, /

```
USAGE
$ flub generate entrypoints [-v | --quiet] [--mainEntrypoint <value>] [--outDir <value>] [--outFilePrefix <value>]
[--outFileSuffix <value>]
$ flub generate entrypoints [-v | --quiet] [--mainEntrypoint <value>] [--outDir <value>] [--outFileAlpha <value>]
[--outFileBeta <value>] [--outFilePublic <value>] [--outFilePrefix <value>] [--outFileSuffix <value>]
FLAGS
--mainEntrypoint=<value> [default: ./src/index.ts] Main entrypoint file containing all untrimmed exports.
--outDir=<value> [default: ./lib] Directory to emit entrypoint declaration files.
--outFileAlpha=<value> [default: alpha] Base file name for alpha entrypoint declaration files.
--outFileBeta=<value> [default: beta] Base file name for beta entrypoint declaration files.
--outFilePrefix=<value> File name prefix for emitting entrypoint declaration files. Pattern of
'{@unscopedPackageName}' within value will be replaced with the unscoped name of this
package.
--outFilePublic=<value> [default: public] Base file name for public entrypoint declaration files.
--outFileSuffix=<value> [default: .d.ts] File name suffix including extension for emitting entrypoint declaration
files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export default class GenerateEntrypointsCommand extends BaseCommand<
default: "./lib",
exists: true,
}),
outFileAlpha: Flags.string({
description: "Base file name for alpha entrypoint declaration files.",
default: ApiLevel.alpha,
}),
outFileBeta: Flags.string({
description: "Base file name for beta entrypoint declaration files.",
default: ApiLevel.beta,
}),
outFilePublic: Flags.string({
description: "Base file name for public entrypoint declaration files.",
default: ApiLevel.public,
}),
outFilePrefix: Flags.string({
description: `File name prefix for emitting entrypoint declaration files. Pattern of '${unscopedPackageNameString}' within value will be replaced with the unscoped name of this package.`,
default: "",
Expand All @@ -57,11 +69,28 @@ export default class GenerateEntrypointsCommand extends BaseCommand<
};

public async run(): Promise<void> {
const { mainEntrypoint, outFileSuffix } = this.flags;
const { mainEntrypoint, outFileSuffix, outFileAlpha, outFileBeta, outFilePublic } =
this.flags;

return generateEntrypoints(
mainEntrypoint,
{ pathPrefix: await getOutPathPrefix(this.flags), pathSuffix: outFileSuffix },
(level: Exclude<ApiLevel, typeof ApiLevel.internal>): string => {
switch (level) {
case ApiLevel.alpha: {
return outFileAlpha;
}
case ApiLevel.beta: {
return outFileBeta;
}
case ApiLevel.public: {
return outFilePublic;
}
default: {
this.error(`Unexpected ApiLevel value: ${level}`, { exit: 1 });
}
}
},
this.logger,
);
}
Expand Down Expand Up @@ -114,11 +143,13 @@ const generatedHeader: string = `/*!
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
* Generated by "flub generate entrypoints" in @fluidframework/build-tools.
*/
`;

async function generateEntrypoints(
mainEntrypoint: string,
{ pathPrefix, pathSuffix }: { pathPrefix: string; pathSuffix: string },
getApiLevelDisplayName: (level: Exclude<ApiLevel, typeof ApiLevel.internal>) => string,
log: CommandLogger,
): Promise<void> {
/**
Expand Down Expand Up @@ -182,7 +213,7 @@ async function generateEntrypoints(
namedExports[namedExports.length - 1].trailingTrivia = "\n";
}

const outFile = `${pathPrefix}${apiLevel}${pathSuffix}`;
const outFile = `${pathPrefix}${getApiLevelDisplayName(apiLevel)}${pathSuffix}`;
log.info(`\tGenerating ${outFile}`);
const sourceFile = project.createSourceFile(outFile, undefined, {
overwrite: true,
Expand Down

0 comments on commit 4e94094

Please sign in to comment.