0

I do have a mono repo that has a CLI package (packages/cli) and a web app (apps/web) and I'd like to consume the public API of the CLI within the web app.

The CLI package is bundled with tsup:

export default defineConfig({
  clean: false,
  dts: true,
  entry: ["src/index.ts"],
  format: ["esm"],
  sourcemap: true,
  target: "esnext",
  outDir: "dist",
});

The index.ts is simply calling commander:

(async () => {
  const program = new Command()
    .name("cli")
    .addCommand(info);

  program.parse();
})();

The info command is a simple Commander Command that prints some information:

export function getInfo() {
  console.log("Hello there");
}

export const info = new Command().name("info").action(async () => {
  getInfo();
});

What I'd like to achieve now is to use getInfo within my web app - but how do I export it?

Putting a simple export * from "./commands/info" in my index.ts wouldn't work, since the entire CLI tool is automatically executed as soon as the index.ts of it is called.

I'm thinking of something like import { getInfo } from "@pkg/cli/api", where I'd add a api.ts to my cli that's also exported - but how do I achieve this?

I tried to modify the entry of my tsup to entry: ["src/index.ts", "src/api.ts"], where my api.ts simply exports the getInfo function. But my IDE suggests the getInfo import comes from @pkg/cli/dist/api - which doesn't work due to Package path ./dist/api is not exported from package.

Anyone got an idea?

nehalist
  • 1,434
  • 18
  • 45
  • Do you want it published to npm? Or why can't you just import it from "./commands/info"? – MaximilianMairinger Aug 29 '23 at 09:09
  • The CLI will be published, the web app will be just a website. `./commands/info` doesn't exist in the web app, it's in another project in the mono repo – nehalist Aug 29 '23 at 09:18
  • Can you share your package.json, please. I don't quite understand how you published your CLI tool, if you already have, or how you intend to do so? As of my understanding, CLI expose their executable via the "bin" key in the package.json. So the "main" field could be used for your `api` build. If that's not helpful for some reason, look into the ["exports"](https://medium.com/swlh/npm-new-package-json-exports-field-1a7d1f489ccf) field in package.json. This way, you could probably resolve the problem described in your last paragraph, although I am not 100% sure what the issue is exactly. – MaximilianMairinger Aug 29 '23 at 10:12
  • The `main` field helped to get the imports working - unfortunately I now receive build warnings: "Critical dependency: the request of a dependency is an expression". These seem to originate from the fact that I'm using libs within the CLI that use `require`. – nehalist Aug 29 '23 at 10:30
  • I need more details to resolve this issue. Does it happen when using the bin entry point? Did it work before? How did you export it before? Does this help you https://stackoverflow.com/a/42924147/10226440? – MaximilianMairinger Aug 30 '23 at 11:43

0 Answers0