Skip to content

Commit 56804b8

Browse files
CLOUDP-352308 Fix tsc script and share build commands (#3323)
* refactor: share commands across lg and lg-build scripts * refactor: remove unused "direct" flag * refactor: minor changes * docs: changelog
1 parent e58d9a8 commit 56804b8

File tree

8 files changed

+73
-75
lines changed

8 files changed

+73
-75
lines changed

.changeset/legal-ants-stare.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@lg-tools/build': patch
3+
'@lg-tools/cli': patch
4+
---
5+
6+
Refactored lg and lg-build scripts to share build command registration logic, ensuring consistent options and argument handling which fixes an issue where lg-build tsc was not processing args (e.g. --verbose) correctly. Also removed the unused --direct option from the build commands/scripts.

packages/icon/scripts/build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function chunkArray<T>(arr: Array<T>, size: number): Array<Array<T>> {
2828
* Runs the Rollup build command for index and story files.
2929
*/
3030
async function buildIndex({ verbose }: { verbose?: boolean }): Promise<void> {
31-
await buildPackage({ direct: true, verbose });
31+
await buildPackage({ verbose });
3232
}
3333

3434
/**

tools/build/src/cli-commands.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Command } from 'commander';
2+
3+
import { buildPackage } from './rollup/build-package';
4+
import { buildTSDoc } from './tsdoc/build-tsdoc';
5+
import { buildTypescript } from './typescript/build-ts';
6+
7+
export function registerBundleCommand(command: Command) {
8+
command
9+
.description('Builds a package using Rollup')
10+
.option('-v, --verbose', 'Enable verbose logging', false)
11+
.action(buildPackage);
12+
}
13+
14+
export function registerBuildTSCommand(command: Command) {
15+
command
16+
.description("Builds a package's TypeScript definitions")
17+
.argument('[pass-through...]', 'Pass-through options for `tsc`')
18+
.option('-v, --verbose', 'Enable verbose logging', false)
19+
.option(
20+
'-d, --downlevel',
21+
'Downlevel TypeScript packages based on the typesVersions field in package.json',
22+
false,
23+
)
24+
.option(
25+
'-u, --update',
26+
'Update package.json typesVersions and exports fields',
27+
false,
28+
)
29+
.allowUnknownOption(true)
30+
.action(buildTypescript);
31+
}
32+
33+
export function registerBuildDocsCommand(command: Command) {
34+
command
35+
.description('Build TSDoc documentation')
36+
.option('-v, --verbose', 'Enable verbose logging', false)
37+
.action(buildTSDoc);
38+
}

tools/build/src/cli.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
11
import { Command } from 'commander';
22

3-
import { buildPackage } from './rollup/build-package';
4-
import { buildTSDoc } from './tsdoc/build-tsdoc';
5-
import { buildTypescript } from './typescript/build-ts';
3+
import {
4+
registerBuildDocsCommand,
5+
registerBuildTSCommand,
6+
registerBundleCommand,
7+
} from './cli-commands';
68

79
const build = new Command('lg-build');
810
build.description('Build LeafyGreen packages');
911

10-
build
11-
.command('bundle')
12-
.description('Bundle packages with Rollup')
13-
.option('-v, --verbose', 'Enable verbose logging', false)
14-
.option(
15-
'-d, --direct',
16-
'Build package using the lg-build rollup command directly from @lg-tools/build',
17-
true,
18-
)
19-
.action(buildPackage);
20-
21-
build
22-
.command('tsc')
23-
.description('Build TypeScript packages')
24-
.option('-v, --verbose', 'Enable verbose logging', false)
25-
.option(
26-
'-d, --downlevel',
27-
'Downlevel TypeScript packages based on the typesVersions field in package.json',
28-
false,
29-
)
30-
.option(
31-
'-u, --update',
32-
'Update package.json typesVersions and exports fields',
33-
false,
34-
)
35-
.action(buildTypescript);
36-
37-
build
38-
.command('docs')
39-
.description('Build TSDoc documentation')
40-
.option('-v, --verbose', 'Enable verbose logging', false)
41-
.action(buildTSDoc);
12+
registerBundleCommand(build.command('bundle'));
13+
registerBuildTSCommand(build.command('tsc'));
14+
registerBuildDocsCommand(build.command('docs'));
4215

4316
build.parse(process.argv);

tools/build/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
export { buildAll as build } from './buildAll';
2+
export {
3+
registerBuildDocsCommand,
4+
registerBuildTSCommand,
5+
registerBundleCommand,
6+
} from './cli-commands';
27
export { buildPackage } from './rollup/build-package';
38
export { buildTSDoc } from './tsdoc/build-tsdoc';
49
export { parseTSDoc } from './tsdoc/tsdocParser';

tools/build/src/rollup/build-package.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ const loadConfigFile = _loadConfigFile as LoadConfigFile;
1212
import { bundleStats } from 'rollup-plugin-bundle-stats';
1313

1414
interface BuildPackageOptions {
15-
/**
16-
* Pass this option if the function is called directly, and not via Commander.action.
17-
* We use this option in this package's `bin/build-packages.js` command,
18-
* in order to log a warning to consumers to use the `lg` command from @lg-tools/cli
19-
*/
20-
direct?: boolean;
2115
verbose?: boolean;
2216
}
2317

tools/build/src/typescript/build-ts.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function buildTypescript(
2525
passThru?: Array<string>,
2626
options?: BuildTypescriptOptions,
2727
) {
28-
const { verbose } = options ?? { verbose: false };
28+
const { verbose, downlevel } = options ?? {};
2929
const packageDir = process.cwd();
3030
const tsConfigPath = path.join(packageDir, 'tsconfig.json');
3131

@@ -34,10 +34,11 @@ export function buildTypescript(
3434
process.exit(1);
3535
}
3636

37-
verbose &&
37+
if (verbose) {
3838
console.log(chalk.blue.bold(`Building TypeScript (v${ts.version})`));
39-
verbose && console.log(chalk.blue('Building TypeScript'));
40-
verbose && console.log(chalk.gray(tsConfigPath));
39+
console.log(chalk.blue('Building TypeScript'));
40+
console.log(chalk.gray(tsConfigPath));
41+
}
4142

4243
// Any additional options passed in via the CLI
4344
const cliCompilerOptions = parsePassThruOptions(passThru);
@@ -61,7 +62,7 @@ export function buildTypescript(
6162
// Build the project
6263
const exitStatus = builder.build();
6364

64-
if (options?.downlevel) {
65+
if (downlevel) {
6566
runTypescriptDownlevel({
6667
verbose,
6768
});

tools/cli/src/index.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { buildPackage, buildTSDoc, buildTypescript } from '@lg-tools/build';
1+
import {
2+
registerBuildDocsCommand,
3+
registerBuildTSCommand,
4+
registerBundleCommand,
5+
} from '@lg-tools/build';
26
import { migrator } from '@lg-tools/codemods';
37
import { createPackage } from '@lg-tools/create';
48
import { installLeafyGreen } from '@lg-tools/install';
@@ -236,33 +240,10 @@ cli
236240
.action(migrator);
237241

238242
/** Build steps */
239-
cli
240-
.command('build-package')
241-
.description('Builds a package')
242-
.option('-v --verbose', 'Prints additional information to the console', false)
243-
.action(buildPackage);
244-
cli
245-
.command('build-ts')
246-
.description("Builds a package's TypeScript definitions")
247-
.argument('[pass-through...]', 'Pass-through options for `tsc`')
248-
.option('-v --verbose', 'Prints additional information to the console', false)
249-
.option(
250-
'--downlevel',
251-
'Builds all TS downlevel targets based on the typesVersions field in package.json',
252-
false,
253-
)
254-
.option(
255-
'-u, --update',
256-
'Update package.json typesVersions and exports fields',
257-
false,
258-
)
259-
.allowUnknownOption(true)
260-
.action(buildTypescript);
261-
cli
262-
.command('build-tsdoc')
263-
.description("Builds a package's TSDoc file")
264-
.option('-v --verbose', 'Prints additional information to the console', false)
265-
.action(buildTSDoc);
243+
244+
registerBundleCommand(cli.command('build-package'));
245+
registerBuildTSCommand(cli.command('build-ts'));
246+
registerBuildDocsCommand(cli.command('build-tsdoc'));
266247

267248
/** Merge editor settings */
268249
cli

0 commit comments

Comments
 (0)