Hone logo
Hone
Problems

Re-export Utilities for Modular TypeScript Projects

Creating modular TypeScript projects often involves breaking down code into smaller, manageable files and directories. Re-exporting utilities from these modules allows you to provide a clean and simplified API for consumers of your library, avoiding them needing to import from multiple internal modules. This challenge focuses on building a utility that automates the creation of re-export files.

Problem Description

You are tasked with creating a TypeScript utility function called createReExportFile. This function will take a list of module paths (relative to the current working directory) and an output file path as input. It should generate a new TypeScript file at the specified output path that re-exports all the modules listed in the input array. The generated file should use the export * from syntax for re-exporting.

Key Requirements:

  • Input: An array of strings, where each string represents the path to a TypeScript module (e.g., "./src/utils/math.ts").
  • Output: A new TypeScript file containing export * from statements for each module in the input array.
  • Error Handling: The function should handle cases where a module path is invalid (e.g., the file doesn't exist). In such cases, it should log an error message to the console and continue processing the remaining modules.
  • File Generation: The function should write the generated TypeScript code to the specified output file path.
  • Path Resolution: The function should resolve relative module paths correctly.

Expected Behavior:

Given an input array of module paths and an output file path, the function should create a TypeScript file at the output path containing the appropriate export * from statements. If any module path is invalid, an error message should be logged, but the function should continue to generate the re-export file for the valid modules.

Edge Cases to Consider:

  • Empty input array: The output file should be empty (or contain a comment indicating no modules were re-exported).
  • Invalid module paths: Handle cases where the specified module file does not exist.
  • Output file path already exists: Overwrite the existing file.
  • Module paths with different extensions (e.g., .ts, .tsx): The function should handle these correctly.

Examples

Example 1:

Input: ["./src/utils/math.ts", "./src/utils/string.ts"]
Output: `./re-exports/index.ts` (containing: `export * from './src/utils/math.ts';\nexport * from './src/utils/string.ts';`)
Explanation: The function creates a file named `index.ts` in the `re-exports` directory, re-exporting the contents of `math.ts` and `string.ts`.

Example 2:

Input: ["./src/utils/math.ts", "./nonexistent/file.ts", "./src/utils/string.ts"]
Output: `./re-exports/index.ts` (containing: `export * from './src/utils/math.ts';\nexport * from './src/utils/string.ts';`)
Explanation: The function creates `index.ts` and re-exports `math.ts` and `string.ts`.  An error message is logged to the console regarding the invalid path `./nonexistent/file.ts`, but the process continues.

Example 3:

Input: []
Output: `./re-exports/index.ts` (containing: `// No modules to re-export.`)
Explanation: The function creates `index.ts` and includes a comment indicating that no modules were re-exported.

Constraints

  • Module paths are relative to the current working directory.
  • The output file path is also relative to the current working directory.
  • The function should be able to handle a maximum of 100 module paths in the input array.
  • The function should not perform any complex transformations on the module paths (e.g., renaming files). It should simply re-export them as they are.
  • The generated file should be valid TypeScript code.

Notes

  • Consider using the fs module in Node.js to interact with the file system.
  • You can use path.resolve to resolve relative module paths to absolute paths.
  • Think about how to handle errors gracefully and provide informative error messages.
  • The goal is to create a reusable utility that simplifies the process of creating re-export files in TypeScript projects. Focus on clarity and robustness.
  • You don't need to actually execute the re-exported modules; you only need to generate the re-export statements.
Loading editor...
typescript