import * as fs from 'fs'; import * as path from 'path'; import { TsPublishConfig } from '../mod_config/index.js'; import { FsHelpers } from '../mod_fs/index.js'; /** * TsUnpacker handles flattening of nested TypeScript output directories. * * When TypeScript compiles files that import from sibling directories, * it creates a nested structure like: * dist_ts_core/ts_core/index.js * dist_ts_core/ts_shared/helper.js * * This class flattens it to: * dist_ts_core/index.js */ export class TsUnpacker { private sourceFolderName: string; private destDir: string; private cwd: string; private config: TsPublishConfig; constructor(sourceFolderName: string, destDir: string, cwd: string = process.cwd()) { this.sourceFolderName = sourceFolderName; this.destDir = destDir; this.cwd = cwd; this.config = new TsPublishConfig(path.join(cwd, sourceFolderName)); } /** * Create an unpacker from a glob pattern * './ts_core/**\/*.ts' → sourceFolderName = 'ts_core' */ public static fromGlobPattern( sourcePattern: string, destDir: string, cwd: string = process.cwd() ): TsUnpacker | null { const sourceFolderName = FsHelpers.extractSourceFolder(sourcePattern); if (!sourceFolderName) { return null; } return new TsUnpacker(sourceFolderName, destDir, cwd); } /** * Get the source folder name */ public getSourceFolderName(): string { return this.sourceFolderName; } /** * Get the destination directory */ public getDestDir(): string { return this.destDir; } /** * Check if unpacking should be performed based on tspublish.json config * Default is true if not specified */ public async shouldUnpack(): Promise { return this.config.shouldUnpack; } /** * Check if nested structure exists in the destination directory */ public async detectNesting(): Promise { const nestedPath = path.join(this.destDir, this.sourceFolderName); return FsHelpers.directoryExists(nestedPath); } /** * Get the path to the nested directory */ public getNestedPath(): string { return path.join(this.destDir, this.sourceFolderName); } /** * Perform the unpack operation - flatten nested output directories. * * When TypeScript compiles files that import from sibling directories, * it creates a nested structure like dist_ts/ts/ with siblings like * dist_ts/ts_interfaces/. This method flattens by: * 1. Removing sibling directories (non-source folders) * 2. Moving contents of the nested source folder up to the dest dir * 3. Removing the now-empty nested source folder * * Uses synchronous fs operations to avoid race conditions with * async readdir returning partial/stale results under signal pressure * or XFS metadata lag (observed in process-group environments like gitzone). * * Returns true if unpacking was performed, false if skipped. */ public async unpack(): Promise { if (!(await this.shouldUnpack())) { return false; } if (!(await this.detectNesting())) { return false; } const nestedPath = this.getNestedPath(); // Step 1: Remove sibling entries (everything in dest except the source folder) // Use opendirSync to keep a single directory handle open for reliable iteration const destDir = fs.opendirSync(this.destDir); let destEntry; while ((destEntry = destDir.readSync()) !== null) { if (destEntry.name !== this.sourceFolderName) { fs.rmSync(path.join(this.destDir, destEntry.name), { recursive: true, force: true }); } } destDir.closeSync(); // Step 2: Move all contents from nested dir up to dest dir // Use opendirSync to keep a single directory handle open — this avoids // partial results from readdirSync which opens a fresh file descriptor // each call and can miss entries on XFS with delayed metadata logging const nestedDir = fs.opendirSync(nestedPath); let nestedEntry; let moved = 0; while ((nestedEntry = nestedDir.readSync()) !== null) { fs.renameSync( path.join(nestedPath, nestedEntry.name), path.join(this.destDir, nestedEntry.name), ); moved++; } nestedDir.closeSync(); // Step 3: Remove the now-empty nested directory fs.rmdirSync(nestedPath); // Diagnostic: verify final state const finalEntries = fs.readdirSync(this.destDir); console.log(` 📦 Unpacked ${this.sourceFolderName}: moved ${moved} entries, final: ${finalEntries.length} entries`); return true; } } /** * Convenience function to perform unpack operation * Can be used directly without instantiating the class */ export async function performUnpack( sourcePattern: string, destDir: string, cwd: string = process.cwd() ): Promise { const unpacker = TsUnpacker.fromGlobPattern(sourcePattern, destDir, cwd); if (!unpacker) { return false; } return unpacker.unpack(); }