Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/legal-ants-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lg-tools/build': patch
'@lg-tools/cli': patch
---

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.
2 changes: 1 addition & 1 deletion packages/icon/scripts/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function chunkArray<T>(arr: Array<T>, size: number): Array<Array<T>> {
* Runs the Rollup build command for index and story files.
*/
async function buildIndex({ verbose }: { verbose?: boolean }): Promise<void> {
await buildPackage({ direct: true, verbose });
await buildPackage({ verbose });
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tools/build/src/cli-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Command } from 'commander';

import { buildPackage } from './rollup/build-package';
import { buildTSDoc } from './tsdoc/build-tsdoc';
import { buildTypescript } from './typescript/build-ts';

export function registerBundleCommand(command: Command) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍 Been wanting to do something like this for a while. It always bothered me that the cli definitions weren't collocated in the same package as the actual logic

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Adam for the quick feedback, glad that the change resonated with you too! 😁💐

command
.description('Builds a package using Rollup')
.option('-v, --verbose', 'Enable verbose logging', false)
.action(buildPackage);
}

export function registerBuildTSCommand(command: Command) {
command
.description("Builds a package's TypeScript definitions")
.argument('[pass-through...]', 'Pass-through options for `tsc`')
.option('-v, --verbose', 'Enable verbose logging', false)
.option(
'-d, --downlevel',
'Downlevel TypeScript packages based on the typesVersions field in package.json',
false,
)
.option(
'-u, --update',
'Update package.json typesVersions and exports fields',
false,
)
.allowUnknownOption(true)
.action(buildTypescript);
}

export function registerBuildDocsCommand(command: Command) {
command
.description('Build TSDoc documentation')
.option('-v, --verbose', 'Enable verbose logging', false)
.action(buildTSDoc);
}
43 changes: 8 additions & 35 deletions tools/build/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
import { Command } from 'commander';

import { buildPackage } from './rollup/build-package';
import { buildTSDoc } from './tsdoc/build-tsdoc';
import { buildTypescript } from './typescript/build-ts';
import {
registerBuildDocsCommand,
registerBuildTSCommand,
registerBundleCommand,
} from './cli-commands';

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

build
.command('bundle')
.description('Bundle packages with Rollup')
.option('-v, --verbose', 'Enable verbose logging', false)
.option(
'-d, --direct',
'Build package using the lg-build rollup command directly from @lg-tools/build',
true,
)
.action(buildPackage);

build
.command('tsc')
.description('Build TypeScript packages')
.option('-v, --verbose', 'Enable verbose logging', false)
.option(
'-d, --downlevel',
'Downlevel TypeScript packages based on the typesVersions field in package.json',
false,
)
.option(
'-u, --update',
'Update package.json typesVersions and exports fields',
false,
)
.action(buildTypescript);

build
.command('docs')
.description('Build TSDoc documentation')
.option('-v, --verbose', 'Enable verbose logging', false)
.action(buildTSDoc);
registerBundleCommand(build.command('bundle'));
registerBuildTSCommand(build.command('tsc'));
registerBuildDocsCommand(build.command('docs'));

build.parse(process.argv);
5 changes: 5 additions & 0 deletions tools/build/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export { buildAll as build } from './buildAll';
export {
registerBuildDocsCommand,
registerBuildTSCommand,
registerBundleCommand,
} from './cli-commands';
export { buildPackage } from './rollup/build-package';
export { buildTSDoc } from './tsdoc/build-tsdoc';
export { parseTSDoc } from './tsdoc/tsdocParser';
Expand Down
6 changes: 0 additions & 6 deletions tools/build/src/rollup/build-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ const loadConfigFile = _loadConfigFile as LoadConfigFile;
import { bundleStats } from 'rollup-plugin-bundle-stats';

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

Expand Down
11 changes: 6 additions & 5 deletions tools/build/src/typescript/build-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function buildTypescript(
passThru?: Array<string>,
options?: BuildTypescriptOptions,
) {
const { verbose } = options ?? { verbose: false };
const { verbose, downlevel } = options ?? {};
const packageDir = process.cwd();
const tsConfigPath = path.join(packageDir, 'tsconfig.json');

Expand All @@ -34,10 +34,11 @@ export function buildTypescript(
process.exit(1);
}

verbose &&
if (verbose) {
console.log(chalk.blue.bold(`Building TypeScript (v${ts.version})`));
verbose && console.log(chalk.blue('Building TypeScript'));
verbose && console.log(chalk.gray(tsConfigPath));
console.log(chalk.blue('Building TypeScript'));
console.log(chalk.gray(tsConfigPath));
}

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

if (options?.downlevel) {
if (downlevel) {
runTypescriptDownlevel({
verbose,
});
Expand Down
37 changes: 9 additions & 28 deletions tools/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { buildPackage, buildTSDoc, buildTypescript } from '@lg-tools/build';
import {
registerBuildDocsCommand,
registerBuildTSCommand,
registerBundleCommand,
} from '@lg-tools/build';
import { migrator } from '@lg-tools/codemods';
import { createPackage } from '@lg-tools/create';
import { installLeafyGreen } from '@lg-tools/install';
Expand Down Expand Up @@ -224,33 +228,10 @@ cli
.action(migrator);

/** Build steps */
cli
.command('build-package')
.description('Builds a package')
.option('-v --verbose', 'Prints additional information to the console', false)
.action(buildPackage);
cli
.command('build-ts')
.description("Builds a package's TypeScript definitions")
.argument('[pass-through...]', 'Pass-through options for `tsc`')
.option('-v --verbose', 'Prints additional information to the console', false)
.option(
'--downlevel',
'Builds all TS downlevel targets based on the typesVersions field in package.json',
false,
)
.option(
'-u, --update',
'Update package.json typesVersions and exports fields',
false,
)
.allowUnknownOption(true)
.action(buildTypescript);
cli
.command('build-tsdoc')
.description("Builds a package's TSDoc file")
.option('-v --verbose', 'Prints additional information to the console', false)
.action(buildTSDoc);

registerBundleCommand(cli.command('build-package'));
registerBuildTSCommand(cli.command('build-ts'));
registerBuildDocsCommand(cli.command('build-tsdoc'));

/** Merge editor settings */
cli
Expand Down
Loading