Skip to content

Add Automated TypeScript Index Generation#20

Open
bawahakim wants to merge 3 commits intoThe-Commit-Company:mainfrom
bawahakim:feat/add-indexes
Open

Add Automated TypeScript Index Generation#20
bawahakim wants to merge 3 commits intoThe-Commit-Company:mainfrom
bawahakim:feat/add-indexes

Conversation

@bawahakim
Copy link

Overview

This PR introduces automated generation of index.ts files for TypeScript type definitions, allowing for easier and more maintainable imports across modules. It also adds user controls for this feature via both the settings UI and CLI, and provides robust test coverage for both the generator logic and the CLI commands.

Key Features

1. Automated index.ts Generation

  • For each module in the {output_path} directory, an index.ts is generated that re-exports all .ts files (excluding itself).
  • A root-level index.ts is generated at:
    {output_path}/index.ts
    to re-export all module-level index files using namespace exports.
  • This makes it easy to import all types from a single entry point.

2. User Controls (Settings & CLI)

Settings Doctype

  • Added a Generate Index Files (generate_indexes) checkbox to the App Type Generation Paths table in the Type Generation Settings doctype.
  • Allows enabling/disabling index file generation per app.

CLI Flags

  • Both bench generate-types-for-doctype and bench generate-types-for-module now accept --generate-indexes / --no-generate-indexes flags.
  • CLI flag takes precedence over the doctype setting for that run.

3. Conditional Index Generation

  • The generator checks the CLI flag first, then falls back to the doctype setting.
  • Index files are only generated/updated if enabled, ensuring user control and backwards compatibility.

4. Testing

Unit Test

  • Added test_type_generator.py to verify correct creation of module and root index.ts files.
  • Tests are compatible with Frappe’s bench run-tests runner.

How to Use

Enable/Disable index generation in the Type Generation Settings doctype per app.

Override via CLI:

bench generate-types-for-doctype --app erpnext --doctype "Purchase Order" --generate-indexes

bench generate-types-for-doctype --app erpnext --doctype "Purchase Order" --no-generate-indexes

@bawahakim bawahakim changed the title Add Automated TypeScript Index Generation and CLI Controls Add Automated TypeScript Index Generation Jun 20, 2025
@bawahakim
Copy link
Author

@nikkothari22 I've made the generate index option true by default, because I don't see any reason not to include indexes, but let me know if you think otherwise!

@nikkothari22
Copy link
Member

I think it should be turned off by default because we generally avoid barrel imports in our codebase - it's a tiny bit faster to compile from what I've read online.

The CLI should also respect the settings set in the DB unless overridden via a flag.

@bawahakim
Copy link
Author

bawahakim commented Jun 21, 2025

Got it, I figured compile time doesn't matter much (especially if we're talking a few seconds), in contrast to keeping imports cleaner. In any case, will default it to false.

The CLI uses a tertiary flag. If no flag is passed, it prioritizes the DB setting, otherwise defaults to true (I can also change that to false). When flag is passed, it takes precedence over DB setting (that's why there's --generate-indexes / --no-generate-indexes flags)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants