Class CompileDeclarationsFiles


// Example usage:
const options = {
directoryPath: 'src/target/',
outputFileName: 'index',
};

const concatenator = new CreateIndexFiles(
options.directoryPath,
options.outputFileName,
);

concatenator.run()
.then(() => console.log('Done!'))
.catch((err) => console.error("Failed!", err));

Constructors

  • Constructor for the CreateIndexFiles class.

    Parameters

    • targetDirectory: string

      The directory to process.

    • outputName: string

      The base name for the output files (d.ts and js).

    Returns CompileDeclarationsFiles

    
    

Properties

_dtsExt: string
_jsExt: string
_outputName: string
_root: string
_targetDirectory: string

Methods

  • Checks for the existence of output files (d.ts and js) and removes them if they exist.

    This method constructs the full paths for the output d.ts and js files based on the project root and the provided file names. It then checks if each file exists using existsSync. If a file exists, it logs a message indicating that it will be removed and then proceeds to remove it using rm. Finally, it returns an object containing the full paths of the d.ts and js files.

    Parameters

    • dtsFileName: string

      The name of the d.ts output file (e.g., 'index.d.ts').

    • jsFileName: string

      The name of the js output file (e.g., 'index.js').

    Returns Promise<{ dts: string; js: string }>

    A promise that resolves with an object containing the full paths of the d.ts and js files.


    const compiler = new CompileDeclarationsFiles('src/target', 'index');
    const outputFiles = await compiler.checkOutputFiles('index.d.ts', 'index.js');
    console.log(outputFiles.dts); // Output: /path/to/project/index.d.ts
    console.log(outputFiles.js); // Output: /path/to/project/index.js
  • Parameters

    • files: Dirent[]

    Returns Promise<string[]>

  • Filters an array of strings to extract and return unique function and class names.

    This method iterates through an array of strings, typically representing lines of code from .d.ts files. It uses a regular expression to identify lines that declare functions or classes. The names of the identified functions and classes are extracted and stored. Duplicate names are removed, ensuring that only unique names are returned.

    Parameters

    • content: string[]

      An array of strings, each representing a line of code.

    Returns string[]

    An array of unique function and class names found in the input content.


    const content = [
    'function myFunction();',
    'class MyClass {}',
    'function myFunction();', // Duplicate
    'class AnotherClass {}',
    ];
    const filteredNames = this.filterFnClass(content);
    console.log(filteredNames); // Output: ['myFunction', 'MyClass', 'AnotherClass']
  • Formats content based on the specified task ('js' or 'dts') and adds a header indicating the source file.

    This method is responsible for formatting the content that will be written to either the .js or .d.ts output files. It adds a comment header indicating the source file path and then formats the content based on the task. For the 'js' task, it generates an export statement. For other tasks, it simply includes the content as is.

    Parameters

    • task: string

      The task to perform ('js' for generating export statements, 'dts' for including raw content).

    • content: string[]

      An array of strings representing the content to format.

    • filePath: string

      The path of the file from which the content originated.

    Returns string[]

    An array of strings representing the formatted content.


    const compiler = new CompileDeclarationsFiles('src/target', 'index');
    const dtsContent = ['function myFunction();', 'class MyClass {}'];
    const formattedDts = compiler.format('dts', dtsContent, '/path/to/myFile.d.ts');
    console.log(formattedDts);
    // Output: ['// Content from: /path/to/myFile.d.ts', 'function myFunction();', 'class MyClass {}', '\n']

    const jsContent = ['myFunction', 'MyClass'];
    const formattedJs = compiler.format('js', jsContent, '/path/to/myFile.d.ts');
    console.log(formattedJs);
    // Output: ['// Content from: /path/to/myFile.d.ts', 'export { myFunction, MyClass } from "./index";', '\n']
  • Generates export statements for functions and classes found in the concatenated .d.ts files.

    This method takes the concatenated content of .d.ts files and extracts function and class names. It then formats these names into export statements for a .js file.

    Parameters

    • docs: string[]

      An array of strings representing the concatenated content of .d.ts files.

    • outputDtsPath: string

      The path where the combined .d.ts file is written.

    Returns string[]

    An array of strings representing the formatted export statements.


    const docs = ['function myFunction();', 'class MyClass {}'];
    const outputDtsPath = '/path/to/index.d.ts';
    const exports = this.generateExports(docs, outputDtsPath);
    console.log(exports); // Output: ['// Content from: /path/to/index.d.ts', 'export { myFunction, MyClass } from "./index";', '\n']
  • Opens a file at the specified path and returns a file handle.

    Parameters

    • filePath: string

      The path of the file to open.

    Returns Promise<FileHandle>

    A promise that resolves to a FileHandle object for the opened file.

    • Throws an error if the file cannot be opened.
    
    
  • Executes the main logic of the class.

    This method orchestrates the entire process of:

    1. Checking and removing existing output files (d.ts and js).
    2. Searching for .d.ts files in the target directory.
    3. Concatenating the content of all found .d.ts files.
    4. Writing the concatenated content to a new .d.ts file.
    5. Generating export statements from the concatenated content.
    6. Writing the export statements to a new .js file.

    Returns Promise<void>

    A promise that resolves when the process is complete.


    const compiler = new CompileDeclarationsFiles('src/target', 'index');
    await compiler.run();
  • Reads a file line by line and returns an array of strings, where each string is a line from the file.

    Parameters

    • filePath: string

      The path to the file to be scanned.

    Returns Promise<string[]>

    • A promise that resolves to an array of strings, where each string is a line from the file.
    
    
  • Searches for files within a specified directory, filtering by file extension.

    This method recursively scans a directory and its subdirectories for files. It can filter the results based on a specified file extension.

    Parameters

    • directory: string

      The path to the directory to search.

    • ext: string

      A single file extension to filter by.

    Returns Promise<Dirent[]>

    • A promise that resolves to an array of Dirent objects representing the found files.
    
    
  • Writes an array of strings to a file, overwriting any existing content.

    Parameters

    • outputFilePath: string

      The path to the file to be written.

    • content: string[]

      The array of strings to be written to the file.

    Returns Promise<void>

    • A promise that resolves when the write operation is complete.