Forráskód Böngészése

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 <https://github.com/codama-idl/codama/pull/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.
Loris Leiva 3 hónapja
szülő
commit
14e1614e23

+ 6 - 0
.changeset/lazy-bobcats-melt.md

@@ -0,0 +1,6 @@
+---
+'codama': patch
+'@codama/cli': patch
+---
+
+Include CLI in main Codama library

+ 4 - 2
packages/cli/README.md

@@ -9,12 +9,14 @@
 
 This package provides a CLI for the Codama library that can be used to run scripts on Codama IDLs.
 
+Note that, whilst the CLI code is located in the `@codama/cli` package, the CLI binary is also included in the main `codama` library.
+
 ## Getting started
 
-To get started with Codama, simply install `@codama/cli` which provides the `codama` binary. Then, run the `init` command like so:
+To get started with Codama, simply install `codama` to your project and run the `init` command like so:
 
 ```sh
-pnpm install @codama/cli
+pnpm install codama
 pnpm codama init
 ```
 

+ 2 - 14
packages/cli/src/cli/index.ts

@@ -1,19 +1,7 @@
-import pico from 'picocolors';
-
-import { createProgram } from '../program';
-import { logDebug, logError } from '../utils';
+import { createProgram, runProgram } from '../program';
 
 const program = createProgram();
 
 export async function run(argv: readonly string[]) {
-    try {
-        await program.parseAsync(argv);
-    } catch (err) {
-        const error = err as { message: string; stack?: string; items?: string[] };
-        if (program.opts().debug) {
-            logDebug(`${error.stack}`);
-        }
-        logError(pico.bold(error.message), error.items ?? []);
-        process.exitCode = 1;
-    }
+    await runProgram(program, argv);
 }

+ 0 - 1
packages/cli/src/index.ts

@@ -1,2 +1 @@
 export * from './program';
-export * from './utils/logs';

+ 19 - 3
packages/cli/src/program.ts

@@ -1,13 +1,29 @@
-import { Command, createCommand } from 'commander';
+import { Command, createCommand, ParseOptions } from 'commander';
+import pico from 'picocolors';
 
 import { setInitCommand, setRunCommand } from './commands';
 import { setProgramOptions } from './programOptions';
+import { logDebug, logError } from './utils';
 
 export async function codama(args: string[], opts?: { suppressOutput?: boolean }): Promise<void> {
-    await createProgram({
+    const program = createProgram({
         exitOverride: true,
         suppressOutput: opts?.suppressOutput,
-    }).parseAsync(args, { from: 'user' });
+    });
+    await runProgram(program, args, { from: 'user' });
+}
+
+export async function runProgram(program: Command, argv: readonly string[], parseOptions?: ParseOptions) {
+    try {
+        await program.parseAsync(argv, parseOptions);
+    } catch (err) {
+        const error = err as { message: string; stack?: string; items?: string[] };
+        if (program.opts().debug) {
+            logDebug(`${error.stack}`);
+        }
+        logError(pico.bold(error.message), error.items ?? []);
+        process.exitCode = 1;
+    }
 }
 
 export function createProgram(internalOptions?: { exitOverride?: boolean; suppressOutput?: boolean }): Command {

+ 5 - 0
packages/library/bin/cli.cjs

@@ -0,0 +1,5 @@
+#!/usr/bin/env -S node
+
+const run = require('../dist/cli.cjs').run;
+
+run(process.argv);

+ 6 - 1
packages/library/package.json

@@ -26,8 +26,11 @@
     "react-native": "./dist/index.react-native.mjs",
     "types": "./dist/types/index.d.ts",
     "type": "commonjs",
+    "bin": "./bin/cli.cjs",
     "files": [
+        "./bin",
         "./dist/types",
+        "./dist/cli.*",
         "./dist/index.*"
     ],
     "sideEffects": false,
@@ -39,8 +42,9 @@
         "code generation"
     ],
     "scripts": {
-        "build": "rimraf dist && pnpm build:src && pnpm build:types",
+        "build": "rimraf dist && pnpm build:src && pnpm build:cli && pnpm build:types",
         "build:src": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs library",
+        "build:cli": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs cli",
         "build:types": "zx ../../node_modules/@codama/internals/scripts/build-types.mjs",
         "dev": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node --watch",
         "lint": "zx ../../node_modules/@codama/internals/scripts/lint.mjs",
@@ -53,6 +57,7 @@
         "test:types": "zx ../../node_modules/@codama/internals/scripts/test-types.mjs"
     },
     "dependencies": {
+        "@codama/cli": "workspace:*",
         "@codama/errors": "workspace:*",
         "@codama/nodes": "workspace:*",
         "@codama/validators": "workspace:*",

+ 7 - 0
packages/library/src/cli/index.ts

@@ -0,0 +1,7 @@
+import { createProgram, runProgram } from '@codama/cli';
+
+const program = createProgram();
+
+export async function run(argv: readonly string[]) {
+    await runProgram(program, argv);
+}

+ 3 - 0
pnpm-lock.yaml

@@ -160,6 +160,9 @@ importers:
 
   packages/library:
     dependencies:
+      '@codama/cli':
+        specifier: workspace:*
+        version: link:../cli
       '@codama/errors':
         specifier: workspace:*
         version: link:../errors