node-scripts-docs
Version:
Generate documentation for your `package.json` scripts.
100 lines • 4.33 kB
JavaScript
import { mustExist } from "@oliversalzburg/js-utils/data/nil.js";
import { isDefaultDescription } from "./FragmentRenderer.js";
/**
* Validates the state of the stores in the environment.
*/
export class Validator {
/**
* The fragments to validate.
*/
fragmentStore;
/**
* The stored metadata.
*/
metadata;
/**
* The current metadata.
*/
metadataFromScan;
/**
* Constructs a new validator.
* @param metadata - The stored metadata.
* @param metadataFromScan - The current metadata.
* @param fragmentStore - The fragments to validate.
*/
constructor(metadata, metadataFromScan, fragmentStore) {
this.metadata = metadata;
this.metadataFromScan = metadataFromScan;
this.fragmentStore = fragmentStore;
}
/**
* Validate and report the results.
* @param withLocals - Include project-local scripts?
* @returns A validation report.
*/
generateReport(withLocals = false) {
const report = {
changedFragments: new Set(),
corruptedMetadataRecords: new Set(),
missingFragments: new Set(),
newScripts: new Set(),
obsoleteFragments: new Set(),
pendingDocumentation: new Set(),
unchangedFragments: new Set(),
};
// Find fragments in the fragment store for which the corresponding script has been removed.
const obsoleteFragments = new Set();
for (const fragment of this.fragmentStore.fragments.values()) {
obsoleteFragments.add(fragment);
}
for (const scriptMeta of this.metadataFromScan.scripts) {
const fragment = this.fragmentStore.fragments.get(scriptMeta.scriptName);
if (fragment) {
obsoleteFragments.delete(fragment);
}
}
report.obsoleteFragments = obsoleteFragments;
for (const scriptMeta of this.metadataFromScan.scripts) {
if (!scriptMeta.isGlobal && !scriptMeta.isRootManifest && !withLocals) {
continue;
}
const cachedMeta = this.metadata.scripts.find(candidate => candidate.projectName === scriptMeta.projectName &&
candidate.scriptName === scriptMeta.scriptName);
if (!cachedMeta) {
report.newScripts.add(scriptMeta);
continue;
}
// If a script is not in the fragment store, it is treated as missing.
if (!this.fragmentStore.fragments.has(scriptMeta.scriptName)) {
report.missingFragments.add(scriptMeta);
continue;
}
const fragment = mustExist(this.fragmentStore.fragments.get(scriptMeta.scriptName));
// If the fragment has the default description, it was written by an earlier run.
// If the metadata contains no description, the documentation is still pending.
if (isDefaultDescription(fragment.descriptionMarkdown) &&
(!cachedMeta.description || isDefaultDescription(cachedMeta.description))) {
report.unchangedFragments.add(scriptMeta);
report.pendingDocumentation.add(scriptMeta);
}
// If the fragment has an authored description, it was well in production.
// If the metadata contains no description, it seems to be corrupted.
if (!isDefaultDescription(fragment.descriptionMarkdown) && !cachedMeta.description) {
report.corruptedMetadataRecords.add(scriptMeta);
}
// If a non-default description is authored in the fragment and there's a description in the metdata,
// this should be changed for changes.
if (!isDefaultDescription(fragment.descriptionMarkdown) && cachedMeta.description) {
// If the description in the fragment is identical to the metadata, it's treated as unchanged.
if (fragment.descriptionMarkdown === cachedMeta.description) {
report.unchangedFragments.add(scriptMeta);
}
else {
report.changedFragments.add(scriptMeta);
}
}
}
return report;
}
}
//# sourceMappingURL=Validator.js.map