Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IndexGenerationOptions {
export interface IndexGenerationConfig {
enabled?: boolean;
directories?: string[];
skipDirectories?: string[];
options?: Partial<IndexGenerationOptions>;
updateMainIndex?: boolean;
}
Expand Down
31 changes: 22 additions & 9 deletions dist/core/formatters/rules/index-generation/IndexGenerationRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class IndexGenerationRule extends BaseFormattingRule.BaseFormattingRule {
];
return testPatterns.some((pattern) => pattern.test(fileName));
}
generateSingleDirectoryIndex(dir, options) {
generateSingleDirectoryIndex(dir, skipPaths, options) {
try {
const entries = fs__namespace.readdirSync(dir, { withFileTypes: true });
const exports$1 = [];
Expand All @@ -74,7 +74,11 @@ class IndexGenerationRule extends BaseFormattingRule.BaseFormattingRule {
if (this.isTestDirectory(entry.name)) {
continue;
}
const subIndexPath = path__namespace.join(dir, entry.name, options.indexFileName);
const subDir = path__namespace.join(dir, entry.name);
if (skipPaths.has(subDir)) {
continue;
}
const subIndexPath = path__namespace.join(subDir, options.indexFileName);
if (fs__namespace.existsSync(subIndexPath)) {
exports$1.push(`export * from "./${entry.name}";`);
}
Expand Down Expand Up @@ -104,7 +108,7 @@ ${exports$1.join("\n")}
console.warn(`Warning: Failed to generate index for ${dir}: ${error.message}`);
}
}
generateIndexExportRecursive(dir, options) {
generateIndexExportRecursive(dir, skipPaths, options) {
try {
const entries = fs__namespace.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
Expand All @@ -113,22 +117,25 @@ ${exports$1.join("\n")}
continue;
}
const subDir = path__namespace.join(dir, entry.name);
this.generateIndexExportRecursive(subDir, options);
if (skipPaths.has(subDir)) {
continue;
}
this.generateIndexExportRecursive(subDir, skipPaths, options);
}
}
this.generateSingleDirectoryIndex(dir, options);
this.generateSingleDirectoryIndex(dir, skipPaths, options);
} catch (error) {
console.warn(`Warning: Failed to process directory ${dir}: ${error.message}`);
}
}
generateIndexExport(dir, options) {
generateIndexExport(dir, skipPaths, options) {
if (!fs__namespace.existsSync(dir)) {
return;
}
if (options.recursive) {
this.generateIndexExportRecursive(dir, options);
this.generateIndexExportRecursive(dir, skipPaths, options);
} else {
this.generateSingleDirectoryIndex(dir, options);
this.generateSingleDirectoryIndex(dir, skipPaths, options);
}
}
discoverExportableModules(srcDir) {
Expand Down Expand Up @@ -180,9 +187,15 @@ ${exports$1}
const config = this.getIndexGenerationConfig();
const directories = config?.directories || [];
const options = { ...this.defaultOptions, ...config?.options };
const skipPaths = new Set(
(config?.skipDirectories || []).map((d) => path__namespace.resolve(projectRoot, d))
);
for (const dir of directories) {
const fullDirPath = path__namespace.resolve(projectRoot, dir);
this.generateIndexExport(fullDirPath, options);
if (skipPaths.has(fullDirPath)) {
continue;
}
this.generateIndexExport(fullDirPath, skipPaths, options);
}
if (config?.updateMainIndex !== false) {
const srcDir = path__namespace.join(projectRoot, "src");
Expand Down
1 change: 0 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from "./build-plugins";
export * from "./core";
export * from "./formatters";
export * from "./shared";
export { tsfmt } from "./tsfmt";
2 changes: 0 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const StructuralIndentationRule = require("./core/formatters/rules/style/Structu
const FormatterPipeline = require("./core/pipeline/FormatterPipeline.js");
const _package = require("./formatters/package.js");
const types = require("./shared/types.js");
const tsfmt = require("./tsfmt.js");
exports.transformGenericsPlugin = transformGenericsPlugin.transformGenericsPlugin;
exports.ASTAnalyzer = ASTAnalyzer.ASTAnalyzer;
exports.ASTTransformer = ASTTransformer.ASTTransformer;
Expand Down Expand Up @@ -65,4 +64,3 @@ exports.FormatterError = FormatterPipeline.FormatterError;
exports.FormatterPipeline = FormatterPipeline.FormatterPipeline;
exports.sortExportsKeys = _package.sortExportsKeys;
exports.DefaultSortOptions = types.DefaultSortOptions;
exports.tsfmt = tsfmt.tsfmt;
9 changes: 0 additions & 9 deletions dist/tsfmt.js

This file was deleted.

37 changes: 28 additions & 9 deletions src/core/formatters/rules/index-generation/IndexGenerationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface IndexGenerationConfig {
/** Directories to process for index generation */
directories?: string[];

/** Directories to always skip, even if listed in directories (takes priority) */
skipDirectories?: string[];

/** Default options for index generation */
options?: Partial<IndexGenerationOptions>;

Expand Down Expand Up @@ -88,7 +91,7 @@ export class IndexGenerationRule extends BaseFormattingRule {
return testPatterns.some(pattern => pattern.test(fileName));
}

private generateSingleDirectoryIndex(dir: string, options: IndexGenerationOptions): void {
private generateSingleDirectoryIndex(dir: string, skipPaths: Set<string>, options: IndexGenerationOptions): void {
try {
const entries = fs.readdirSync(dir, {withFileTypes: true});
const exports: string[] = [];
Expand All @@ -103,8 +106,13 @@ export class IndexGenerationRule extends BaseFormattingRule {
if (this.isTestDirectory(entry.name)) {
continue;
}
const subDir = path.join(dir, entry.name);

if (skipPaths.has(subDir)) {
continue;
}
// Check if subdirectory has an index file
const subIndexPath = path.join(dir, entry.name, options.indexFileName);
const subIndexPath = path.join(subDir, options.indexFileName);

if (fs.existsSync(subIndexPath)) {
exports.push(`export * from "./${entry.name}";`);
Expand Down Expand Up @@ -145,7 +153,7 @@ ${exports.join("\n")}
}
}

private generateIndexExportRecursive(dir: string, options: IndexGenerationOptions): void {
private generateIndexExportRecursive(dir: string, skipPaths: Set<string>, options: IndexGenerationOptions): void {
try {
const entries = fs.readdirSync(dir, {withFileTypes: true});

Expand All @@ -158,25 +166,29 @@ ${exports.join("\n")}

const subDir = path.join(dir, entry.name);

this.generateIndexExportRecursive(subDir, options);
if (skipPaths.has(subDir)) {
continue;
}

this.generateIndexExportRecursive(subDir, skipPaths, options);
}
}

this.generateSingleDirectoryIndex(dir, options);
this.generateSingleDirectoryIndex(dir, skipPaths, options);
} catch (error) {
console.warn(`Warning: Failed to process directory ${dir}: ${(error as Error).message}`);
}
}

private generateIndexExport(dir: string, options: IndexGenerationOptions): void {
private generateIndexExport(dir: string, skipPaths: Set<string>, options: IndexGenerationOptions): void {
if (!fs.existsSync(dir)) {
return;
}

if (options.recursive) {
this.generateIndexExportRecursive(dir, options);
this.generateIndexExportRecursive(dir, skipPaths, options);
} else {
this.generateSingleDirectoryIndex(dir, options);
this.generateSingleDirectoryIndex(dir, skipPaths, options);
}
}

Expand Down Expand Up @@ -247,11 +259,18 @@ ${exports}
const config = this.getIndexGenerationConfig();
const directories = config?.directories || [];
const options = {...this.defaultOptions, ...config?.options};
const skipPaths = new Set(
(config?.skipDirectories || []).map(d => path.resolve(projectRoot, d))
);

for (const dir of directories) {
const fullDirPath = path.resolve(projectRoot, dir);

this.generateIndexExport(fullDirPath, options);
if (skipPaths.has(fullDirPath)) {
continue;
}

this.generateIndexExport(fullDirPath, skipPaths, options);
}

// Update main src/index.ts if configured
Expand Down
Loading