-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate chosen checks from options
- Loading branch information
Showing
6 changed files
with
72 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,74 @@ | ||
import { explainVariable, typedObjectKeys } from '@voxpelli/typed-utils'; | ||
|
||
import { checkEngineVersions } from './check-engine-versions.js'; | ||
import { checkPackageVersions } from './check-package-versions.js'; | ||
|
||
/** | ||
* @throws {Error} | ||
* @typedef InstalledCheckResult | ||
* @property {string[]} errors | ||
* @property {string[]} warnings | ||
*/ | ||
|
||
/** @typedef {'engine' | 'version'} InstalledChecks */ | ||
|
||
/** @type {Record<InstalledChecks, true>} */ | ||
const checkTypeMap = { | ||
'engine': true, | ||
'version': true, | ||
}; | ||
|
||
const checkTypes = typedObjectKeys(checkTypeMap); | ||
|
||
/** | ||
* @typedef InstalledCheckOptions | ||
* @property {string[]|undefined} [ignores] | ||
* @property {boolean|undefined} [noDev] | ||
* @property {boolean|undefined} [strict] | ||
*/ | ||
|
||
/** | ||
* @param {InstalledChecks[]} checks | ||
* @param {import('./get-installed-data.js').PackageJsonLike} mainPackage | ||
* @param {import('./get-installed-data.js').InstalledDependencies} installedDependencies | ||
* @param {Omit<import('./installed-check.js').InstalledCheckOptions, 'path'>} options | ||
* @returns {Promise<import('./installed-check.js').InstalledCheckResult>} | ||
* @param {InstalledCheckOptions} options | ||
* @returns {Promise<InstalledCheckResult>} | ||
*/ | ||
export async function performInstalledCheck (mainPackage, installedDependencies, options) { | ||
if (!mainPackage) throw new TypeError('Expected mainPackage to be set'); | ||
if (!installedDependencies) throw new TypeError('Expected installedDependencies to be set'); | ||
if (!options) throw new TypeError('Expected options to be set'); | ||
|
||
const { | ||
engineCheck = false, | ||
engineIgnores = [], | ||
engineNoDev = false, | ||
strict = false, | ||
versionCheck = false, | ||
} = options; | ||
|
||
if (!engineCheck && !versionCheck) { | ||
throw new Error('Expected to run at least one check. Add engineCheck and/or versionCheck'); | ||
export async function performInstalledCheck (checks, mainPackage, installedDependencies, options) { | ||
if (!checks || !Array.isArray(checks)) { | ||
throw new TypeError('Expected a "checks" array, got: ' + explainVariable(checks)); | ||
} | ||
if (!mainPackage || typeof mainPackage !== 'object') { | ||
throw new TypeError('Expected a "mainPackage" object, got: ' + explainVariable(mainPackage)); | ||
} | ||
if (!installedDependencies || typeof installedDependencies !== 'object') { | ||
throw new TypeError('Expected a "installedDependencies" object, got: ' + explainVariable(installedDependencies)); | ||
} | ||
if (!options || typeof options !== 'object') { | ||
throw new TypeError('Expected a "options" object, got :' + explainVariable(options)); | ||
} | ||
|
||
let hasCheck = false; | ||
/** @type {string[]} */ | ||
let errors = []; | ||
/** @type {string[]} */ | ||
let warnings = []; | ||
|
||
const results = [ | ||
versionCheck && checkPackageVersions(mainPackage, installedDependencies), | ||
engineCheck && checkEngineVersions( | ||
mainPackage, | ||
installedDependencies, | ||
{ | ||
noDev: engineNoDev, | ||
ignore: engineIgnores, | ||
strict, | ||
} | ||
), | ||
checks.includes('version') && checkPackageVersions(mainPackage, installedDependencies), | ||
checks.includes('engine') && checkEngineVersions(mainPackage, installedDependencies, options), | ||
]; | ||
|
||
for (const result of results) { | ||
if (result) { | ||
hasCheck = true; | ||
errors = [...errors, ...result.errors]; | ||
warnings = [...warnings, ...result.warnings]; | ||
} | ||
} | ||
|
||
if (!hasCheck) { | ||
throw new Error('Expected to run at least one check. "checks" should include at least one of: ' + checkTypes.join(', ')); | ||
} | ||
|
||
return { errors, warnings }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters