Revert most changes

Signed-off-by: Gabriel Dugny <gabriel.dugny@believe.com>
This commit is contained in:
Gabriel Dugny
2026-02-22 17:00:06 +01:00
parent b2d9d78c27
commit 0e5fd0fd27
11 changed files with 18 additions and 152 deletions
-3
View File
@@ -23,9 +23,6 @@ export class ExitCodeError extends CustomErrorBase {
// @public
export function findPaths(searchDir: string): Paths;
// @public
export function getWorkspacesPatterns(pkgJson: unknown): string[];
// @public
export function isChildPath(base: string, path: string): boolean;
-1
View File
@@ -21,7 +21,6 @@
*/
export { findPaths, BACKSTAGE_JSON } from './paths';
export { getWorkspacesPatterns } from './workspaces';
export { isChildPath } from './isChildPath';
export type { Paths, ResolveFunc } from './paths';
export { bootstrapEnvProxyAgents } from './proxyBootstrap';
+1 -2
View File
@@ -16,7 +16,6 @@
import fs from 'node:fs';
import { dirname, resolve as resolvePath } from 'node:path';
import { getWorkspacesPatterns } from './workspaces';
/**
* A function that takes a set of path fragments and resolves them into a
@@ -142,7 +141,7 @@ export function findPaths(searchDir: string): Paths {
try {
const content = fs.readFileSync(path, 'utf8');
const data = JSON.parse(content);
return getWorkspacesPatterns(data).length > 0;
return Boolean(data.workspaces);
} catch (error) {
throw new Error(
`Failed to parse package.json file while searching for root, ${error}`,
@@ -1,69 +0,0 @@
/*
* Copyright 2026 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getWorkspacesPatterns } from './workspaces';
describe('getWorkspacesPatterns', () => {
it('returns empty array when pkgJson has no workspaces key', () => {
expect(getWorkspacesPatterns({ name: 'pkg' })).toEqual([]);
});
it('returns patterns when workspaces is array form', () => {
const patterns = ['packages/*', 'apps/*'];
expect(getWorkspacesPatterns({ workspaces: patterns })).toEqual(patterns);
});
it('returns patterns when workspaces is object form with packages', () => {
const patterns = ['packages/*'];
expect(
getWorkspacesPatterns({ workspaces: { packages: patterns } }),
).toEqual(patterns);
});
it('returns empty array when workspaces is object without packages array', () => {
expect(getWorkspacesPatterns({ workspaces: {} })).toEqual([]);
expect(getWorkspacesPatterns({ workspaces: { noMatch: [] } })).toEqual([]);
});
it('throws when pkgJson is not an object', () => {
expect(() => getWorkspacesPatterns(null)).toThrow(
'pkgJson must be an object',
);
expect(() => getWorkspacesPatterns('string')).toThrow(
'pkgJson must be an object',
);
expect(() => getWorkspacesPatterns(42)).toThrow(
'pkgJson must be an object',
);
});
it('throws when workspaces array contains non-strings', () => {
expect(() => getWorkspacesPatterns({ workspaces: ['a', 1, 'b'] })).toThrow(
'must be an array of strings',
);
expect(() => getWorkspacesPatterns({ workspaces: [null] })).toThrow(
'must be an array of strings',
);
});
it('throws when workspaces.packages contains non-strings', () => {
expect(() =>
getWorkspacesPatterns({
workspaces: { packages: ['a', 1] },
}),
).toThrow('must be an array of strings');
});
});
-63
View File
@@ -1,63 +0,0 @@
/*
* Copyright 2026 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function assertStringArray(
value: unknown,
field: string,
): asserts value is string[] {
if (!Array.isArray(value)) {
throw new Error(`${field} must be an array`);
}
if (!value.every((item): item is string => typeof item === 'string')) {
throw new Error(`${field} must be an array of strings`);
}
}
/**
* Gets the workspaces patterns from a package.json object.
*
* Supports both the array form (`"workspaces": ["packages/*"]`) and the object
* form (`"workspaces": { "packages": ["packages/*"] }`) from npm/yarn.
*
* @param pkgJson - The package.json object. Type as `unknown` so callers must pass parsed JSON; an object type check is performed.
* @returns The workspaces patterns (globs not resolved). Empty array if none.
* @throws Error if `pkgJson` is not an object or workspaces/packages contain non-strings.
* @public
*/
export function getWorkspacesPatterns(pkgJson: unknown): string[] {
if (typeof pkgJson !== 'object' || pkgJson === null) {
throw new Error('pkgJson must be an object');
}
if (!('workspaces' in pkgJson)) {
return [];
}
const workspaces = pkgJson.workspaces;
if (Array.isArray(workspaces)) {
assertStringArray(workspaces, 'pkgJson.workspaces');
return workspaces;
}
if (
typeof workspaces === 'object' &&
workspaces !== null &&
'packages' in workspaces &&
Array.isArray(workspaces.packages)
) {
assertStringArray(workspaces.packages, 'pkgJson.workspaces.packages');
return workspaces.packages;
}
return [];
}