Hone logo
Hone
Problems

Modularizing TypeScript Projects with Barrel Files

Barrel files are a crucial tool for organizing and exporting modules in TypeScript projects, especially as they grow in complexity. This challenge asks you to implement a utility function that analyzes a directory structure and generates a barrel file (a .ts file containing multiple export statements) based on the provided file paths. This is useful for simplifying imports and creating a cleaner API for your modules.

Problem Description

You are tasked with creating a TypeScript function generateBarrelFileContent that takes an array of file paths (strings) and generates the content of a barrel file. Each file path represents a TypeScript module that should be exported in the barrel file. The function should produce a string containing the necessary export statements to import all specified modules.

Key Requirements:

  • The function must accept an array of strings, where each string is a relative path to a TypeScript module (e.g., "./components/Button.ts").
  • The function should generate a string containing export { ... } from './...' statements for each file path.
  • The generated string should be properly formatted with newlines for readability.
  • The function should handle edge cases gracefully, such as empty input arrays.

Expected Behavior:

The function should return a string containing the barrel file content. The content should consist of export { ... } from './...' statements, one for each input file path. The statements should be separated by newlines.

Edge Cases to Consider:

  • Empty Input Array: If the input array is empty, the function should return an empty string.
  • Invalid File Paths: While the challenge doesn't require validation of file paths, consider how your code would behave if an invalid path were provided. (For simplicity, assume all paths are valid for this challenge).
  • Duplicate File Paths: The function should handle duplicate file paths without generating duplicate export statements.

Examples

Example 1:

Input: ["./components/Button.ts", "./components/Input.ts"]
Output:
export { Button } from './components/Button.ts';
export { Input } from './components/Input.ts';
Explanation: The function generates two export statements, one for each file path, correctly formatted and separated by a newline.

Example 2:

Input: ["./utils/math.ts", "./utils/string.ts", "./utils/math.ts"]
Output:
export { add, subtract } from './utils/math.ts';
export { capitalize, reverse } from './utils/string.ts';
Explanation: The function handles duplicate file paths by only generating one export statement per unique file path.  (Note: The specific exports from each file are assumed for demonstration purposes; the function itself doesn't need to know what's exported from each file.)

Example 3:

Input: []
Output: ""
Explanation: The function returns an empty string when the input array is empty.

Constraints

  • The input array will contain only strings.
  • Each string in the input array will represent a relative path to a TypeScript file (e.g., "./src/components/MyComponent.ts").
  • The function should be efficient enough to handle an array of up to 100 file paths.
  • The output string should be a valid TypeScript barrel file content.

Notes

  • This challenge focuses on generating the content of the barrel file, not creating the file itself.
  • You don't need to worry about parsing the contents of the individual TypeScript files to determine what to export. Assume that each file exports something.
  • Consider using a Set to efficiently handle duplicate file paths.
  • Focus on producing clean, readable, and well-formatted output. The specific exports from each file are not important for this challenge. The goal is to generate the correct export { ... } from './...' syntax.
Loading editor...
typescript