UNPKG

hash-runner

Version:

Executes a command when a change is detected in specified files. Not an active file watcher.

136 lines (135 loc) 5.09 kB
export interface HashRunnerConfigFile { inputs: { includes: string[]; excludes?: string[]; }; outputs?: { includes: string[]; excludes?: string[]; }; execOnChange: string; hashFile: string; parallelizeComparisonsChunkSize?: number; } export interface HashFileV1 { [filepath: string]: string; } export interface HashFileV2 { hashSchemaVersion: "2"; inputs: Record<string, string>; outputs?: Record<string, string>; } export interface HashRunnerOptions { force?: boolean; silent?: boolean; } /** * Class representing a HashRunner that detects file changes and runs a command. */ export declare class HashRunner { configPath?: string; options: HashRunnerOptions; /** * Constructs a new HashRunner. * @param {string} [configPath] - Path to the configuration file. * @param {HashRunnerOptions} [options={}] - Options for the HashRunner. */ constructor(configPath?: string, options?: HashRunnerOptions); /** * Exits the process with a given exit code. * @param {number} code - Exit code. * @private */ private exitProcess; /** * Logs a message to the console if not in silent mode. * @param {string} message - The message to log. * @private */ private log; /** * Runs a given command in a child process. * @param {string} command - The command to run. * @param {string} cwd - The current working directory. * @returns {Promise<number>} - Resolves with the exit code of the command. * @private */ private runCommand; /** * Computes the hash of a given file using SHA-256. * @param {string} filePath - Path to the file. * @returns {Promise<string>} - The computed hash. * @private */ private computeFileHash; /** * Gets the hashes of input files based on the configuration. * @param {string} configDir - Directory containing the configuration. * @param {HashRunnerConfigFile} config - Configuration object. * @param {string} configFilePath - Path to the configuration file to exclude from processing. * @returns {Promise<Record<string, string>>} - A record of file paths and their corresponding hashes. * @private */ private getInputHashes; /** * Gets the hashes of output files based on the configuration. * @param {string} configDir - Directory containing the configuration. * @param {HashRunnerConfigFile} config - Configuration object. * @param {string} configFilePath - Path to the configuration file to exclude from processing. * @returns {Promise<Record<string, string> | undefined>} - A record of file paths and their corresponding hashes, or undefined if no outputs configured. * @private */ private getOutputHashes; /** * Checks if outputs have changed or are missing. * @param {Record<string, string> | undefined} currentOutputs - Current output hashes. * @param {Record<string, string> | undefined} previousOutputs - Previous output hashes. * @returns {boolean} - True if outputs are missing or have changed. * @private */ private checkOutputsChanged; /** * Loads the configuration from a file. * @returns {Promise<{ config: HashRunnerConfigFile; configDir: string; configFilePath: string }>} - The configuration, its directory, and file path. * @throws {Error} - Throws an error if the config file is not found or is empty. * @private */ private loadConfig; /** * Reads the hash file containing previous file hashes. * @param {string} hashFilePath - Path to the hash file. * @returns {Promise<HashFileV2 | null>} - The previous hashes or null if file not found. * @private */ private readHashFile; /** * Migrates a hash file from v1 to v2 format if needed. * @param {HashFileV1 | HashFileV2} hashData - The hash data to migrate. * @returns {HashFileV2} - The migrated hash data. * @private */ private migrateHashFile; /** * Writes the provided hash data to a file. * @param {string} hashFilePath - Path to the hash file. * @param {Record<string, string>} inputHashes - The input hash data to write. * @param {Record<string, string>} outputHashes - The output hash data to write. * @returns {Promise<void>} * @private */ private writeHashFile; /** * Checks if there are changes between current and previous file hashes in chunks. * @param {Record<string, string>} currentHashes - The current file hashes. * @param {Record<string, string>} previousHashes - The previous file hashes. * @param {number} [chunkSize=COMPARISON_CHUNK_SIZE] - Chunk size for parallel comparisons. * @returns {Promise<boolean>} - Resolves to true if changes are detected, otherwise false. * @private */ private checkChangesInChunks; /** * Main function to run the hash runner. * @returns {Promise<void>} */ run(): Promise<void>; }