Skip to content

Trustworthy refactoring knife for JS/TS projects 🍰

License

Notifications You must be signed in to change notification settings

mherod/module-master

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

module-master

The surgical refactoring tool for TypeScript monorepos. Move files between packages, rename exports, and watch every import update automatically β€” with zero breaking changes.

Built on the TypeScript Compiler API for AST-level precision. Understands your barrel files, respects your path aliases, and handles the gnarly edge cases that break other tools.

Why module-master?

Refactoring in monorepos is painful. Move a utility from your app to a shared package and you'll spend the next hour:

  • Hunting down every import that needs updating
  • Figuring out which barrel files need new exports
  • Rebuilding packages in the right order
  • Fixing the type errors you inevitably missed

module-master does all of this in one command:

bun src/cli.ts move apps/web/src/utils/formatDate.ts packages/shared/src/formatDate.ts

That's it. Every import updated. Barrel files handled. Packages rebuilt. Type-checked and verified.

Install

bun install

Quick Start

# Move a file and update all imports
bun src/cli.ts move src/old/file.ts src/new/file.ts

# Preview changes without modifying files
bun src/cli.ts move src/old.ts src/new.ts --dry-run

# Move between packages in a monorepo
bun src/cli.ts move apps/web/src/utils/helper.ts packages/shared/src/helper.ts

Cross-Package Refactoring

The killer feature. Move files between packages in your monorepo and module-master handles everything:

bun src/cli.ts move apps/main-web/lib/utils/date-formatter.ts packages/shared-utils/src/date-formatter.ts --verbose

What happens automatically:

  1. Workspace discovery β€” Detects pnpm, yarn, or npm workspaces
  2. Smart import updates β€” Changes imports to use package names (@scope/shared) instead of brittle relative paths
  3. Barrel file management β€” Adds export to destination package's index.ts, removes from source
  4. Import splitting β€” When a file imports multiple things from a barrel and only some are moving, splits the import correctly:
// Before: importing from a barrel that re-exports the moved file
import { formatDate, makeAuthorUrl } from "@/lib/utils";

// After: automatically split into two imports
import { formatDate } from "@plugg/shared-utils";
import { makeAuthorUrl } from "@/lib/utils";
  1. Package rebuilds β€” Runs build scripts on affected packages so dist/ stays in sync
  2. Type verification β€” Runs tsc --noEmit before and after to catch any issues

No more duplicate export errors

Other tools naively change export * from "./moved-file" to export * from "@scope/package", which pulls in everything and causes conflicts. module-master removes the re-export entirely β€” the destination package exports it now.

Commands

move <source> <target>

Move a file and update all import references across the codebase.

bun src/cli.ts move src/utils/old.ts src/helpers/new.ts --dry-run

Handles:

  • All import statements referencing the moved file
  • Path aliases from tsconfig.json
  • Barrel file re-exports (export * from)
  • Dynamic imports and require() calls
  • Internal imports within the moved file
  • Jest/Vitest mock calls

rename <file> <oldName> <newName>

Rename an exported symbol and update all imports.

bun src/cli.ts rename src/components/Button.tsx Button PrimaryButton --dry-run
  • Renames the export in the source file
  • Updates all named imports across the codebase
  • Updates barrel file re-exports
  • Preserves import aliases (import { Old as X } β†’ import { New as X })

analyze <file>

Understand a module's place in your codebase.

bun src/cli.ts analyze src/components/Button.tsx --verbose

Shows:

  • All exports from the file
  • All imports used by the file
  • All files that reference this module
  • Barrel files that re-export this module

find <query>

Search for files and exports by name.

bun src/cli.ts find User -p /path/to/project
bun src/cli.ts find Button --type export
bun src/cli.ts find helpers --type file
  • Case-insensitive partial matching
  • Searches filenames and export names simultaneously
  • Smart sorting: exact matches first

alias <target>

Normalize import paths using tsconfig aliases, relative paths, or the shortest option.

bun src/cli.ts alias src/components --prefer=alias
bun src/cli.ts alias src --prefer=relative
bun src/cli.ts alias . --prefer=shortest

discover <directory>

Map all tsconfig.json files in a project.

bun src/cli.ts discover /path/to/monorepo --verbose

Shows tsconfig inheritance, project references, file ownership, and path aliases.

workspace <directory>

Discover monorepo workspace packages and their structure.

bun src/cli.ts workspace /path/to/monorepo --json

Supports pnpm, yarn, and npm workspaces. Shows packages, entrypoints, barrel files, and internal dependencies.

Features

  • AST-level precision β€” Uses TypeScript Compiler API, not regex
  • Type-safe refactoring β€” Runs tsc --noEmit before and after changes
  • Full import coverage β€” Named, default, namespace, dynamic, require, require.resolve
  • Test mock support β€” jest.mock(), vi.mock(), vitest.mock()
  • Path alias preservation β€” Respects your tsconfig paths
  • Barrel file intelligence β€” recursively resolves re-export chains to find deep dependencies
  • Monorepo-native β€” First-class support for pnpm, yarn, and npm workspaces
  • Cross-package moves β€” The hard problem, solved
  • Import splitting β€” Handles mixed imports from barrels correctly
  • Auto-rebuild β€” Keeps dist/ in sync after cross-package moves
  • Dry-run mode β€” Preview everything before committing

Options

Option Short Description
--help -h Show help message
--version -v Show version
--dry-run -n Preview changes without modifying files
--project -p Path to project directory or tsconfig.json
--verbose Enable detailed output
--no-verify Skip type checking verification (not recommended)
--type -t Filter find results by type: file, export, or all
--prefer Alias strategy: alias, relative, or shortest
--json Output in JSON format (workspace command)

How It Works

  1. Load project β€” Parse tsconfig.json, extract compiler options and path aliases
  2. Discover workspace β€” Find all packages, barrel files, and tsconfigs
  3. Build dependency graph β€” Scan all files, create import/importedBy maps with recursive barrel resolution
  4. Find references β€” Query graph for files importing target module (direct and through barrels)
  5. Calculate changes β€” Determine new specifiers, split imports if needed, identify removals
  6. Apply updates β€” Modify source text at precise AST node positions
  7. Update barrels β€” Add exports to destination, remove from source
  8. Rebuild packages β€” Run build scripts on affected packages
  9. Verify types β€” Run tsc to ensure no breaking changes

Development

bun test             # Run tests
bun run lint         # Lint and format with Biome
bun run typecheck    # Type check with tsc
bun run build        # Compile to standalone binary

License

MIT

About

Trustworthy refactoring knife for JS/TS projects 🍰

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published