{
  "version": 3,
  "sources": ["../../../../src/packages/plugin-commands-patching/stateFile.ts"],
  "sourcesContent": ["import fs from 'node:fs';\nimport path from 'node:path';\nimport util from 'node:util';\n\nexport type EditDir = string & { __brand: 'patch-edit-dir' };\n\nexport type EditDirState = {\n  patchedPkg: string;\n  applyToAll: boolean;\n};\n\nexport type State = Record<EditDir, EditDirState>;\n\nexport type EditDirKeyInput = {\n  editDir: string;\n};\n\nfunction createEditDirKey(opts: EditDirKeyInput): EditDir {\n  return opts.editDir as EditDir;\n}\n\nexport interface ReadEditDirStateOptions extends EditDirKeyInput {\n  modulesDir: string;\n}\n\nexport function readEditDirState(\n  opts: ReadEditDirStateOptions\n): EditDirState | undefined {\n  const state = readStateFile(opts.modulesDir);\n\n  if (!state) {\n    return undefined;\n  }\n\n  const key = createEditDirKey(opts);\n\n  return state[key];\n}\n\nexport interface WriteEditDirStateOptions\n  extends ReadEditDirStateOptions,\n    EditDirState {}\n\nexport function writeEditDirState(opts: WriteEditDirStateOptions): void {\n  modifyStateFile(opts.modulesDir, (state: State): void => {\n    const key = createEditDirKey(opts);\n\n    state[key] = {\n      patchedPkg: opts.patchedPkg,\n      applyToAll: opts.applyToAll,\n    };\n  });\n}\n\nfunction modifyStateFile(\n  modulesDir: string,\n  modifyState: (state: State) => void\n): void {\n  const filePath = getStateFilePath(modulesDir);\n\n  let state = readStateFile(modulesDir);\n\n  if (!state) {\n    state = {};\n\n    fs.mkdirSync(path.dirname(filePath), { recursive: true });\n  }\n\n  modifyState(state);\n\n  fs.writeFileSync(filePath, JSON.stringify(state, undefined, 2));\n}\n\nfunction readStateFile(modulesDir: string): State | undefined {\n  let fileContent: string;\n\n  try {\n    fileContent = fs.readFileSync(getStateFilePath(modulesDir), 'utf-8');\n  } catch (err: unknown) {\n    if (\n      util.types.isNativeError(err) &&\n      'code' in err &&\n      err.code === 'ENOENT'\n    ) {\n      return undefined;\n    }\n\n    throw err;\n  }\n\n  // TODO: valibot schema\n  return JSON.parse(fileContent) as State;\n}\n\nfunction getStateFilePath(modulesDir: string): string {\n  return path.join(modulesDir, '.pnpm_patches', 'state.json');\n}\n"],
  "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,UAAU;AAejB,SAAS,iBAAiB,MAAgC;AACxD,SAAO,KAAK;AACd;AAMO,SAAS,iBACd,MAC0B;AAC1B,QAAM,QAAQ,cAAc,KAAK,UAAU;AAE3C,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,iBAAiB,IAAI;AAEjC,SAAO,MAAM,GAAG;AAClB;AAMO,SAAS,kBAAkB,MAAsC;AACtE,kBAAgB,KAAK,YAAY,CAAC,UAAuB;AACvD,UAAM,MAAM,iBAAiB,IAAI;AAEjC,UAAM,GAAG,IAAI;AAAA,MACX,YAAY,KAAK;AAAA,MACjB,YAAY,KAAK;AAAA,IACnB;AAAA,EACF,CAAC;AACH;AAEA,SAAS,gBACP,YACA,aACM;AACN,QAAM,WAAW,iBAAiB,UAAU;AAE5C,MAAI,QAAQ,cAAc,UAAU;AAEpC,MAAI,CAAC,OAAO;AACV,YAAQ,CAAC;AAET,OAAG,UAAU,KAAK,QAAQ,QAAQ,GAAG,EAAE,WAAW,KAAK,CAAC;AAAA,EAC1D;AAEA,cAAY,KAAK;AAEjB,KAAG,cAAc,UAAU,KAAK,UAAU,OAAO,QAAW,CAAC,CAAC;AAChE;AAEA,SAAS,cAAc,YAAuC;AAC5D,MAAI;AAEJ,MAAI;AACF,kBAAc,GAAG,aAAa,iBAAiB,UAAU,GAAG,OAAO;AAAA,EACrE,SAAS,KAAc;AACrB,QACE,KAAK,MAAM,cAAc,GAAG,KAC5B,UAAU,OACV,IAAI,SAAS,UACb;AACA,aAAO;AAAA,IACT;AAEA,UAAM;AAAA,EACR;AAGA,SAAO,KAAK,MAAM,WAAW;AAC/B;AAEA,SAAS,iBAAiB,YAA4B;AACpD,SAAO,KAAK,KAAK,YAAY,iBAAiB,YAAY;AAC5D;",
  "names": []
}
