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?