Skip to content

Commit

Permalink
feat: Add support for globs in the linkable configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jansav committed Apr 25, 2023
1 parent 78fe22e commit 62144eb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { getInjectable } from '@ogre-tools/injectable';
import { resolvePathInjectable } from '../shared/path/resolve-path.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 { globInjectable } from '../shared/fs/glob.injectable';
import { awaitAll } from '../await-all';

export const getPackageJsonPathsInjectable = getInjectable({
id: 'get-package-json-paths',

instantiate: di => {
const resolvePath = di.inject(resolvePathInjectable);
const workingDirectory = di.inject(workingDirectoryInjectable);
const glob = di.inject(globInjectable);

return (config: Config) =>
config.map((linkPath: string) =>
resolvePath(workingDirectory, linkPath, 'package.json'),
pipeline(
config,

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

awaitAll,

flatten,
);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const getPackageJsonsInjectable = getInjectable({
return async (config: Config) => {
const packageJsonPaths = await getPackageJsonPaths(config);

await checkForMissingPackageJsons(packageJsonPaths);
// await checkForMissingPackageJsons(packageJsonPaths);

return pipeline(
packageJsonPaths,
Expand Down
104 changes: 88 additions & 16 deletions packages/linkable/src/linkable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,63 @@ describe('creation of "npm pack" -like symlinks', () => {
});
});

describe('when config file resolves with module paths containing glob', () => {
beforeEach(async () => {
await readJsonFileMock.resolve([
'../some-monorepo/packages/**/*',
'../some-other-monorepo/packages/**/*',
]);
});

it('discovers the target module package.jsons using glob', () => {
expect(globMock.mock.calls).toEqual([
[
'../some-monorepo/packages/**/*/package.json',

{
cwd: '/some-directory/some-project',
ignore: ['**/node_modules/**/*'],
absolute: true,
},
],

[
'../some-other-monorepo/packages/**/*/package.json',
{
cwd: '/some-directory/some-project',
ignore: ['**/node_modules/**/*'],
absolute: true,
},
],
]);
});

describe('when discovering resolves with package.jsons', () => {
beforeEach(async () => {
readJsonFileMock.mockClear();

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([]);
});

it('reads contents of package.jsons', () => {
expect(readJsonFileMock.mock.calls).toEqual([
[
'/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',
],
]);
});
});
});

describe('when config file resolves with module paths', () => {
beforeEach(async () => {
existsMock.mockClear();
Expand All @@ -138,28 +195,41 @@ describe('creation of "npm pack" -like symlinks', () => {
]);
});

it('checks for existence of package.jsons in configured module paths', () => {
expect(existsMock.mock.calls).toEqual([
['/some-directory/some-module/package.json'],
['/some-other-directory/some-other-module/package.json'],
it('discovers package.jsons using glob', () => {
expect(globMock.mock.calls).toEqual([
[
'../some-module/package.json',

{
absolute: true,
cwd: '/some-directory/some-project',
ignore: ['**/node_modules/**/*'],
},
],

[
'/some-other-directory/some-other-module/package.json',

{
absolute: true,
cwd: '/some-directory/some-project',
ignore: ['**/node_modules/**/*'],
},
],
]);
});

it('given some of the package.jsons do not exist, throws', () => {
existsMock.resolve(false);
existsMock.resolve(false);

return expect(actualPromise).rejects.toThrow(
'Tried to install links of linkable, but configured package.jsons were not found: "/some-directory/some-module/package.json", "/some-other-directory/some-other-module/package.json".',
);
});

describe('given all configured package.jsons exist', () => {
describe('when discover resolves', () => {
beforeEach(async () => {
readJsonFileMock.mockClear();

await existsMock.resolve(true);
await existsMock.resolve(true);
await globMock.resolve([
'/some-directory/some-module/package.json',
]);

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

it('reads contents of package.jsons', () => {
Expand Down Expand Up @@ -328,6 +398,8 @@ describe('creation of "npm pack" -like symlinks', () => {

describe('given link directories are handled', () => {
beforeEach(async () => {
globMock.mockClear();

await ensureEmptyDirectoryMock.resolve();
await ensureEmptyDirectoryMock.resolve();
});
Expand Down

0 comments on commit 62144eb

Please sign in to comment.