hash-runner
Version:
Executes a command when a change is detected in specified files. Not an active file watcher.
96 lines (95 loc) • 3.41 kB
TypeScript
export interface HashRunnerConfigFile {
include?: string[];
exclude?: string[];
execOnChange: string;
hashFile: string;
parallelizeComparisonsChunkSize?: number;
}
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 files included in the configuration.
* @param {string} configDir - Directory containing the configuration.
* @param {HashRunnerConfigFile} config - Configuration object.
* @returns {Promise<Record<string, string>>} - A record of file paths and their corresponding hashes.
* @private
*/
private getHashedFiles;
/**
* Loads the configuration from a file.
* @returns {Promise<{ config: HashRunnerConfigFile; configDir: string }>} - The configuration and its directory.
* @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<Record<string, string> | null>} - The previous hashes or null if file not found.
* @private
*/
private readHashFile;
/**
* Writes the provided hash data to a file.
* @param {string} hashFilePath - Path to the hash file.
* @param {Record<string, string>} hashData - The 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>;
}