(dir: string, target: Target, spec: string, force: boolean, dep: PatchDeps)
| 343 | } |
| 344 | |
| 345 | async function patchOne(dir: string, target: Target, spec: string, force: boolean, dep: PatchDeps): Promise<PatchOne> { |
| 346 | const name = patchName(target.kind) |
| 347 | await using _ = await Flock.acquire(`plug-config:${Filesystem.resolve(path.join(dir, name))}`) |
| 348 | |
| 349 | const files = dep.files(dir, name) |
| 350 | let cfg = files[0] |
| 351 | for (const file of files) { |
| 352 | if (!(await dep.exists(file))) continue |
| 353 | cfg = file |
| 354 | break |
| 355 | } |
| 356 | |
| 357 | const src = await dep.readText(cfg).catch((err: NodeJS.ErrnoException) => { |
| 358 | if (err.code === "ENOENT") return "{}" |
| 359 | return err |
| 360 | }) |
| 361 | if (src instanceof Error) { |
| 362 | return { |
| 363 | ok: false, |
| 364 | code: "patch_failed", |
| 365 | kind: target.kind, |
| 366 | error: src, |
| 367 | } |
| 368 | } |
| 369 | const text = src.trim() ? src : "{}" |
| 370 | |
| 371 | const errs: JsoncParseError[] = [] |
| 372 | const data = parseJsonc(text, errs, { allowTrailingComma: true }) |
| 373 | if (errs.length) { |
| 374 | const err = errs[0] |
| 375 | const lines = text.substring(0, err.offset).split("\n") |
| 376 | return { |
| 377 | ok: false, |
| 378 | code: "invalid_json", |
| 379 | kind: target.kind, |
| 380 | file: cfg, |
| 381 | line: lines.length, |
| 382 | col: lines[lines.length - 1].length + 1, |
| 383 | parse: printParseErrorCode(err.error), |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | const list = pluginList(data) |
| 388 | const item = target.opts ? ([spec, target.opts] as const) : spec |
| 389 | const out = patchPluginList(text, list, spec, item, force) |
| 390 | if (out.mode === "noop") { |
| 391 | return { |
| 392 | ok: true, |
| 393 | item: { |
| 394 | kind: target.kind, |
| 395 | mode: out.mode, |
| 396 | file: cfg, |
| 397 | }, |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | const write = await dep.write(cfg, out.text).catch((error: unknown) => error) |
| 402 | if (write instanceof Error) { |
no test coverage detected