import { DataFileFromDataOptions, Key, Logger, DataPath, PredicateFunction, StringDataPath, DataFileLoadOptions } from "./types"; /** * Read, edit and write configuration files. */ export default class DataFile { #private; /** Actual data */ data: T; /** Whether file exists or cosmiconfig configuration found. */ found: boolean; private constructor(); /** File path relative to root. */ private get shortPath(); /** Whether file can be saved using this library. */ get readOnly(): boolean; /** * Returns whether given `path` exists in file data. * * @param path is data path of the property to check. * @returns whether path exists. * * @example * dataFile.has("script.build"); * dataFile.has(["script", "build"]); */ has(path: DataPath): boolean; /** * Gets the value at `path` of file data. If the resolved value is undefined, the `defaultValue` is returned in its place. * * @param path is data path of the property to get. * @param defaultValue is value to get if path does not exists on data. * @returns data stored in given object path or default value. * * @example * dataFile.get("script.build"); * dataFile.get(["script", "build"]); */ get(path: DataPath, defaultValue?: any): any; /** * Sets the value at `path` of file data. If a portion of path doesn't exist, it's created. * Arrays are created for missing index properties while objects are created for all other missing properties. * * @param path is data path of the property to set. * @param value is value to set or a function which returns value to be set. * @param if is the function to test whether operation should be performed. If result is false, operation is not performed. * @param logger is winston compatible logger to be used when logging. * * @example * dataFile * .set("script.build", "tsc") * .set(["scripts", "test"], "jest", { if: (value) => value !== "mocha" }); */ set(path: DataPath, value: any, { if: condition, logger }?: { if?: PredicateFunction; logger?: Logger; }): this; /** * Deletes the property at `path` of file data. * * @param path is data path of the property to delete. * @param if is the function to test whether operation should be performed. If result is false, operation is not performed. * @param logger is winston compatible logger to be used when logging. * * @example * dataFile * .delete("script.build") * .delete(["scripts", "test"], { if: (value) => value !== "jest" }); */ delete(path: DataPath, { if: condition, logger }?: { if?: PredicateFunction; logger?: Logger; }): this; /** * Deletes path recursively if value at given path is empty. (If parent path is empty after value is deleted, parent path would be deleted too.) * * @param path is data path of the property to delete. * * @example * // { a: { b: { c: {} } } } * dataFile.deleteEmptyPath("a.b.c"); // Result: {} * * // { a: { b: { c: {}, x: 1 } } } * dataFile.deleteEmptyPath("a.b.c"); // Result: { a: { b: { x: 1 } } } */ deleteEmptyPath(path: DataPath): this; /** * Tests whether given value at path is empty. Empty values are empty objects, maps, sets, string, `undefined` and `null`. * * @returns whether given value is empty. */ isEmpty(path: DataPath): boolean; /** * This method is like assign except that it recursively merges own and inherited enumerable string keyed properties of source objects * into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. * Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. * Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources. * * If you would like merge root object (`this.data`), use empty array `[]` as path, because `undefined`, '' and `null` are valid object keys. * * @param path is data path of the property to delete. * @param valuesAndOptions are objects to merge given path or a function which returns object to be merged. * @param valuesAndOptions.predicateFn is the function to test whether operation should be performed. If result is false, operation is not performed. * @param valuesAndOptions.logger is winston compatible logger to be used when logging. * * @example * dataFile.merge("scripts", { build: "tsc", test: "jest", }, { if: (scripts) => scripts.build !== "someCompiler" }); * dataFile.merge([], { name: "my-module", version: "1.0.0" }); */ merge(path: DataPath, ...valuesAndOptions: any[]): this; /** * Returns deleted and modified keys (paths) in data file. Keys may be filtered by required condition. * * @param filter is a filter function to test whether to include key and type in result. * @returns set and deleted keys * * @example * dataFile.getModifiedKeys({ include: "scripts", exclude: ["scripts.validate", "scripts.docs"] }); */ getModifiedKeys({ filter }?: { filter?: (path: Key[], type: "set" | "deleted") => boolean; }): { set: StringDataPath[]; deleted: StringDataPath[]; }; /** * Sort keys in given order. Missing keys in `keys` added to the end. If no keys are provided, sorts alphabetically. * * @ignore * @param object is the object to order keys of. * @param start are ordered keys to appear at the beginning of given path when saved. * @param end are ordered keys to appear at the end of given path when saved. * @returns same object with ordered keys. */ private _sortObjectKeys; /** * When keys/values added which are previously does not exist, they are added to the end of the file during file write. * This method allows reordering of the keys in given path. Required keys may be put at the beginning and of the order. * * If you would like sort root object (`this.data`) use `sort` method or, provide use empty array `[]` as path, because `undefined`, '' and `null` are valid object keys. * * @param path is data path of the property to order keys of. * @param start are ordered keys to appear at the beginning of given path when saved. * @param end are ordered keys to appear at the end of given path when saved. * * @example * dataFile.sortKeys("scripts", { start: ["build", "lint"], end: ["release"] }); * dataFile.sortKeys({ start: ["name", "description"], end: ["dependencies", "devDependencies"] }); */ sortKeys(path: DataPath, { start, end }?: { start?: string[]; end?: string[]; }): this; /** Saves file. If this is a partial data uses only related part by utilizing `rootDataPath` option. */ save({ /** Whether to throw if file is read only. */ throwOnReadOnly, /** Winston compatible logger to be used when logging. */ logger, }?: { throwOnReadOnly?: boolean; logger?: Logger; }): Promise; /** * Returns data serialized as text. * * @param wholeFile is whether to serialize whole file when `rootDataPath` is set. Reads whole file including `rootDataPath` part and serializes whole file data. * @returns serialized data as string. */ serialize(wholeFile?: boolean): Promise; /** * Logs given operation * * @param op is the name of the operation. * @param success is whether operations is successful. * @param path is the path of the data modified. */ private logOperation; /** * Creates [[DataFile]] instance from given data to be saved for given file path. * * @param path is path of the file. * @param data is the data to create [[DataFile]] from. * @param options are options. * @returns [[DataFile]] instance. */ static fromData(path: string, data: object, options?: DataFileFromDataOptions): Promise; /** * Reads data from given file. If file is not present returns default data to be saved with {{save}} method. * * @param path is path of the file. * @param options are options. * @returns [[DataFile]] instance. * @throws if file exists but cannot be parsed. */ static load(path: string, options?: DataFileLoadOptions): Promise; /** * Reload data from disk. If file is not present resets data to default data. */ reload(): Promise; } //# sourceMappingURL=data-file.d.ts.map