syncpack
Version:
Consistent dependency versions in large JavaScript Monorepos
55 lines (54 loc) • 2.29 kB
JavaScript
import { Effect, pipe } from 'effect';
import { Specifier } from './index.js';
import { NonSemverError } from './lib/non-semver-error.js';
import { parseSpecifier } from './lib/parse-specifier.js';
export class BaseSpecifier {
/** should be overridden by sub classes */
_tag = 'Base';
/** should be overridden by sub classes */
name = 'base';
/** The raw semver/workspace:/git etc version value */
raw;
/**
* A `Specifier` describes the version specifier (eg "^1.4.4") of a given
* instance.
*
* Initially there will be a `Specifier` which describes the version as it is
* on disk, but other `Specifier`s will need to be created which reflect what
* the version should be once fixed – first for the instance's version group
* and second for its semver group.
*
* The intermediate and final variants of `Specifier` could differ along the
* way and whether we're linting the current state of the monorepo or
* determining whether possible fixes will ultimately still have a valid
* version and range, each one has a reference back to the original `Instance`
* for writing back to it when we do finally commit our fixes once verified.
*/
instance;
constructor(data) {
this.raw = data.raw;
this.instance = data.instance;
}
/**
* Parse the raw version specifier using
* https://github.com/npm/npm-package-arg
*/
parse() {
const name = this.instance.name;
const raw = this.raw;
const packageJsonFile = this.instance.packageJsonFile;
return pipe(Effect.try(() => parseSpecifier(name, raw, packageJsonFile)), Effect.tapError(() => Effect.logError(`parseSpecifier threw on ${name}@${raw} at ${packageJsonFile.jsonFile.shortPath}`)));
}
/** Return the version portion if it is valid semver */
getSemver() {
return NonSemverError.asEffect(this);
}
/** Get a new `Specifier` from the given semver version applied to this one */
setSemver(_version) {
return NonSemverError.asEffect(this);
}
/** Apply the given specifier to a new one with this instance bound to it */
replaceWith(specifier) {
return Specifier.create(this.instance, specifier.raw);
}
}