Constructor for the CreateIndexFiles class.
The directory to process.
The base name for the output files (d.ts and js).
Private
_dtsPrivate
_jsPrivate
_outputPrivate
_rootPrivate
_targetChecks 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.
The name of the d.ts output file (e.g., 'index.d.ts').
The name of the js output file (e.g., 'index.js').
A promise that resolves with an object containing the full paths of the d.ts and js files.
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.
An array of strings, each representing a line of code.
An array of unique function and class names found in the input content.
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.
The task to perform ('js' for generating export statements, 'dts' for including raw content).
An array of strings representing the content to format.
The path of the file from which the content originated.
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']
Private
generateGenerates 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.
An array of strings representing the concatenated content of .d.ts files.
The path where the combined .d.ts file is written.
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']
Executes the main logic of the class.
This method orchestrates the entire process of:
A promise that resolves when the process is complete.
Reads a file line by line and returns an array of strings, where each string is a line from the file.
The path to the file to be scanned.
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.
The path to the directory to search.
A single file extension to filter by.
Writes an array of strings to a file, overwriting any existing content.
The path to the file to be written.
The array of strings to be written to the file.
Example