syncpack
Version:
Consistent dependency versions in large JavaScript Monorepos
46 lines (45 loc) • 2.46 kB
JavaScript
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';
import { getNonEmptyStringProp } from './lib/get-non-empty-string-prop.js';
const getOptionOfNonEmptyObject = Option.liftPredicate((isNonEmptyObject));
export class UnnamedVersionStringStrategy {
_tag = 'version';
name;
path;
constructor(name, path) {
this.name = name;
this.path = path;
}
read(file) {
return pipe(
// get version prop
getNonEmptyStringProp(this.path, file),
// if it is a non empty string, we can read it
Effect.map((version) => {
const name = this.path.split('.').slice(-1).join('');
return [[name, version]];
}), Effect.tapError(() => Effect.logDebug(`UnnamedVersionStringStrategy#${this.name} found nothing at <${file.jsonFile.shortPath}>.${this.path}`)),
// if value is invalid, default to empty
Effect.catchAll(() => Effect.succeed([])));
}
write(file, [, version]) {
const { contents } = file.jsonFile;
const isNestedPath = this.path.includes('.');
const nextValue = version === DELETE ? undefined : version;
if (isNestedPath) {
const fullPath = this.path.split('.');
const pathToParent = fullPath.slice(0, fullPath.length - 1).join('.');
const key = fullPath.slice(-1).join('');
return pipe(get(contents, ...pathToParent.split('.')), Effect.flatMap(getOptionOfNonEmptyObject), Effect.flatMap(parent => Effect.try(() => {
parent[key] = nextValue;
file.applyEdit(fullPath, nextValue);
})), Effect.tapError(() => Effect.logDebug(`strategy ${this._tag} with name ${this.name} failed to write to <${file.jsonFile.shortPath}>.${this.path}`)), Effect.catchAll(() => Effect.succeed(file)), Effect.map(() => file));
}
return pipe(getOptionOfNonEmptyObject(contents), Effect.flatMap(parent => Effect.try(() => {
parent[this.path] = nextValue;
file.applyEdit([this.path], nextValue);
})), Effect.tapError(() => Effect.logDebug(`strategy ${this._tag} with name ${this.name} failed to write to <${file.jsonFile.shortPath}>.${this.path}`)), Effect.catchAll(() => Effect.succeed(file)), Effect.map(() => file));
}
}