@expo/json-file
Advanced tools
Comparing version 8.3.4-canary-20240912-1059f85 to 8.4.0-canary-20240927-ab8a962
@@ -31,8 +31,15 @@ export type JSONValue = boolean | number | string | null | JSONArray | JSONObject; | ||
static parseJsonString: typeof parseJsonString; | ||
static write: typeof write; | ||
static writeAsync: typeof writeAsync; | ||
static get: typeof getSync; | ||
static getAsync: typeof getAsync; | ||
static set: typeof setSync; | ||
static setAsync: typeof setAsync; | ||
static merge: typeof merge; | ||
static mergeAsync: typeof mergeAsync; | ||
static deleteKey: typeof deleteKey; | ||
static deleteKeyAsync: typeof deleteKeyAsync; | ||
static deleteKeys: typeof deleteKeys; | ||
static deleteKeysAsync: typeof deleteKeysAsync; | ||
static rewrite: typeof rewrite; | ||
static rewriteAsync: typeof rewriteAsync; | ||
@@ -42,9 +49,16 @@ constructor(file: string, options?: Options<TJSONObject>); | ||
readAsync(options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
write(object: TJSONObject, options?: Options<TJSONObject>): TJSONObject; | ||
writeAsync(object: TJSONObject, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
parseJsonString(json: string, options?: Options<TJSONObject>): TJSONObject; | ||
get<K extends keyof TJSONObject, TDefault extends TJSONObject[K] | null>(key: K, defaultValue: TDefault, options?: Options<TJSONObject>): Defined<TJSONObject[K]> | TDefault; | ||
getAsync<K extends keyof TJSONObject, TDefault extends TJSONObject[K] | null>(key: K, defaultValue: TDefault, options?: Options<TJSONObject>): Promise<Defined<TJSONObject[K]> | TDefault>; | ||
set(key: string, value: unknown, options?: Options<TJSONObject>): TJSONObject; | ||
setAsync(key: string, value: unknown, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
merge(sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
mergeAsync(sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
deleteKey(key: string, options?: Options<TJSONObject>): TJSONObject; | ||
deleteKeyAsync(key: string, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
deleteKeys(keys: string[], options?: Options<TJSONObject>): TJSONObject; | ||
deleteKeysAsync(keys: string[], options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
rewrite(options?: Options<TJSONObject>): TJSONObject; | ||
rewriteAsync(options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
@@ -56,9 +70,16 @@ _getOptions(options?: Options<TJSONObject>): Options<TJSONObject>; | ||
declare function parseJsonString<TJSONObject extends JSONObject>(json: string, options?: Options<TJSONObject>, fileName?: string): TJSONObject; | ||
declare function getSync<TJSONObject extends JSONObject, K extends keyof TJSONObject, DefaultValue>(file: string, key: K, defaultValue: DefaultValue, options?: Options<TJSONObject>): any; | ||
declare function getAsync<TJSONObject extends JSONObject, K extends keyof TJSONObject, DefaultValue>(file: string, key: K, defaultValue: DefaultValue, options?: Options<TJSONObject>): Promise<any>; | ||
declare function write<TJSONObject extends JSONObject>(file: string, object: TJSONObject, options?: Options<TJSONObject>): TJSONObject; | ||
declare function writeAsync<TJSONObject extends JSONObject>(file: string, object: TJSONObject, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function setSync<TJSONObject extends JSONObject>(file: string, key: string, value: unknown, options?: Options<TJSONObject>): TJSONObject; | ||
declare function setAsync<TJSONObject extends JSONObject>(file: string, key: string, value: unknown, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function mergeAsync<TJSONObject extends JSONObject>(file: string, sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function merge<TJSONObject extends JSONObject>(file: string, sources: Partial<TJSONObject> | Partial<TJSONObject>[], options?: Options<TJSONObject>): TJSONObject; | ||
declare function deleteKeyAsync<TJSONObject extends JSONObject>(file: string, key: string, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function deleteKey<TJSONObject extends JSONObject>(file: string, key: string, options?: Options<TJSONObject>): TJSONObject; | ||
declare function deleteKeysAsync<TJSONObject extends JSONObject>(file: string, keys: string[], options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function deleteKeys<TJSONObject extends JSONObject>(file: string, keys: string[], options?: Options<TJSONObject>): TJSONObject; | ||
declare function rewriteAsync<TJSONObject extends JSONObject>(file: string, options?: Options<TJSONObject>): Promise<TJSONObject>; | ||
declare function rewrite<TJSONObject extends JSONObject>(file: string, options?: Options<TJSONObject>): TJSONObject; | ||
export {}; |
@@ -30,9 +30,9 @@ "use strict"; | ||
const code_frame_1 = require("@babel/code-frame"); | ||
const fs_1 = __importDefault(require("fs")); | ||
const json5_1 = __importDefault(require("json5")); | ||
const path_1 = __importDefault(require("path")); | ||
const util_1 = require("util"); | ||
const node_fs_1 = __importDefault(require("node:fs")); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const node_util_1 = require("node:util"); | ||
const write_file_atomic_1 = __importDefault(require("write-file-atomic")); | ||
const JsonFileError_1 = __importStar(require("./JsonFileError")); | ||
const writeFileAtomicAsync = (0, util_1.promisify)(write_file_atomic_1.default); | ||
const writeFileAtomicAsync = (0, node_util_1.promisify)(write_file_atomic_1.default); | ||
const DEFAULT_OPTIONS = { | ||
@@ -61,8 +61,15 @@ badJsonDefault: undefined, | ||
static parseJsonString = parseJsonString; | ||
static write = write; | ||
static writeAsync = writeAsync; | ||
static get = getSync; | ||
static getAsync = getAsync; | ||
static set = setSync; | ||
static setAsync = setAsync; | ||
static merge = merge; | ||
static mergeAsync = mergeAsync; | ||
static deleteKey = deleteKey; | ||
static deleteKeyAsync = deleteKeyAsync; | ||
static deleteKeys = deleteKeys; | ||
static deleteKeysAsync = deleteKeysAsync; | ||
static rewrite = rewrite; | ||
static rewriteAsync = rewriteAsync; | ||
@@ -79,2 +86,5 @@ constructor(file, options = {}) { | ||
} | ||
write(object, options) { | ||
return write(this.file, object, this._getOptions(options)); | ||
} | ||
async writeAsync(object, options) { | ||
@@ -86,17 +96,35 @@ return writeAsync(this.file, object, this._getOptions(options)); | ||
} | ||
get(key, defaultValue, options) { | ||
return getSync(this.file, key, defaultValue, this._getOptions(options)); | ||
} | ||
async getAsync(key, defaultValue, options) { | ||
return getAsync(this.file, key, defaultValue, this._getOptions(options)); | ||
} | ||
set(key, value, options) { | ||
return setSync(this.file, key, value, this._getOptions(options)); | ||
} | ||
async setAsync(key, value, options) { | ||
return setAsync(this.file, key, value, this._getOptions(options)); | ||
} | ||
async merge(sources, options) { | ||
return merge(this.file, sources, this._getOptions(options)); | ||
} | ||
async mergeAsync(sources, options) { | ||
return mergeAsync(this.file, sources, this._getOptions(options)); | ||
} | ||
deleteKey(key, options) { | ||
return deleteKey(this.file, key, this._getOptions(options)); | ||
} | ||
async deleteKeyAsync(key, options) { | ||
return deleteKeyAsync(this.file, key, this._getOptions(options)); | ||
} | ||
deleteKeys(keys, options) { | ||
return deleteKeys(this.file, keys, this._getOptions(options)); | ||
} | ||
async deleteKeysAsync(keys, options) { | ||
return deleteKeysAsync(this.file, keys, this._getOptions(options)); | ||
} | ||
rewrite(options) { | ||
return rewrite(this.file, this._getOptions(options)); | ||
} | ||
async rewriteAsync(options) { | ||
@@ -116,3 +144,3 @@ return rewriteAsync(this.file, this._getOptions(options)); | ||
try { | ||
json = fs_1.default.readFileSync(file, 'utf8'); | ||
json = node_fs_1.default.readFileSync(file, 'utf8'); | ||
} | ||
@@ -134,3 +162,3 @@ catch (error) { | ||
try { | ||
json = await fs_1.default.promises.readFile(file, 'utf8'); | ||
json = await node_fs_1.default.promises.readFile(file, 'utf8'); | ||
} | ||
@@ -175,2 +203,12 @@ catch (error) { | ||
} | ||
function getSync(file, key, defaultValue, options) { | ||
const object = read(file, options); | ||
if (key in object) { | ||
return object[key]; | ||
} | ||
if (defaultValue === undefined) { | ||
throw new JsonFileError_1.default(`No value at key path "${String(key)}" in JSON object from: ${file}`); | ||
} | ||
return defaultValue; | ||
} | ||
async function getAsync(file, key, defaultValue, options) { | ||
@@ -186,5 +224,28 @@ const object = await readAsync(file, options); | ||
} | ||
function write(file, object, options) { | ||
if (options?.ensureDir) { | ||
node_fs_1.default.mkdirSync(node_path_1.default.dirname(file), { recursive: true }); | ||
} | ||
const space = _getOption(options, 'space'); | ||
const json5 = _getOption(options, 'json5'); | ||
const addNewLineAtEOF = _getOption(options, 'addNewLineAtEOF'); | ||
let json; | ||
try { | ||
if (json5) { | ||
json = json5_1.default.stringify(object, null, space); | ||
} | ||
else { | ||
json = JSON.stringify(object, null, space); | ||
} | ||
} | ||
catch (e) { | ||
throw new JsonFileError_1.default(`Couldn't JSON.stringify object for file: ${file}`, e); | ||
} | ||
const data = addNewLineAtEOF ? `${json}\n` : json; | ||
write_file_atomic_1.default.sync(file, data, {}); | ||
return object; | ||
} | ||
async function writeAsync(file, object, options) { | ||
if (options?.ensureDir) { | ||
await fs_1.default.promises.mkdir(path_1.default.dirname(file), { recursive: true }); | ||
await node_fs_1.default.promises.mkdir(node_path_1.default.dirname(file), { recursive: true }); | ||
} | ||
@@ -210,2 +271,8 @@ const space = _getOption(options, 'space'); | ||
} | ||
function setSync(file, key, value, options) { | ||
// TODO: Consider implementing some kind of locking mechanism, but | ||
// it's not critical for our use case, so we'll leave it out for now | ||
const object = read(file, options); | ||
return write(file, { ...object, [key]: value }, options); | ||
} | ||
async function setAsync(file, key, value, options) { | ||
@@ -227,5 +294,18 @@ // TODO: Consider implementing some kind of locking mechanism, but | ||
} | ||
function merge(file, sources, options) { | ||
const object = read(file, options); | ||
if (Array.isArray(sources)) { | ||
Object.assign(object, ...sources); | ||
} | ||
else { | ||
Object.assign(object, sources); | ||
} | ||
return write(file, object, options); | ||
} | ||
async function deleteKeyAsync(file, key, options) { | ||
return deleteKeysAsync(file, [key], options); | ||
} | ||
function deleteKey(file, key, options) { | ||
return deleteKeys(file, [key], options); | ||
} | ||
async function deleteKeysAsync(file, keys, options) { | ||
@@ -246,2 +326,17 @@ const object = await readAsync(file, options); | ||
} | ||
function deleteKeys(file, keys, options) { | ||
const object = read(file, options); | ||
let didDelete = false; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
if (object.hasOwnProperty(key)) { | ||
delete object[key]; | ||
didDelete = true; | ||
} | ||
} | ||
if (didDelete) { | ||
return write(file, object, options); | ||
} | ||
return object; | ||
} | ||
async function rewriteAsync(file, options) { | ||
@@ -251,2 +346,5 @@ const object = await readAsync(file, options); | ||
} | ||
function rewrite(file, options) { | ||
return write(file, read(file, options), options); | ||
} | ||
function jsonParseErrorDefault(options = {}) { | ||
@@ -253,0 +351,0 @@ if (options.jsonParseErrorDefault === undefined) { |
{ | ||
"name": "@expo/json-file", | ||
"version": "8.3.4-canary-20240912-1059f85", | ||
"version": "8.4.0-canary-20240927-ab8a962", | ||
"description": "A module for reading, writing, and manipulating JSON files", | ||
@@ -41,3 +41,4 @@ "main": "build/JsonFile.js", | ||
"@types/write-file-atomic": "^2.1.1", | ||
"expo-module-scripts": "3.6.0-canary-20240912-1059f85" | ||
"expo-module-scripts": "3.6.0-canary-20240927-ab8a962", | ||
"memfs": "^3.2.0" | ||
}, | ||
@@ -47,3 +48,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "1059f8556047a3e02fa319e8b2459274571f4e6f" | ||
"gitHead": "ab8a962d2c3dddbda124a6bd88d24475831dae00" | ||
} |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
36059
509
0
5