Skip to content

Commit

Permalink
feat: return suggestions separate from errors
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Mar 22, 2024
1 parent 879fc64 commit 1723b7b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ Exists as a CLI as well: [installed-check](https://www.npmjs.com/package/install
```javascript
import { installedCheck } from 'installed-check-core';

const { errors } = await installedCheck(['version']);
const { errors, suggestions } = await installedCheck(['version']);

if (result.errors.length) {
console.error('Dependency errors: \n\n' + result.errors.join('\n') + '\n');
}
if (result.suggestions.length) {
console.error('Suggestions: \n\n' + result.suggestions.join('\n') + '\n');
}
```

### In CommonJS using dynamic [`import()`](https://nodejs.org/api/esm.html#import-expressions) expression
Expand Down Expand Up @@ -173,7 +176,11 @@ type InstalledCheckOptions = {
prefix?: string | undefined;
strict?: boolean | undefined;
};
type InstalledCheckResult = { errors: string[], warnings: string[] }
type InstalledCheckResult = {
errors: string[],
warnings: string[],
suggestions: string[],
}
```
#### Checks
Expand All @@ -194,7 +201,7 @@ type InstalledCheckResult = { errors: string[], warnings: string[] }
```javascript
import { installedCheck } from 'installed-check-core';
const { errors, warnings } = await installedCheck(['engine', 'version'], {
const { errors, warnings, suggestions } = await installedCheck(['engine', 'version'], {
cwd: 'path/to/module',
ignore: ['foo'],
noDev: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/check-package-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import semver from 'semver';
/**
* @param {import('./lookup-types.d.ts').PackageJsonLike} pkg
* @param {import('./lookup-types.d.ts').InstalledDependencies} installedDependencies
* @returns {{ errors: string[], warnings: string[] }}
* @returns {import('./perform-installed-check.js').InstalledCheckResult}
*/
export function checkPackageVersions (pkg, installedDependencies) {
/** @type {string[]} */
Expand Down Expand Up @@ -52,5 +52,5 @@ export function checkPackageVersions (pkg, installedDependencies) {
}
}

return { errors, warnings };
return { errors, warnings, suggestions: [] };
}
9 changes: 5 additions & 4 deletions lib/check-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { checkVersionRangeCollection } from './check-version-range.js';
* @param {string} key
* @param {import('./lookup-types.d.ts').InstalledDependencies} installed
* @param {import('./check-version-range.js').VersionRangesOptions} options
* @returns {{ errors: string[], warnings: string[] }}
* @returns {import('./perform-installed-check.js').InstalledCheckResult}
*/
export function checkVersions (pkg, key, installed, options) {
/** @type {string[]} */
const errors = [];
/** @type {string[]} */
const warnings = [];
/** @type {string[]} */
const summaries = [];
const suggestions = [];

const rangesResult = checkVersionRangeCollection(pkg, key, installed, options);

Expand All @@ -25,7 +25,7 @@ export function checkVersions (pkg, key, installed, options) {
}

if (!rangeResult.valid) {
summaries.push((
suggestions.push((
rangeResult.suggested
? `Combined "${name}" needs to be narrower: ${rangeResult.suggested}`
: `Incompatible combined "${name}" requirements.`
Expand All @@ -34,7 +34,8 @@ export function checkVersions (pkg, key, installed, options) {
}

return {
errors: [...new Set(errors), ...summaries],
errors: [...new Set(errors)],
warnings: [...new Set(warnings)],
suggestions,
};
}
4 changes: 4 additions & 0 deletions lib/installed-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export async function installedCheck (checks, lookupOptions, options) {
const errors = new Map();
/** @type {Map<string|ROOT, string[]>} */
const warnings = new Map();
/** @type {Map<string|ROOT, string[]>} */
const suggestions = new Map();

for await (const item of workspaceLookup({ ...lookupOptionsRest, path: cwd })) {
const result = await performInstalledCheck(checks, item.pkg, item.installed, options);
Expand All @@ -27,11 +29,13 @@ export async function installedCheck (checks, lookupOptions, options) {

errors.set(key, result.errors);
warnings.set(key, result.warnings);
suggestions.set(key, result.suggestions);
}

return {
errors: prefixNotes(errors, lookupOptions),
warnings: prefixNotes(warnings, lookupOptions),
suggestions: prefixNotes(suggestions, lookupOptions),
};
}

Expand Down
6 changes: 5 additions & 1 deletion lib/perform-installed-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { checkVersions } from './check-versions.js';
* @typedef InstalledCheckResult
* @property {string[]} errors
* @property {string[]} warnings
* @property {string[]} suggestions
*/

/** @typedef {'engine' | 'peer' | 'version'} InstalledChecks */
Expand Down Expand Up @@ -64,6 +65,8 @@ export async function performInstalledCheck (checks, pkg, installed, options) {
let errors = [];
/** @type {string[]} */
let warnings = [];
/** @type {string[]} */
let suggestions = [];

const results = [
checks.includes('version') && checkPackageVersions(pkg, installed),
Expand All @@ -80,12 +83,13 @@ export async function performInstalledCheck (checks, pkg, installed, options) {
hasCheck = true;
errors = [...errors, ...(prefix ? result.errors.map(note => prefix + ': ' + note) : result.errors)];
warnings = [...warnings, ...(prefix ? result.warnings.map(note => prefix + ': ' + note) : result.warnings)];
suggestions = [...suggestions, ...(prefix ? result.suggestions.map(note => prefix + ': ' + note) : result.suggestions)];
}
}

if (!hasCheck) {
throw new Error('Expected to run at least one check. "checks" should include at least one of: ' + checkTypes.join(', '));
}

return { errors, warnings };
return { errors, warnings, suggestions };
}
20 changes: 19 additions & 1 deletion test/installed-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('installedCheck()', () => {
})
.should.eventually.deep.equal({
errors: ["foo: Dependency is not installed. Can't check its version"],
suggestions: [],
warnings: ["foo: Dependency is not installed. Can't check its requirements"],
});
});
Expand All @@ -45,6 +46,7 @@ describe('installedCheck()', () => {
})
.should.eventually.deep.equal({
errors: [],
suggestions: [],
warnings: [],
});
});
Expand All @@ -55,6 +57,7 @@ describe('installedCheck()', () => {
})
.should.eventually.deep.equal({
errors: [],
suggestions: [],
warnings: [],
});
});
Expand All @@ -71,6 +74,8 @@ describe('installedCheck()', () => {
'invalid-module-version: Invalid version, expected a ^1.0.0',
'invalid-engine: Narrower "engines.node" is needed: >=10.0.0',
'invalid-engine: Narrower "engines.abc" is needed: >=1.0.0',
],
suggestions: [
'Combined "engines.node" needs to be narrower: >=10.0.0',
'Combined "engines.abc" needs to be narrower: >=1.0.0',
],
Expand Down Expand Up @@ -98,6 +103,8 @@ describe('installedCheck()', () => {
.should.eventually.deep.equal({
'errors': [
'foo: Narrower "engines.node" is needed: >=8.0.0',
],
suggestions: [
'Combined "engines.node" needs to be narrower: >=8.0.0',
],
warnings: [
Expand All @@ -113,6 +120,8 @@ describe('installedCheck()', () => {
.should.eventually.deep.equal({
'errors': [
'foo: Incompatible "engines.node" requirement: <6.0.0',
],
suggestions: [
'Incompatible combined "engines.node" requirements.',
],
warnings: [],
Expand All @@ -125,6 +134,7 @@ describe('installedCheck()', () => {
})
.should.eventually.deep.equal({
'errors': [],
suggestions: [],
warnings: [],
});
});
Expand All @@ -139,6 +149,8 @@ describe('installedCheck()', () => {
'errors': [
'invalid-engine: Narrower "engines.node" is needed: >=10.0.0',
'invalid-engine: Narrower "engines.abc" is needed: >=1.0.0',
],
suggestions: [
'Combined "engines.node" needs to be narrower: >=10.0.0',
'Combined "engines.abc" needs to be narrower: >=1.0.0',
],
Expand All @@ -159,6 +171,8 @@ describe('installedCheck()', () => {
.should.eventually.deep.equal({
'errors': [
'foo: Narrower "peerDependencies.bar" is needed: >=4.6.8',
],
suggestions: [
'Combined "peerDependencies.bar" needs to be narrower: >=4.6.8',
],
warnings: [
Expand All @@ -174,10 +188,12 @@ describe('installedCheck()', () => {
'errors': [
'root: foo: Narrower "engines.node" is needed: >=10.4.0',
'root: bar: Narrower "engines.node" is needed: >=12.0.0',
'root: Combined "engines.node" needs to be narrower: >=12.0.0',
'@voxpelli/workspace-a: foo: Narrower "engines.node" is needed: >=10.4.0',
'@voxpelli/workspace-a: bar: Narrower "engines.node" is needed: >=10.5.0',
'@voxpelli/workspace-a: abc: Narrower "engines.node" is needed: >=10.8.0',
],
suggestions: [
'root: Combined "engines.node" needs to be narrower: >=12.0.0',
'@voxpelli/workspace-a: Combined "engines.node" needs to be narrower: >=10.8.0',
],
warnings: [
Expand All @@ -195,6 +211,8 @@ describe('installedCheck()', () => {
'@voxpelli/workspace-a: foo: Narrower "engines.node" is needed: >=10.4.0',
'@voxpelli/workspace-a: bar: Narrower "engines.node" is needed: >=10.5.0',
'@voxpelli/workspace-a: abc: Narrower "engines.node" is needed: >=10.8.0',
],
suggestions: [
'@voxpelli/workspace-a: Combined "engines.node" needs to be narrower: >=10.8.0',
],
warnings: [
Expand Down

0 comments on commit 1723b7b

Please sign in to comment.