UNPKG

foalts2-cli

Version:

CLI tool for FoalTS

281 lines (280 loc) 9.73 kB
/** * Error thrown by the FileSystem which aims to be pretty * printed without the stacktrace. * * @export * @class ClientError * @extends {Error} */ export declare class ClientError extends Error { readonly name = "ClientError"; } /** * This class provides more methods that Node std "fs". * It also allows to create an isolated directory during directory. * * @export * @class FileSystem */ export declare class FileSystem { currentDir: string; private readonly testDir; private logs; /** * Do not show create and update logs. * * @returns {this} * @memberof FileSystem */ hideLogs(): this; /** * Changes the current working directory. * * @param {string} path - Relative path of the directory. * @returns {this} * @memberof FileSystem */ cd(path: string): this; /** * Changes the current working directory to the project root * directory. * * It searches for the closer package.json containing @foal/core * as dependency. * * @returns {this} * @memberof FileSystem */ cdProjectRootDir(): this; /** * Checks if a file or directory exists. * * @param {string} path - The path relative to the client directory. * @returns {boolean} * @memberof FileSystem */ exists(path: string): boolean; /** * Recursively ensures that a directory exists. If the directory structure does not * exist, it is created. * * @param {string} path - The path relative to the client directory. * @returns {this} * @memberof FileSystem */ ensureDir(path: string): this; /** * Recursively ensures that a directory exists if the condition is true. * If the directory structure does not exist, it is created. * * @param {boolean} condition - The condition. * @param {string} path - The path relative to the client directory. * @returns {this} * @memberof FileSystem */ ensureDirOnlyIf(condition: boolean, path: string): this; /** * Ensures that the file exists. If the file does not exist, it is created. * * @param {string} path - The path relative to the client directory. * @returns {this} * @memberof FileSystem */ ensureFile(path: string): this; /** * Copies a file from the `templates` directory. * * @param {string} src - The source path relative to the `templates/` directory. * @param {string} dest - The destination path relative to the client directory. * @returns {this} * @memberof FileSystem */ copy(src: string, dest: string): this; /** * Copies a file from the `templates` directory if the condition is true. * * @param {boolean} condition - The condition. * @param {string} src - The source path relative to the `templates/` directory. * @param {string} dest - The destination path relative to the client directory. * @returns {this} * @memberof FileSystem */ copyOnlyIf(condition: boolean, src: string, dest: string): this; /** * Copies and renders a template from the `templates` directory. * * @param {string} src - The source path relative to the `templates/` directory. * @param {string} dest - The destination path relative to the client directory. * @param {*} locals - The template variables. * @returns {this} * @memberof FileSystem */ render(src: string, dest: string, locals: any): this; /** * Copies and renders a template from the `templates` directory if the condition is true. * * @param {boolean} condition - The condition. * @param {string} src - The source path relative to the `templates/` directory. * @param {string} dest - The destination path relative to the client directory. * @param {*} locals - The template variables. * @returns {this} * @memberof FileSystem */ renderOnlyIf(condition: boolean, src: string, dest: string, locals: any): this; /** * Reads and modifies the content of a file. * * @param {string} path - The path relative to the client directory. * @param {(content: string) => string} callback - The callback that modifies the content. * @returns {this} * @memberof FileSystem */ modify(path: string, callback: (content: string) => string): this; /** * Reads and modifies the content of a file if the condition is true. * * @param {boolean} condition - The condition. * @param {string} path - The path relative to the client directory. * @param {(content: string) => string} callback - The callback that modifies the content. * @returns {this} * @memberof FileSystem */ modifyOnlyfIf(condition: boolean, path: string, callback: (content: string) => string): this; /** * Add a named import at the bottom of the file. * * @param {string} path - The file path relative to the client directory. * @param {string} specifier - The import specifier. * @param {string} source - The import source. * @returns {this} * @memberof FileSystem */ addNamedExportIn(path: string, specifier: string, source: string): this; /** * Adds or extends a named import at the beginning of the file. * * If an import already exists with this source path, it is completed. * If it does not already exist, it is added at the end of all imports. * * @param {string} path - The file path relative to the client directory. * @param {string} specifier - The import specifier. * @param {string} source - The import source. * @returns {this} * @memberof FileSystem */ addOrExtendNamedImportIn(path: string, specifier: string, source: string, options?: { logs: boolean; }): this; /** * Creates or adds an element to the array property of a class. * * If the class does not exist, this method does nothing. * * @param {string} path - The file path relative to the client directory. * @param {string} className - The class name. * @param {string} propertyName - The property name. * @param {string} element - The item to add to the array. * @returns {this} * @memberof FileSystem */ addOrExtendClassArrayPropertyIn(path: string, propertyName: string, element: string, options?: { logs: boolean; }): this; /** * Returns true if the project package.json has this dependency. * Returns false otherwise. * * @param {string} name - The name of the dependency. * @returns {boolean} * @memberof FileSystem */ projectHasDependency(name: string): boolean; /** * Returns the dependencies of the project package.json. * * @returns {{ name: string, version: string }[]} */ getProjectDependencies(): { name: string; version: string; }[]; /** * Returns the dev dependencies of the project package.json. * * @returns {{ name: string, version: string }[]} */ getProjectDevDependencies(): { name: string; version: string; }[]; /************************ Testing Methods ************************/ /** * Creates the test client directory. Sets current directory to none. * * @memberof FileSystem */ setUp(): void; /** * Empties and removes the test client directory. Sets current directory to none. * * @memberof FileSystem */ tearDown(): void; /** * Throws an error if the file or directory does not exist. * * @param {string} path - The path relative to the client directory. * @returns {this} * @memberof FileSystem */ assertExists(path: string): this; /** * Throws an error if the file or directory exists. * * @param {string} path - The path relative to the client directory. * @returns {this} * @memberof FileSystem */ assertNotExists(path: string): this; /** * Throws an error if the directory is not empty. * * @param {string} path - The path relative to the client directory. * @memberof FileSystem */ assertEmptyDir(path: string): void; /** * Throws an error if the two files are different. * * @param {string} actual - The path relative to the client directory. * @param {string} expected - The path relative to the `specs/` directory. * @param {{ binary: boolean }} [{ binary }={ binary: true }] - Specify if the file is binary. * @returns {this} * @memberof FileSystem */ assertEqual(actual: string, expected: string, { binary }?: { binary: boolean; }): this; /** * Copies a file from the `fixtures` directory. * * @param {string} src - The source path relative to the `fixtures/` directory. * @param {string} dest - The destination path relative to the client directory. * @returns {this} * @memberof FileSystem */ copyFixture(src: string, dest: string): this; /** * Removes a file. * * @param {string} path - The path relative to the client directory. * @memberof FileSystem */ rmfile(path: string): void; private isTestingEnvironment; private parse; private logCreate; private logUpdate; }