Skip to content

Commit

Permalink
Enumerate patch directories only (#1426)
Browse files Browse the repository at this point in the history
Apparently `index.d.ts` is not a valid semver :D

```
skuba    │ Failed to run skuba lints.
skuba    │ TypeError: Invalid Version: index.d.ts
    at new SemVer (/Users/rling/Code/github.com/seek-oss/skuba/node_modules/.pnpm/[email protected]/node_modules/semver/classes/semver.js:38:13)
    at compare (/Users/rling/Code/github.com/seek-oss/skuba/node_modules/.pnpm/[email protected]/node_modules/semver/functions/compare.js:3:3)
    at gte (/Users/rling/Code/github.com/seek-oss/skuba/node_modules/.pnpm/[email protected]/node_modules/semver/functions/gte.js:2:30)
    at /Users/rling/Code/github.com/seek-oss/skuba/lib/cli/configure/upgrade/index.js:46:38
    at Array.filter (<anonymous>)
    at getPatches (/Users/rling/Code/github.com/seek-oss/skuba/lib/cli/configure/upgrade/index.js:44:13)
    at async upgradeSkuba (/Users/rling/Code/github.com/seek-oss/skuba/lib/cli/configure/upgrade/index.js:78:19)
    at async Promise.all (index 0)
    at async lintConcurrently (/Users/rling/Code/github.com/seek-oss/skuba/lib/cli/lint/internal.js:59:10)
    at async internalLint (/Users/rling/Code/github.com/seek-oss/skuba/lib/cli/lint/internal.js:76:21)
```
  • Loading branch information
72636c authored Feb 6, 2024
1 parent 889da38 commit 04e32fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
29 changes: 20 additions & 9 deletions src/cli/configure/upgrade/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ describe('upgradeSkuba in format mode', () => {
jest.mocked(getSkubaVersion).mockResolvedValue('2.0.0');

// readdir has overloads and the mocked version doesn't match the string version
jest
.mocked(readdir)
.mockResolvedValue(['0.9.0', '1.0.0', '2.0.0'] as never);
jest.mocked(readdir).mockResolvedValue([
{ isDirectory: () => true, name: '0.9.0' },
{ isDirectory: () => true, name: '1.0.0' },
{ isDirectory: () => true, name: '2.0.0' },
{ isDirectory: () => false, name: 'index.d.ts' },
] as never);

await expect(upgradeSkuba('format', log)).resolves.toEqual({
ok: true,
Expand Down Expand Up @@ -118,7 +121,9 @@ describe('upgradeSkuba in format mode', () => {
jest.mocked(getSkubaVersion).mockResolvedValue('2.0.0');

// readdir has overloads and the mocked version doesn't match the string version
jest.mocked(readdir).mockResolvedValue(['2.0.0'] as never);
jest
.mocked(readdir)
.mockResolvedValue([{ isDirectory: () => true, name: '2.0.0' }] as never);

await expect(upgradeSkuba('format', log)).resolves.toEqual({
ok: true,
Expand Down Expand Up @@ -161,7 +166,9 @@ describe('upgradeSkuba in format mode', () => {
jest.mocked(getSkubaVersion).mockResolvedValue('2.0.0');

// readdir has overloads and the mocked version doesn't match the string version
jest.mocked(readdir).mockResolvedValue(['2.0.0'] as never);
jest
.mocked(readdir)
.mockResolvedValue([{ isDirectory: () => true, name: '2.0.0' }] as never);

await expect(upgradeSkuba('format', log)).resolves.toEqual({
ok: true,
Expand Down Expand Up @@ -232,9 +239,11 @@ describe('upgradeSkuba in lint mode', () => {
jest.mocked(getSkubaVersion).mockResolvedValue('2.0.0');

// readdir has overloads and the mocked version doesn't match the string version
jest
.mocked(readdir)
.mockResolvedValue(['0.9.0', '1.0.0', '2.0.0'] as never);
jest.mocked(readdir).mockResolvedValue([
{ isDirectory: () => true, name: '0.9.0' },
{ isDirectory: () => true, name: '1.0.0' },
{ isDirectory: () => true, name: '2.0.0' },
] as never);

await expect(upgradeSkuba('lint', log)).resolves.toEqual({
ok: false,
Expand Down Expand Up @@ -265,7 +274,9 @@ describe('upgradeSkuba in lint mode', () => {

jest.mocked(getSkubaVersion).mockResolvedValue('2.0.0');

jest.mocked(readdir).mockResolvedValue(['0.9.0'] as never);
jest
.mocked(readdir)
.mockResolvedValue([{ isDirectory: () => true, name: '0.9.0' }] as never);

await expect(upgradeSkuba('lint', log)).resolves.toEqual({
ok: true,
Expand Down
13 changes: 11 additions & 2 deletions src/cli/configure/upgrade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ export type PatchFunction = (
) => Promise<PatchReturnType>;

const getPatches = async (manifestVersion: string): Promise<Patches> => {
const patches = await readdir(path.join(__dirname, 'patches'));
const patches = await readdir(path.join(__dirname, 'patches'), {
withFileTypes: true,
});

// The patches are sorted by the version they were added from.
// Only return patches that are newer or equal to the current version.
const patchesForVersion = sort(
patches.filter((filename) => gte(filename, manifestVersion)),
patches.flatMap((patch) =>
// Is a directory rather than a JavaScript source file
patch.isDirectory() &&
// Has been added since the last patch run on the project
gte(patch.name, manifestVersion)
? patch.name
: [],
),
);

return (await Promise.all(patchesForVersion.map(resolvePatches))).flat();
Expand Down

0 comments on commit 04e32fc

Please sign in to comment.