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YAAY,EAAE,CAAC;KAC/D;IACD,2DAA2D;IAC3D,MAAM,KAAK,GAAG,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACtD,IAAI,KAAK,EAAE;QACT,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACrC,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3D,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;KACvE;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,qBAAqB,CAAC,IAAa,EAAE,IAAa;IACzD,IAAI,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,IAAI,EAAE,MAAK,EAAE,EAAE;QACvB,MAAM,IAAI,kCAAkB,CAAC,IAAI,CAAC,CAAC;KACpC;AACH,CAAC","sourcesContent":["import { codeFrameColumns } from '@babel/code-frame';\nimport fs from 'fs';\nimport JSON5 from 'json5';\nimport path from 'path';\nimport { promisify } from 'util';\nimport writeFileAtomic from 'write-file-atomic';\n\nimport JsonFileError, { EmptyJsonFileError } from './JsonFileError';\n\nconst writeFileAtomicAsync: (\n filename: string,\n data: string | Buffer,\n options: writeFileAtomic.Options\n) => void = promisify(writeFileAtomic);\n\nexport type JSONValue = boolean | number | string | null | JSONArray | JSONObject;\nexport interface JSONArray extends Array<JSONValue> {}\nexport interface JSONObject {\n [key: string]: JSONValue | undefined;\n}\n\ntype Defined<T> = T extends undefined ? never : T;\n\ntype Options<TJSONObject extends JSONObject> = {\n badJsonDefault?: TJSONObject;\n jsonParseErrorDefault?: TJSONObject;\n cantReadFileDefault?: TJSONObject;\n ensureDir?: boolean;\n default?: TJSONObject;\n json5?: boolean;\n space?: number;\n addNewLineAtEOF?: boolean;\n};\n\nconst DEFAULT_OPTIONS = {\n badJsonDefault: undefined,\n jsonParseErrorDefault: undefined,\n cantReadFileDefault: undefined,\n ensureDir: false,\n default: undefined,\n json5: false,\n space: 2,\n addNewLineAtEOF: true,\n};\n\n/**\n * The JsonFile class represents the contents of json file.\n *\n * It's polymorphic on \"JSONObject\", which is a simple type representing\n * and object with string keys and either objects or primitive types as values.\n * @type {[type]}\n */\nexport default class JsonFile<TJSONObject extends JSONObject> {\n file: string;\n options: Options<TJSONObject>;\n\n static read = read;\n static readAsync = readAsync;\n static parseJsonString = parseJsonString;\n static writeAsync = writeAsync;\n static getAsync = getAsync;\n static setAsync = setAsync;\n static mergeAsync = mergeAsync;\n static deleteKeyAsync = deleteKeyAsync;\n static deleteKeysAsync = deleteKeysAsync;\n static rewriteAsync = rewriteAsync;\n\n constructor(file: string, options: Options<TJSONObject> = {}) {\n this.file = file;\n this.options = options;\n }\n\n read(options?: Options<TJSONObject>): TJSONObject {\n return read(this.file, this._getOptions(options));\n }\n\n async readAsync(options?: Options<TJSONObject>): Promise<TJSONObject> {\n return readAsync(this.file, this._getOptions(options));\n }\n\n async writeAsync(object: TJSONObject, options?: Options<TJSONObject>) {\n return writeAsync(this.file, object, this._getOptions(options));\n }\n\n parseJsonString(json: string, options?: Options<TJSONObject>): TJSONObject {\n return parseJsonString(json, options);\n }\n\n async getAsync<K extends keyof TJSONObject, TDefault extends TJSONObject[K] | null>(\n key: K,\n defaultValue: TDefault,\n options?: Options<TJSONObject>\n ): Promise<Defined<TJSONObject[K]> | TDefault> {\n return getAsync(this.file, key, defaultValue, this._getOptions(options));\n }\n\n async setAsync(key: string, value: unknown, options?: Options<TJSONObject>) {\n return setAsync(this.file, key, value, this._getOptions(options));\n }\n\n async mergeAsync(\n sources: Partial<TJSONObject> | Partial<TJSONObject>[],\n options?: Options<TJSONObject>\n ): Promise<TJSONObject> {\n return mergeAsync<TJSONObject>(this.file, sources, this._getOptions(options));\n }\n\n async deleteKeyAsync(key: string, options?: Options<TJSONObject>) {\n return deleteKeyAsync(this.file, key, this._getOptions(options));\n }\n\n async deleteKeysAsync(keys: string[], options?: Options<TJSONObject>) {\n return deleteKeysAsync(this.file, keys, this._getOptions(options));\n }\n\n async rewriteAsync(options?: Options<TJSONObject>) {\n return rewriteAsync(this.file, this._getOptions(options));\n }\n\n _getOptions(options?: Options<TJSONObject>): Options<TJSONObject> {\n return {\n ...this.options,\n ...options,\n };\n }\n}\n\nfunction read<TJSONObject extends JSONObject>(\n file: string,\n options?: Options<TJSONObject>\n): TJSONObject {\n let json;\n try {\n json = fs.readFileSync(file, 'utf8');\n } catch (error) {\n assertEmptyJsonString(json, file);\n const defaultValue = cantReadFileDefault(options);\n if (defaultValue === undefined) {\n throw new JsonFileError(`Can't read JSON file: ${file}`, error, error.code, file);\n } else {\n return defaultValue;\n }\n }\n return parseJsonString(json, options, file);\n}\n\nasync function readAsync<TJSONObject extends JSONObject>(\n file: string,\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n let json;\n try {\n json = await fs.promises.readFile(file, 'utf8');\n } catch (error) {\n assertEmptyJsonString(json, file);\n const defaultValue = cantReadFileDefault(options);\n if (defaultValue === undefined) {\n throw new JsonFileError(`Can't read JSON file: ${file}`, error, error.code);\n } else {\n return defaultValue;\n }\n }\n return parseJsonString(json, options);\n}\n\nfunction parseJsonString<TJSONObject extends JSONObject>(\n json: string,\n options?: Options<TJSONObject>,\n fileName?: string\n): TJSONObject {\n assertEmptyJsonString(json, fileName);\n try {\n if (_getOption(options, 'json5')) {\n return JSON5.parse(json);\n } else {\n return JSON.parse(json);\n }\n } catch (e) {\n const defaultValue = jsonParseErrorDefault(options);\n if (defaultValue === undefined) {\n const location = locationFromSyntaxError(e, json);\n if (location) {\n const codeFrame = codeFrameColumns(json, { start: location });\n e.codeFrame = codeFrame;\n e.message += `\\n${codeFrame}`;\n }\n throw new JsonFileError(`Error parsing JSON: ${json}`, e, 'EJSONPARSE', fileName);\n } else {\n return defaultValue;\n }\n }\n}\n\nasync function getAsync<TJSONObject extends JSONObject, K extends keyof TJSONObject, DefaultValue>(\n file: string,\n key: K,\n defaultValue: DefaultValue,\n options?: Options<TJSONObject>\n): Promise<any> {\n const object = await readAsync(file, options);\n if (key in object) {\n return object[key];\n }\n if (defaultValue === undefined) {\n throw new JsonFileError(`No value at key path \"${key}\" in JSON object from: ${file}`);\n }\n return defaultValue;\n}\n\nasync function writeAsync<TJSONObject extends JSONObject>(\n file: string,\n object: TJSONObject,\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n if (options?.ensureDir) {\n await fs.promises.mkdir(path.dirname(file), { recursive: true });\n }\n const space = _getOption(options, 'space');\n const json5 = _getOption(options, 'json5');\n const addNewLineAtEOF = _getOption(options, 'addNewLineAtEOF');\n let json;\n try {\n if (json5) {\n json = JSON5.stringify(object, null, space);\n } else {\n json = JSON.stringify(object, null, space);\n }\n } catch (e) {\n throw new JsonFileError(`Couldn't JSON.stringify object for file: ${file}`, e);\n }\n const data = addNewLineAtEOF ? `${json}\\n` : json;\n await writeFileAtomicAsync(file, data, {});\n return object;\n}\n\nasync function setAsync<TJSONObject extends JSONObject>(\n file: string,\n key: string,\n value: unknown,\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n // TODO: Consider implementing some kind of locking mechanism, but\n // it's not critical for our use case, so we'll leave it out for now\n const object = await readAsync(file, options);\n return writeAsync(file, { ...object, [key]: value }, options);\n}\n\nasync function mergeAsync<TJSONObject extends JSONObject>(\n file: string,\n sources: Partial<TJSONObject> | Partial<TJSONObject>[],\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n const object = await readAsync(file, options);\n if (Array.isArray(sources)) {\n Object.assign(object, ...sources);\n } else {\n Object.assign(object, sources);\n }\n return writeAsync(file, object, options);\n}\n\nasync function deleteKeyAsync<TJSONObject extends JSONObject>(\n file: string,\n key: string,\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n return deleteKeysAsync(file, [key], options);\n}\n\nasync function deleteKeysAsync<TJSONObject extends JSONObject>(\n file: string,\n keys: string[],\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n const object = await readAsync(file, options);\n let didDelete = false;\n\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n if (object.hasOwnProperty(key)) {\n delete object[key];\n didDelete = true;\n }\n }\n\n if (didDelete) {\n return writeAsync(file, object, options);\n }\n return object;\n}\n\nasync function rewriteAsync<TJSONObject extends JSONObject>(\n file: string,\n options?: Options<TJSONObject>\n): Promise<TJSONObject> {\n const object = await readAsync(file, options);\n return writeAsync(file, object, options);\n}\n\nfunction jsonParseErrorDefault<TJSONObject extends JSONObject>(\n options: Options<TJSONObject> = {}\n): TJSONObject | void {\n if (options.jsonParseErrorDefault === undefined) {\n return options.default;\n } else {\n return options.jsonParseErrorDefault;\n }\n}\n\nfunction cantReadFileDefault<TJSONObject extends JSONObject>(\n options: Options<TJSONObject> = {}\n): TJSONObject | void {\n if (options.cantReadFileDefault === undefined) {\n return options.default;\n } else {\n return options.cantReadFileDefault;\n }\n}\n\nfunction _getOption<TJSONObject extends JSONObject, K extends keyof Options<TJSONObject>>(\n options: Options<TJSONObject> | undefined,\n field: K\n): Options<TJSONObject>[K] {\n if (options) {\n if (options[field] !== undefined) {\n return options[field];\n }\n }\n return DEFAULT_OPTIONS[field];\n}\n\nfunction locationFromSyntaxError(error: any, sourceString: string) {\n // JSON5 SyntaxError has lineNumber and columnNumber.\n if ('lineNumber' in error && 'columnNumber' in error) {\n return { line: error.lineNumber, column: error.columnNumber };\n }\n // JSON SyntaxError only includes the index in the message.\n const match = /at position (\\d+)/.exec(error.message);\n if (match) {\n const index = parseInt(match[1], 10);\n const lines = sourceString.slice(0, index + 1).split('\\n');\n return { line: lines.length, column: lines[lines.length - 1].length };\n }\n\n return null;\n}\n\nfunction assertEmptyJsonString(json?: string, file?: string) {\n if (json?.trim() === '') {\n throw new EmptyJsonFileError(file);\n }\n}\n"]} |