UNPKG

syncpack

Version:

Consistent dependency versions in large JavaScript Monorepos

28 lines (27 loc) 1.55 kB
import { Effect, Option, pipe } from 'effect'; import { isNonEmptyObject } from 'tightrope/guard/is-non-empty-object.js'; import { get } from '../lib/get.js'; import { DELETE } from '../version-group/lib/delete.js'; const getOptionOfNonEmptyObject = Option.liftPredicate((isNonEmptyObject)); export class VersionsByNameStrategy { _tag = 'versionsByName'; name; path; constructor(name, path) { this.name = name; this.path = path; } read(file) { return pipe(get(file.jsonFile.contents, ...this.path.split('.')), Effect.flatMap(value => getOptionOfNonEmptyObject(value)), Effect.map(obj => Object.entries(obj)), Effect.tapError(() => Effect.logDebug(`VersionsByNameStrategy#${this.name} found nothing at <${file.jsonFile.shortPath}>.${this.path}`)), // if value is invalid, default to empty Effect.catchAll(() => Effect.succeed([]))); } write(file, [name, version]) { const nextValue = version === DELETE ? undefined : version; const fullPath = this.path.split('.'); return pipe(get(file.jsonFile.contents, ...fullPath), Effect.flatMap(getOptionOfNonEmptyObject), Effect.flatMap(parent => Effect.try(() => { parent[name] = nextValue; file.applyEdit(fullPath.concat(name), nextValue); })), Effect.tapError(() => Effect.logDebug(`strategy ${this._tag} with name ${this.name} failed to write to <${file.jsonFile.shortPath}>.${this.path}.${name}`)), Effect.catchAll(() => Effect.succeed(file)), Effect.map(() => file)); } }