Skip to content

Commit

Permalink
fix: handle nohoist-packages without error
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuel Kluge authored and herschel666 committed Apr 19, 2021
1 parent 6ad0515 commit df8d647
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/__tests__/workspaces.test.ts
Expand Up @@ -101,7 +101,7 @@ function globSyncMock(pattern: string): string[] {
'/some/directory/hops/packages/package-2/package.json',
];
case 'nohoist/packages/*/package.json':
return ['/some/directory/hops/packages/pkg-1/package.json'];
return ['/some/directory/nohoist/packages/pkg-1/package.json'];
}
return [];
}
Expand Down Expand Up @@ -151,6 +151,10 @@ describe('collectWorkspaces', () => {
url: 'https://github.com/xing/hops.git',
directory: 'hops',
}),
partialRepositoryConfig({
url: 'https://github.com/xing/nohoist.git',
directory: 'nohoist',
}),
],
})
);
Expand All @@ -171,6 +175,12 @@ describe('collectWorkspaces', () => {
manifest: hopsPackage2Manifest,
},
],
[
{
path: '/some/directory/nohoist/packages/pkg-1/package.json',
manifest: nohoistPkg1,
},
],
]);
});
});
Expand Down
20 changes: 13 additions & 7 deletions src/workspaces.ts
Expand Up @@ -12,6 +12,16 @@ const hasKey = <K extends string>(
return typeof object === 'object' && object !== null && key in object;
};

const getWorkspacesPackages = ({ workspaces }: PackageJSON): string[] => {
const packages = Array.isArray(workspaces)
? workspaces
: workspaces && Array.isArray(workspaces.packages)
? workspaces.packages
: [];

return packages;
};

export interface WorkspacesConfig extends Config {
repositories: (Config['repositories'][0] & {
manifest: PackageJSON;
Expand All @@ -27,11 +37,12 @@ export function collectWorkspaces(config: Config): WorkspacesConfig {
'utf8'
)
);
const packages = getWorkspacesPackages(manifest);

return {
...repo,
manifest,
packages: (manifest.workspaces || [])
packages: packages
.map((pattern: string) => {
return glob
.sync(join(repo.directory, pattern, 'package.json'), {
Expand All @@ -57,12 +68,7 @@ export function collectWorkspaces(config: Config): WorkspacesConfig {

export function createRootManifest(config: WorkspacesConfig): PackageJSON {
const workspaces = config.repositories.reduce((accumulator, repository) => {
const { workspaces } = repository.manifest;
const packages = Array.isArray(workspaces)
? workspaces
: workspaces && Array.isArray(workspaces.packages)
? workspaces.packages
: [];
const packages = getWorkspacesPackages(repository.manifest);

return [
...accumulator,
Expand Down

0 comments on commit df8d647

Please sign in to comment.