Skip to content

Migrate cli package to Typescript #2754

@gerteck

Description

@gerteck

Please confirm that you have searched existing issues in the repo

Yes, I have searched the existing issues

Any related issues?

#1913 #2615

What is the area that this feature belongs to?

Code Quality

Is your feature request related to a problem? Please describe.

Currently, @markbind/core is partially migrated to TypeScript, but markbind-cli (packages/cli) is written in plain JavaScript (CommonJS). This creates an inconsistency in the development experience and limits our ability to leverage modern tooling.

Convert the packages/cli package to be fully written in TypeScript

This task is a key enabler for #2615 This is because if @markbind/core migrates to ESM before the CLI is ready, the CLI (if still CJS) will crash with ERR_REQUIRE_ESM because require() cannot load ESM files. Migrating CLI to TypeScript first puts us in a position where we can upgrade both packages to ESM simultaneously and safely.

If we migrate to typescript, all require and module.export calls can be replaced with import and export. TS allows us to use modern ESM syntax while still targeting CommonJS at runtime, and will emit require() calls in the compiled output when the module target is CommonJS.

Note: Some dynamic require usages (e.g. plugin or config loading) will need to be handled separately, but the majority of imports can be safely converted.

Additionally, TS's interop mechanisms allow ESM output to consume CJS dependencies safely

Benefits

  • Type Safety: Catch bugs at compile time rather than runtime.
  • Consistency: Unify codebase language to TS between cli and core.
  • Better Module Interop: TypeScript's esModuleInterop simplifies consuming modern dependencies. We can use standard import syntax even for CommonJS modules, avoiding fragile require patterns and prepare for ESM migration.

Rough Implementation

  1. Configuration: Create packages/cli/tsconfig.json extending the base config.
  • Crucial: specific outDir: "dist" to ensure generated .js files do not clutter the source directory.
  1. Package Updates: Update package.json, add build script (tsc), update bin entry to point to dist/index.js, add files allowlist to publish only the dist folder. Add typescript and ts-node dependencies.
  2. Development Workflow
  • Set up a dev script using ts-node (or tsx) to allow developers to run the CLI directly from source without a manual build step.

Describe the solution you'd like

  • packages/cli source files are renamed to .ts and contain valid TypeScript.
  • npm run build compiles code successfully to packages/cli/dist/.
  • running locally allows running the CLI directly from TypeScript source. (Needs investigation)
  • markbind --help and standard commands (init, build, serve) function identically to before.

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Discussion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions