Skip to content

Commit

Permalink
feat: return workspace success status (#127)
Browse files Browse the repository at this point in the history
Enables callers listing all found workspaces and their success or failure
  • Loading branch information
voxpelli authored Apr 4, 2024
1 parent e2b600b commit 9950e8a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/installed-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { performInstalledCheck } from './perform-installed-check.js';

/** @typedef {Omit<import('list-installed').WorkspaceLookupOptions, 'path'> & { cwd?: string|undefined }} LookupOptions */

const ROOT = Symbol('workspace root');
export const ROOT = Symbol('workspace root');

/** @typedef {{ [workspace: string]: boolean; [ROOT]?: boolean; }} WorkspaceSuccess */

/**
* @param {import('./perform-installed-check.js').InstalledChecks[]} checks
* @param {LookupOptions} [lookupOptions]
* @param {import('./perform-installed-check.js').InstalledCheckOptions} [options]
* @returns {Promise<import('./perform-installed-check.js').InstalledCheckResult>}
* @returns {Promise<import('./perform-installed-check.js').InstalledCheckResult & { workspaceSuccess: WorkspaceSuccess }>}
*/
export async function installedCheck (checks, lookupOptions, options) {
const { cwd = '.', ...lookupOptionsRest } = lookupOptions || {};
Expand All @@ -21,7 +23,10 @@ export async function installedCheck (checks, lookupOptions, options) {
const warnings = new Map();
/** @type {Map<string|ROOT, string[]>} */
const suggestions = new Map();
/** @type {WorkspaceSuccess} */
const workspaceSuccess = {};

// TODO: Optionally use a readWorkspaces() + npm API lookup instead
for await (const item of workspaceLookup({ ...lookupOptionsRest, path: cwd })) {
const result = await performInstalledCheck(checks, item.pkg, item.installed, options);

Expand All @@ -30,12 +35,15 @@ export async function installedCheck (checks, lookupOptions, options) {
errors.set(key, result.errors);
warnings.set(key, result.warnings);
suggestions.set(key, result.suggestions);

workspaceSuccess[key] = result.errors.length === 0;
}

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

Expand Down
18 changes: 17 additions & 1 deletion test/installed-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { join } from 'desm';

import { installedCheck } from '../lib/installed-check.js';
import { ROOT, installedCheck } from '../lib/installed-check.js';

chai.use(chaiAsPromised);
chai.should();
Expand Down Expand Up @@ -35,6 +35,7 @@ describe('installedCheck()', () => {
errors: ["foo: Dependency is not installed. Can't check its version"],
suggestions: [],
warnings: ["foo: Dependency is not installed. Can't check its requirements"],
workspaceSuccess: { [ROOT]: false },
});
});
});
Expand All @@ -48,6 +49,7 @@ describe('installedCheck()', () => {
errors: [],
suggestions: [],
warnings: [],
workspaceSuccess: { [ROOT]: true },
});
});

Expand All @@ -59,6 +61,7 @@ describe('installedCheck()', () => {
errors: [],
suggestions: [],
warnings: [],
workspaceSuccess: { [ROOT]: true },
});
});

Expand Down Expand Up @@ -93,6 +96,7 @@ describe('installedCheck()', () => {
'invalid-module-version: Missing "engines.bar"',
'invalid-module-version: Missing "engines.abc"',
],
workspaceSuccess: { [ROOT]: false },
});
});

Expand All @@ -110,6 +114,7 @@ describe('installedCheck()', () => {
warnings: [
'Missing "engines.node" in main package',
],
workspaceSuccess: { [ROOT]: false },
});
});

Expand All @@ -125,6 +130,7 @@ describe('installedCheck()', () => {
'Incompatible combined "engines.node" requirements.',
],
warnings: [],
workspaceSuccess: { [ROOT]: false },
});
});

Expand All @@ -136,6 +142,7 @@ describe('installedCheck()', () => {
'errors': [],
suggestions: [],
warnings: [],
workspaceSuccess: { [ROOT]: true },
});
});

Expand All @@ -161,6 +168,7 @@ describe('installedCheck()', () => {
'invalid-module-version: Missing "engines.bar"',
'invalid-module-version: Missing "engines.abc"',
],
workspaceSuccess: { [ROOT]: false },
});
});

Expand All @@ -177,6 +185,7 @@ describe('installedCheck()', () => {
],
warnings: [
],
workspaceSuccess: { [ROOT]: false },
});
});

Expand All @@ -198,6 +207,10 @@ describe('installedCheck()', () => {
],
warnings: [
],
workspaceSuccess: {
[ROOT]: false,
'@voxpelli/workspace-a': false,
},
});
});

Expand All @@ -217,6 +230,9 @@ describe('installedCheck()', () => {
],
warnings: [
],
workspaceSuccess: {
'@voxpelli/workspace-a': false,
},
});
});
});
Expand Down

0 comments on commit 9950e8a

Please sign in to comment.