@villedemontreal/general-utils
Version:
General utilities library
228 lines • 8.06 kB
TypeScript
import { StdioOptions } from 'child_process';
/**
* General utilities
*/
export declare class Utils {
private tscCompilerOptionsParams;
/**
* Promisified setTimeout() utility function.
*
* @param ms The number of milliseconds to sleep for.
*/
sleep(ms: number): Promise<void>;
/**
* Checks if a String is null, undefined,
* empty, or contains only whitespaces.
*/
isBlank(str: string): boolean;
/**
* A better version of "isNaN()".
* For example, an empty string is NOT considered as a
* number.
*/
isNaNSafe(value: any): boolean;
/**
* If the "el" is not undefined, returns it as is.
* If the el is undefined, returns NULL.
*
* This is useful when undefined is not acceptable
* but null is.
*/
getDefinedOrNull(el: any): any;
/**
* Checks if a value is an integer.
*
* If you want to use the includeZero parameter without
* using the positiveOnly parameter, we suggest to pass
* undefined as a second parameter.
*
* After a positive check, we suggest to
* pass the value with the Number object (Number(value))
* to "clean" it, e.g., getting rid of unmeaningful
* decimal zeros or whitespaces.
*/
isIntegerValue(value: any, positiveOnly?: boolean, includeZero?: boolean): boolean;
/**
* Converts a string to a boolean.
*
* The string is TRUE only if it is
* "true" (case insensitive) or "1"
* (the *number* 1 is also accepted)
*
* Otherwise, it is considered as FALSE.
*/
stringToBoolean(str: string): boolean;
/**
* Make sure a file is safe to delete, that is:
* - It is truly
* - It is not the path of a root directory or file
*/
isSafeToDelete(path: string): boolean;
/**
* Checks if a path points to an existing directory.
*
* @returns true if the path points to an existing
* directory (not a file).
*/
isDir(dirPath: string): boolean;
/**
* Checks if a directory is empty.
*
* @returns true if the directory is empty or
* doesn't exist. Returns false if the path
* points to a *file* or to a directory that
* is not empty.
*/
isDirEmpty(dirPath: string): boolean;
/**
* Deletes a file, promisified and in a
* solid way.
*
* You can't delete a root file using this function.
*/
deleteFile(filePath: string): Promise<boolean>;
/**
* Deletes a directory, promisified and in a
* solid way.
*
* You can't delete a root directory using this function.
*/
deleteDir(dirPath: string): Promise<boolean>;
/**
* Clears a directory, promisified and in a
* solid way.
*
* You can't clear a root directory using this function.
*/
clearDir(dirPath: string): Promise<void>;
protected get tscCompilerOptions(): string[];
/**
* Runs the "tsc" command on specific files
* using the same options than the ones found
* in the "tsconfig.json" file of the project.
*
* @param files the absolute paths of the files to compile.
* @outdir allows us to redirect the output directory
*/
tsc(files: string[]): Promise<void>;
/**
* Creates a "range", an array of continuous integers, from
* "start" to "end".
* Both "start" and "end" are inclusive.
*/
range: (start: number, end: number) => number[];
/**
* Returns a free port.
*/
findFreePort(): Promise<number>;
/**
* Validates if the object is of type Date and
* is valid. Deserializing an invalid string
* to a Date may result in a Date, but which is invalid.
* Then loadash's isDate(d) is not enough to detect
* if the result is valid or not. This function is.
*/
isValidDate(date: any): boolean;
/**
* To be used as a "@Transform" decorator on a
* Date field, when using the "class-transformer"
* library. For example :
*
* @Transform(utils.dateTransformer)
* public created: Date;
*
* When using what class-transformer provides
* by default ("@Type(() => Date)") there are cases
* that are problematic :
* - "123" and "true" :result in valid dates!
*
* Note : *only* use this decorator on the Date field, not
* in addition to "@Type(() => Date)"!
*/
dateTransformer: (value: any) => Date;
/**
* Throws an Error for the specified element as type "never".
*
* To be used in the "default" section of a switch/case statement.
* This allows the validation at compile time that all elements of the
* swtiched element are managed (as long as it is a discret type such
* as an enum).
*/
throwNotManaged: (messagePrefix: string, element: never) => void;
/**
* Returns TRUE if the parameter is an object but is not an array,
* a Date or a function
* By default, _.isObject(x) from Lodash also returns TRUE for
* an Array, for a Date and a function.
*
* Returns FALSE for null/undefined.
*/
isObjectStrict: (val: any) => boolean;
/**
* Returns TRUE if the specified "array" contains at least one object
* that has the specified "key" and if the value associated with that key is
* strictly equals to the specified "value".
*/
arrayContainsObjectWithKeyEqualsTo: (array: any[], key: string, value: any) => boolean;
/**
* @deprecated Use `exec()` instead.
*/
execPromisified(command: string, args: string[], dataHandler?: (stdoutData: string, stderrData: string) => void, useShellOption?: boolean): Promise<void>;
/**
* Execute a shell command.
*
* This function is a promisified version of Node's `spawn()`
* with extra options added
* ( https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options ).
*
* @param bin The executable program to call.
*
* @param args The arguments for the program.
*
* @param options.successExitCodes The acceptable codes the
* process must exit with to be considered as a success.
* Defaults to [0].
*
* @param options.outputHandler A function that will receive
* the output of the process (stdOut and stdErr).
* This allows you to capture this output and manipulate it.
* No handler by default.
*
* @param options.disableConsoleOutputs Set to `true` in order
* to disable outputs in the current parent process
* (you can still capture them using a `options.dataHandler`).
* Defaults to `false`.
*
* @param options.stdio See https://nodejs.org/api/child_process.html#child_process_options_stdio
* Defaults to `['inherit', 'pipe', 'pipe']`.
*
* @param options.useShellOption See the "shell" option:
* https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
* Defaults to `true`.
*
* @returns The exit code
*
* @throws Will fail with a `ExecError` error if the process returns
* a code different than `options.successExitCodes` ("0" by default).
* The exit code would then be available in the generated Error:
* `err.exitCode`.
*/
exec(bin: string, args?: string[], options?: {
successExitCodes?: number | number[];
outputHandler?: (stdoutOutput: string, stderrOutput: string) => void;
disableConsoleOutputs?: boolean;
stdio?: StdioOptions;
useShellOption?: boolean;
}): Promise<number>;
}
/**
* Error thrown when a process launched with `exec()` fails.
*/
export declare class ExecError extends Error {
exitCode: number;
constructor(message: string, exitCode: number);
}
export declare function getValueDescription(value: any): string;
export declare function getValueDescriptionWithType(value: any): string;
export declare const utils: Utils;
//# sourceMappingURL=utils.d.ts.map