Skip to content

Commit 14e1614

Browse files
authored
Include CLI in main Codama library (#777)
This PR adds the `codama` CLI back to the main library so it is available when consumers install `codama` — instead of having to additionally install `@codama/cli`. This change was reverted before (See <#596>) because `@codama/cli` contained various dependencies (such as renderers) that would then be linked with the main library when versioning package. This caused issues such as: when bumping a renderer package, the node package would need to also be bumped. This limitation is now gone since these additionally dependencies have been removed throughout the PR stack that this PR is part of.
1 parent 8d1888b commit 14e1614

File tree

9 files changed

+52
-21
lines changed

9 files changed

+52
-21
lines changed

.changeset/lazy-bobcats-melt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'codama': patch
3+
'@codama/cli': patch
4+
---
5+
6+
Include CLI in main Codama library

packages/cli/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
This package provides a CLI for the Codama library that can be used to run scripts on Codama IDLs.
1111

12+
Note that, whilst the CLI code is located in the `@codama/cli` package, the CLI binary is also included in the main `codama` library.
13+
1214
## Getting started
1315

14-
To get started with Codama, simply install `@codama/cli` which provides the `codama` binary. Then, run the `init` command like so:
16+
To get started with Codama, simply install `codama` to your project and run the `init` command like so:
1517

1618
```sh
17-
pnpm install @codama/cli
19+
pnpm install codama
1820
pnpm codama init
1921
```
2022

packages/cli/src/cli/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
import pico from 'picocolors';
2-
3-
import { createProgram } from '../program';
4-
import { logDebug, logError } from '../utils';
1+
import { createProgram, runProgram } from '../program';
52

63
const program = createProgram();
74

85
export async function run(argv: readonly string[]) {
9-
try {
10-
await program.parseAsync(argv);
11-
} catch (err) {
12-
const error = err as { message: string; stack?: string; items?: string[] };
13-
if (program.opts().debug) {
14-
logDebug(`${error.stack}`);
15-
}
16-
logError(pico.bold(error.message), error.items ?? []);
17-
process.exitCode = 1;
18-
}
6+
await runProgram(program, argv);
197
}

packages/cli/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './program';
2-
export * from './utils/logs';

packages/cli/src/program.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
import { Command, createCommand } from 'commander';
1+
import { Command, createCommand, ParseOptions } from 'commander';
2+
import pico from 'picocolors';
23

34
import { setInitCommand, setRunCommand } from './commands';
45
import { setProgramOptions } from './programOptions';
6+
import { logDebug, logError } from './utils';
57

68
export async function codama(args: string[], opts?: { suppressOutput?: boolean }): Promise<void> {
7-
await createProgram({
9+
const program = createProgram({
810
exitOverride: true,
911
suppressOutput: opts?.suppressOutput,
10-
}).parseAsync(args, { from: 'user' });
12+
});
13+
await runProgram(program, args, { from: 'user' });
14+
}
15+
16+
export async function runProgram(program: Command, argv: readonly string[], parseOptions?: ParseOptions) {
17+
try {
18+
await program.parseAsync(argv, parseOptions);
19+
} catch (err) {
20+
const error = err as { message: string; stack?: string; items?: string[] };
21+
if (program.opts().debug) {
22+
logDebug(`${error.stack}`);
23+
}
24+
logError(pico.bold(error.message), error.items ?? []);
25+
process.exitCode = 1;
26+
}
1127
}
1228

1329
export function createProgram(internalOptions?: { exitOverride?: boolean; suppressOutput?: boolean }): Command {

packages/library/bin/cli.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env -S node
2+
3+
const run = require('../dist/cli.cjs').run;
4+
5+
run(process.argv);

packages/library/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
"react-native": "./dist/index.react-native.mjs",
2727
"types": "./dist/types/index.d.ts",
2828
"type": "commonjs",
29+
"bin": "./bin/cli.cjs",
2930
"files": [
31+
"./bin",
3032
"./dist/types",
33+
"./dist/cli.*",
3134
"./dist/index.*"
3235
],
3336
"sideEffects": false,
@@ -39,8 +42,9 @@
3942
"code generation"
4043
],
4144
"scripts": {
42-
"build": "rimraf dist && pnpm build:src && pnpm build:types",
45+
"build": "rimraf dist && pnpm build:src && pnpm build:cli && pnpm build:types",
4346
"build:src": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs library",
47+
"build:cli": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs cli",
4448
"build:types": "zx ../../node_modules/@codama/internals/scripts/build-types.mjs",
4549
"dev": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node --watch",
4650
"lint": "zx ../../node_modules/@codama/internals/scripts/lint.mjs",
@@ -53,6 +57,7 @@
5357
"test:types": "zx ../../node_modules/@codama/internals/scripts/test-types.mjs"
5458
},
5559
"dependencies": {
60+
"@codama/cli": "workspace:*",
5661
"@codama/errors": "workspace:*",
5762
"@codama/nodes": "workspace:*",
5863
"@codama/validators": "workspace:*",

packages/library/src/cli/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createProgram, runProgram } from '@codama/cli';
2+
3+
const program = createProgram();
4+
5+
export async function run(argv: readonly string[]) {
6+
await runProgram(program, argv);
7+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)