- Introduction
- Features
- Installation
- Usage
- Helper Functions
- To do
- Future Plans
- Connect with me on Social
The Collect Exports For Bundle Script is a utility designed to automatically generate export statements for files in a specified directory. This tool is especially useful for projects with numerous files that need to be exported, eliminating the tedious task of manually writing export statements.
The primary motivation behind the creation of the Collect Exports For Bundle Script is to automate and simplify the process of bundling exports. By offering a streamlined tool for generating export statements, developers can save time and avoid potential mistakes associated with manually handling numerous export statements.
-
Sharing Types and Enums Between Projects:
- In large-scale development, where multiple projects may depend on the same types and enumerations, maintaining consistency becomes crucial. By centralizing and automating the bundling of these shared types and enums, developers can ensure uniformity and reduce the risk of discrepancies between projects.
-
Automate Extracting Components from Design Systems:
- Design systems often consist of numerous components, each with its own file. Manually exporting each component from an index file can be time-consuming and prone to errors. The Collect Exports For Bundle Script automates this process, making it easy to extract components from the design system's index file, which in turn accelerates the feature deployment process.
-
Saving Time and Increasing Efficiency:
- In today's agile development environment, every moment counts. Manually writing export statements, especially in projects with a vast number of files, can be tedious and error-prone. This package was designed to alleviate this pain point, allowing developers to focus more on writing code and less on the intricacies of managing exports.
By addressing these challenges, the Collect Exports For Bundle Script offers a valuable tool set that promotes best practices, enhances productivity, and ensures consistency across projects.
- Export all your TypeScript files from a project into a single
index.ts
file. - Use in other GitHub gists and repositories.
- Type-safe with a default export option.
- Recursive scanning of directories.
- Specify which file extensions to include or exclude.
- Configurable through command-line arguments or direct function parameters.
- Suitable for both command-line usage and integration with GitHub actions.
To install the Collect Exports For Bundle Script from the provided gist:
npm install @devlander/collect-exports-for-bundle
yarn add @devlander/collect-exports-for-bundle
After installation, you can use the Collect Exports For Bundle Script in two primary ways: Programmatically
First, require or import the autoExporter function from the installed module and call it with an options object:
const autoExporter = require("@devlander/collect-exports-for-bundle").default
autoExporter({
rootDir: "src",
allowedExtensions: [".ts", ".tsx"],
ignoredExtensions: [".test.ts"],
});
This utility is built in TypeScript, tailored for seamless integration with other TypeScript packages.
Control your package's exports by choosing a default export. This ensures that when your package compiles, you'll have full command over your default export and the exports you need to destructure. Below are examples of packages with default and named exports
import PackageName, { otherThingsInYourPackage } from "package-name"
import { otherThingsInYourPackage } from "package-name"
This tool can be effectively utilized in both GitHub repositories and gists. For GitHub gists, particularly when building modules and compiling from the project's root where there isn't a specific src folder, the files and excludedFolders parameters in the configuration become essential.
const autoExporter = require("@devlander/collect-exports-for-bundle").default
const init = () => {
autoExporter({
rootDir: "./src",
outputFilenameExtension: ".ts",
outputFileName: "index",
exportMode: "both",
primaryExportFile: "main.ts",
allowedExtensions: [".enum.ts", ".component.tsx", ".type.ts", ".type.tsx"],
ignoredExtensions: [".test.ts", ".test.tsx", ".stories.tsx"],
});
};
init();
You can also configure the tool to specifically collect files within a particular directory using the specificFiles property. In this example, main.ts and isEmpty.ts would be the only files searched for an export
const autoExporter = require("@devlander/collect-exports-for-bundle").default
const init = () => {
const configForAutoExporter: AutoExporterOptions = {
rootDir: "src",
specificFiles: ["main.ts", "isEmpty.ts"]
};
autoExporter(configForAutoExporter);
};
init();
collectPathsFromDirectories takes in allowedExtensions, ignoredExtensions, specificFiles, debug and excludedFolders. This function returns paths that have valid file extensions for your directory
const {collectPathsFromDirectories} = require("@devlander/collect-exports-for-bundle").default
const validPaths: string[] = await collectPathsFromDirectories("./src", {
allowedExtensions: [".component.tsx", ".tsx", ".ts"],
ignoredExtensions: [".spec.tsx", ".test.tsx"],
specificFiles: [],
debug: false,
excludedFolders: ["node_modules", "dist", "build"]
})
createExtensions takes in a word, a list of words, and fileExtensions and will return a list of file extensions with combinations of the three. This function returns paths that have valid file extensions for your directory
const {createExtensions} = require("@devlander/collect-exports-for-bundle")
const webExtensions = createExtensions(
"web",
["props", "type", "types", "interface", "enum"],
[".tsx", ".ts"]
);
// Output for webExtensions
[
'.web.tsx', '.web.ts',
'.web.props.tsx', '.props.web.tsx',
'.web.props.ts', '.props.web.ts',
'.web.type.tsx', '.type.web.tsx',
'.web.type.ts', '.type.web.ts',
'.web.types.tsx', '.types.web.tsx',
'.web.types.ts', '.types.web.ts',
'.web.interface.tsx', '.interface.web.tsx',
'.web.interface.ts', '.interface.web.ts',
'.web.enum.tsx', '.enum.web.tsx',
'.web.enum.ts', '.enum.web.ts'
]
- Create cli
- Create in depth tests for each function
- Detect Circular dependencies
- Write tests for deep nesting with large file directories
- Have a result output showing which directories it went through, and which directories it skipped, same with functions
// updates for git // improved tests for example folder // using real life sub modules for tests in example folder // you can now pass in a title and description to the config for a comment to be left, which is useful for troubleshooting // got duration from when the script starts, and when it ends, which is useful for troubleshooting // wrote test for regex match functions
// TODO // results will collect all the directories and filenames // while going through the scripts // at the very very end