UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

150 lines (149 loc) 6.34 kB
import { DependencyMap, SimpleDependencyMap } from "./types"; /** * @description Patches a file with given values. * @summary Reads a file, applies patches using TextUtils, and writes the result back to the file. * * @param {string} path - The path to the file to be patched. * @param {Record<string, number | string>} values - The values to patch into the file. * @return {void} * * @function patchFile * * @mermaid * sequenceDiagram * participant Caller * participant patchFile * participant fs * participant readFile * participant TextUtils * participant writeFile * Caller->>patchFile: Call with path and values * patchFile->>fs: Check if file exists * patchFile->>readFile: Read file content * readFile->>fs: Read file * fs-->>readFile: Return file content * readFile-->>patchFile: Return file content * patchFile->>TextUtils: Patch string * TextUtils-->>patchFile: Return patched content * patchFile->>writeFile: Write patched content * writeFile->>fs: Write to file * fs-->>writeFile: File written * writeFile-->>patchFile: File written * patchFile-->>Caller: Patching complete * * @memberOf module:fs-utils */ export declare function patchFile(path: string, values: Record<string, number | string>): void; /** * @description Reads a file and returns its content. * @summary Reads the content of a file at the specified path and returns it as a string. * * @param {string} path - The path to the file to be read. * @return {string} The content of the file. * * @function readFile * * @memberOf module:utils */ export declare function readFile(path: string): string; /** * @description Writes data to a file. * @summary Writes the provided data to a file at the specified path. * * @param {string} path - The path to the file to be written. * @param {string | Buffer} data - The data to be written to the file. * @return {void} * * @function writeFile * * @memberOf module:utils */ export declare function writeFile(path: string, data: string | Buffer): void; /** * @description Retrieves all files recursively from a directory. * @summary Traverses through directories and subdirectories to collect all file paths. * * @param {string} p - The path to start searching from. * @param filter * @return {string[]} Array of file paths. * * @function getAllFiles * * @memberOf module:fs-utils */ export declare function getAllFiles(p: string, filter?: (f: string, i?: number) => boolean): string[]; export declare function renameFile(source: string, dest: string): Promise<void>; export declare function copyFile(source: string, dest: string): void; export declare function deletePath(p: string): void; /** * @description Retrieves package information from package.json. * @summary Loads and parses the package.json file from a specified directory or the current working directory. Can return the entire package object or a specific property. * @param {string} [p=process.cwd()] - The directory path where the package.json file is located. * @param {string} [property] - Optional. The specific property to retrieve from package.json. * @return {object | string} The parsed contents of package.json or the value of the specified property. * @function getPackage * @mermaid * sequenceDiagram * participant Caller * participant getPackage * participant readFile * participant JSON * Caller->>getPackage: Call with path and optional property * getPackage->>readFile: Read package.json * readFile-->>getPackage: Return file content * getPackage->>JSON: Parse file content * JSON-->>getPackage: Return parsed object * alt property specified * getPackage->>getPackage: Check if property exists * alt property exists * getPackage-->>Caller: Return property value * else property doesn't exist * getPackage-->>Caller: Throw Error * end * else no property specified * getPackage-->>Caller: Return entire package object * end * @memberOf module:utils */ export declare function getPackage(p?: string, property?: string): object | string; export declare function setPackageAttribute(attr: string, value: string, p?: string): void; /** * @description Retrieves the version from package.json. * @summary A convenience function that calls getPackage to retrieve the "version" property from package.json. * @param {string} [p=process.cwd()] - The directory path where the package.json file is located. * @return {string} The version string from package.json. * @function getPackageVersion * @memberOf module:fs-utils */ export declare function getPackageVersion(p?: string): string; /** * @description Retrieves all dependencies from the project. * @summary Executes 'npm ls --json' command to get a detailed list of all dependencies (production, development, and peer) and their versions. * @param {string} [path=process.cwd()] - The directory path of the project. * @return {Promise<{prod: Array<{name: string, version: string}>, dev: Array<{name: string, version: string}>, peer: Array<{name: string, version: string}>}>} An object containing arrays of production, development, and peer dependencies. * @function getDependencies * @mermaid * sequenceDiagram * participant Caller * participant getDependencies * participant runCommand * participant JSON * Caller->>getDependencies: Call with optional path * getDependencies->>runCommand: Execute 'npm ls --json' * runCommand-->>getDependencies: Return command output * getDependencies->>JSON: Parse command output * JSON-->>getDependencies: Return parsed object * getDependencies->>getDependencies: Process dependencies * getDependencies-->>Caller: Return processed dependencies * @memberOf module:fs-utils */ export declare function getDependencies(path?: string): Promise<DependencyMap>; export declare function updateDependencies(): Promise<void>; export declare function installIfNotAvailable(deps: string[] | string, dependencies?: SimpleDependencyMap): Promise<SimpleDependencyMap>; export declare function pushToGit(): Promise<void>; export declare function installDependencies(dependencies: { prod?: string[]; dev?: string[]; peer?: string[]; }): Promise<void>; export declare function normalizeImport<T>(importPromise: Promise<T>): Promise<T>;