Skip to content

Commit

Permalink
feat: Make linkable throw when a glob in .linkable.json leads to no p…
Browse files Browse the repository at this point in the history
…ackage.json
  • Loading branch information
Iku-turso committed Sep 12, 2023
1 parent 38cd379 commit be952b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
21 changes: 19 additions & 2 deletions packages/linkable/src/create-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ describe('create-links', () => {

await globMock.resolve([
'/some-directory/some-monorepo/packages/some-other-directory/some-module/package.json',
'/some-directory/some-monorepo/packages/some-other-directory/some-other-module/package.json',
]);

await globMock.resolve([]);
await globMock.resolve([
'/some-directory/some-monorepo/packages/some-other-directory/some-other-module/package.json',
]);
});

it('reads contents of package.jsons', () => {
Expand All @@ -180,6 +181,22 @@ describe('create-links', () => {
]);
});
});

describe('when one of discoveries resolves with nothing', () => {
beforeEach(async () => {
readJsonFileMock.mockClear();

await globMock.resolve([]);

globMock.resolve(['irrelevant']);
});

it('throws about bad path', () => {
return expect(actualPromise).rejects.toThrow(
'Tried to linkable-link: "../some-monorepo/packages/**/*/package.json" from "/some-directory/some-project/.linkable.json", but no package.jsons were found.',
);
});
});
});

describe('when config file resolves with module paths', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getInjectable } from '@ogre-tools/injectable';
import { workingDirectoryInjectable } from '../shared/working-directory.injectable';
import type { Config } from '../config/get-config.injectable';
import { pipeline } from '@ogre-tools/fp';
import { map, flatten } from 'lodash/fp';
import { map, flatMap, tap } from 'lodash/fp';
import { globInjectable } from '../shared/fs/glob.injectable';
import { awaitAll } from '../await-all';

Expand All @@ -17,17 +17,41 @@ export const getPackageJsonPathsInjectable = getInjectable({
pipeline(
config,

map(modulePathGlob =>
glob(`${modulePathGlob}/package.json`, {
cwd: workingDirectory,
ignore: ['**/node_modules/**/*'],
absolute: true,
}),
),
map(async modulePathGlob => {
const globString = `${modulePathGlob}/package.json`;

return {
globString: globString,

packageJsonPaths: await glob(globString, {
cwd: workingDirectory,
ignore: ['**/node_modules/**/*'],
absolute: true,
}),
};
}),

awaitAll,

flatten,
tap(checkForGlobMatchesFor(workingDirectory)),

flatMap('packageJsonPaths'),
);
},
});

const checkForGlobMatchesFor =
(workingDirectory: string) =>
(results: { globString: string; packageJsonPaths: any[] }[]) => {
const globStringsForNoMatches = results
.filter(x => !x.packageJsonPaths.length)
.map(x => x.globString);

if (globStringsForNoMatches.length) {
throw new Error(
`Tried to linkable-link: "${globStringsForNoMatches.join(
'", "',
)}" from "${workingDirectory}/.linkable.json", but no package.jsons were found.`,
);
}
};

0 comments on commit be952b6

Please sign in to comment.