export const PACKAGE_NAME = 'react-native-nitro-chucker' export type NitroChuckerPluginProps = { enabled?: boolean } type Json = Record // Add or remove PACKAGE_NAME from obj[key], pruning the key when the list empties. function applyToList(obj: Json, key: string, shouldExclude: boolean): void { const current = Array.isArray(obj[key]) ? obj[key].filter((x: unknown): x is string => typeof x === 'string') : [] if (shouldExclude) { obj[key] = current.includes(PACKAGE_NAME) ? current : [...current, PACKAGE_NAME] return } const filtered = current.filter((x: string) => x !== PACKAGE_NAME) if (filtered.length) obj[key] = filtered else delete obj[key] } export function applyExclusion( pkg: Json, props?: NitroChuckerPluginProps ): Json { const shouldExclude = props?.enabled === false // package.json is pure JSON: clone via JSON to avoid mutating the input. const next: Json = JSON.parse(JSON.stringify(pkg)) next.expo ??= {} next.expo.autolinking ??= {} const al: Json = next.expo.autolinking al.ios ??= {} al.android ??= {} applyToList(al, 'exclude', shouldExclude) applyToList(al.ios, 'exclude', shouldExclude) applyToList(al.android, 'exclude', shouldExclude) if (!shouldExclude) { if (Object.keys(al.ios).length === 0) delete al.ios if (Object.keys(al.android).length === 0) delete al.android if (Object.keys(al).length === 0) delete next.expo.autolinking if (Object.keys(next.expo).length === 0) delete next.expo } return next }