Skip to content

Commit 99a4f7d

Browse files
authored
Add back error messages when unable to detect workspaces (#586)
1 parent a4af705 commit 99a4f7d

File tree

3 files changed

+43
-69
lines changed

3 files changed

+43
-69
lines changed

lib/package.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ export class Package {
3838
if (this.packageJson.workspaces) {
3939
if (Array.isArray(this.packageJson.workspaces)) {
4040
return this.packageJson.workspaces;
41-
}
42-
if (this.packageJson.workspaces.packages) {
41+
} else if (this.packageJson.workspaces.packages) {
42+
if (!Array.isArray(this.packageJson.workspaces.packages)) {
43+
throw new TypeError(
44+
'package.json `workspaces.packages` is not a string array.'
45+
);
46+
}
4347
return this.packageJson.workspaces.packages;
48+
} else {
49+
throw new TypeError('package.json `workspaces` is not a string array.');
4450
}
4551
}
4652
return [];

lib/workspace.ts

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { join } from 'node:path';
2-
import { existsSync, readFileSync } from 'node:fs';
3-
import type { PackageJson } from 'type-fest';
42
import { globbySync } from 'globby';
53
import { Package } from './package.js';
64

@@ -11,6 +9,15 @@ export function getPackages(
119
ignorePaths: string[],
1210
ignorePathPatterns: RegExp[]
1311
): Package[] {
12+
// Check for some error cases first.
13+
if (!Package.exists(root)) {
14+
throw new Error('No package.json found at provided path.');
15+
}
16+
const package_ = new Package(root, root);
17+
if (package_.workspacePatterns.length === 0) {
18+
throw new Error('Package at provided path has no workspaces specified.');
19+
}
20+
1421
const packages = accumulatePackages(root, ['.']);
1522

1623
for (const ignoredPackage of ignorePackages) {
@@ -86,38 +93,6 @@ export function getPackages(
8693
return packages;
8794
}
8895

89-
export function getWorkspaces(root: string): string[] {
90-
const workspacePackageJsonPath = join(root, 'package.json');
91-
if (!existsSync(workspacePackageJsonPath)) {
92-
throw new Error('No package.json found at provided path.');
93-
}
94-
95-
const workspacePackageJson: PackageJson = JSON.parse(
96-
readFileSync(join(root, 'package.json'), 'utf8')
97-
);
98-
99-
if (!workspacePackageJson.workspaces) {
100-
throw new Error(
101-
'package.json at provided path does not specify `workspaces`.'
102-
);
103-
}
104-
105-
if (!Array.isArray(workspacePackageJson.workspaces)) {
106-
if (workspacePackageJson.workspaces.packages) {
107-
if (Array.isArray(workspacePackageJson.workspaces.packages)) {
108-
return workspacePackageJson.workspaces.packages;
109-
} else {
110-
throw new TypeError(
111-
'package.json `workspaces.packages` is not a string array.'
112-
);
113-
}
114-
}
115-
throw new TypeError('package.json `workspaces` is not a string array.');
116-
}
117-
118-
return workspacePackageJson.workspaces;
119-
}
120-
12196
// Expand workspace globs into concrete paths.
12297
function expandWorkspaces(root: string, workspacePatterns: string[]): string[] {
12398
return workspacePatterns.flatMap((workspace) => {

test/lib/workspace-test.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getPackages, getWorkspaces } from '../../lib/workspace.js';
1+
import { getPackages } from '../../lib/workspace.js';
22
import { join } from 'node:path';
33
import {
44
FIXTURE_PATH_NOT_A_WORKSPACE,
@@ -33,6 +33,26 @@ describe('Utils | workspace', function () {
3333
);
3434
});
3535

36+
it('behaves correctly with valid fixture for using nohoist', function () {
37+
expect(
38+
getPackages(FIXTURE_PATH_VALID_WITH_PACKAGES, [], [], [], []).map(
39+
(package_) => package_.path
40+
)
41+
).toStrictEqual(
42+
[
43+
'.',
44+
'@scope1/package1',
45+
'@scope1/package2',
46+
'@scope2/deps-only',
47+
'@scope2/dev-deps-only',
48+
'nested-scope/@nested-level/package',
49+
'foo1',
50+
'foo2',
51+
'package1',
52+
].map((path) => join(FIXTURE_PATH_VALID_WITH_PACKAGES, path))
53+
);
54+
});
55+
3656
it('filters out ignored package', function () {
3757
expect(
3858
getPackages(FIXTURE_PATH_VALID, ['@scope1/package1'], [], [], []).map(
@@ -179,61 +199,34 @@ describe('Utils | workspace', function () {
179199
)
180200
);
181201
});
182-
});
183-
184-
describe('#getWorkspaces', function () {
185-
it('behaves correctly with valid fixture', function () {
186-
expect(getWorkspaces(FIXTURE_PATH_VALID)).toMatchInlineSnapshot(`
187-
Array [
188-
"@scope1/*",
189-
"@scope2/*",
190-
"nested-scope/**",
191-
"foo*",
192-
"package1",
193-
]
194-
`);
195-
});
196-
197-
it('behaves correctly with valid fixture for using nohoist', function () {
198-
expect(getWorkspaces(FIXTURE_PATH_VALID_WITH_PACKAGES))
199-
.toMatchInlineSnapshot(`
200-
Array [
201-
"@scope1/*",
202-
"@scope2/*",
203-
"nested-scope/**",
204-
"foo*",
205-
"package1",
206-
]
207-
`);
208-
});
209202

210203
it('throws with fixture that has no package.json', function () {
211204
expect(() =>
212-
getWorkspaces(FIXTURE_PATH_NO_PACKAGE_JSON)
205+
getPackages(FIXTURE_PATH_NO_PACKAGE_JSON, [], [], [], [])
213206
).toThrowErrorMatchingInlineSnapshot(
214207
'"No package.json found at provided path."'
215208
);
216209
});
217210

218211
it('throws with fixture that does not have workspace specified', function () {
219212
expect(() =>
220-
getWorkspaces(FIXTURE_PATH_NOT_A_WORKSPACE)
213+
getPackages(FIXTURE_PATH_NOT_A_WORKSPACE, [], [], [], [])
221214
).toThrowErrorMatchingInlineSnapshot(
222-
'"package.json at provided path does not specify `workspaces`."'
215+
'"Package at provided path has no workspaces specified."'
223216
);
224217
});
225218

226219
it('throws with fixture that does not have workspace specified as array', function () {
227220
expect(() =>
228-
getWorkspaces(FIXTURE_PATH_WORKSPACE_NOT_AN_ARRAY)
221+
getPackages(FIXTURE_PATH_WORKSPACE_NOT_AN_ARRAY, [], [], [], [])
229222
).toThrowErrorMatchingInlineSnapshot(
230223
'"package.json `workspaces` is not a string array."'
231224
);
232225
});
233226

234227
it('throws with fixture that does not have workspace packages specified as array', function () {
235228
expect(() =>
236-
getWorkspaces(FIXTURE_PATH_WORKSPACE_PACKAGE_NOT_AN_ARRAY)
229+
getPackages(FIXTURE_PATH_WORKSPACE_PACKAGE_NOT_AN_ARRAY, [], [], [], [])
237230
).toThrowErrorMatchingInlineSnapshot(
238231
'"package.json `workspaces.packages` is not a string array."'
239232
);

0 commit comments

Comments
 (0)