-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib): replace fp-ts with @mobily/ts-belt
- Loading branch information
1 parent
c72fdd3
commit 5a226fa
Showing
29 changed files
with
565 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export class BaseError extends Error { | ||
name: 'SyncpackError'; | ||
cause?: BaseError | Error | null; | ||
|
||
constructor( | ||
message: string, | ||
options?: { | ||
cause?: unknown; | ||
props?: { args: any[] }; | ||
}, | ||
) { | ||
super(message); | ||
this.name = 'SyncpackError'; | ||
this.cause = BaseError.normalize(options?.cause); | ||
} | ||
|
||
static normalize(value: unknown): BaseError | Error | null { | ||
if (value instanceof BaseError) return value; | ||
if (value instanceof Error) return value; | ||
if (typeof value === 'string') return new Error(value); | ||
return null; | ||
} | ||
|
||
static map(message: string) { | ||
return (cause: unknown): BaseError => { | ||
return new BaseError(message, { cause }); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import { BaseError } from '../error'; | ||
|
||
/** Additional helpers for https://mobily.github.io/ts-belt/api/result */ | ||
export const $R = { | ||
/** | ||
* Return an R.Ok<output[]> for every R.Result which succeeded, or an | ||
* R.Error<BaseError> if none succeeded. | ||
*/ | ||
onlyOk<Input, Output = Input>( | ||
getResult: (value: Input) => R.Result<Output, BaseError>, | ||
) { | ||
return (inputs: Input[]): R.Result<Output[], BaseError> => { | ||
const outputs: Output[] = []; | ||
for (const value of inputs) { | ||
const result = getResult(value); | ||
if (R.isError(result)) continue; | ||
outputs.push(R.getExn(result)); | ||
} | ||
return outputs.length > 0 | ||
? (R.Ok<Output[]>(outputs) as R.Result<Output[], BaseError>) | ||
: R.Error(new BaseError('No R.Ok() returned by $R.onlyOk')); | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/lib/get-context/get-package-json-files/get-file-paths.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import { mockDisk } from '../../../../test/mock-disk'; | ||
import { BaseError } from '../../error'; | ||
import { getConfig } from '../get-config'; | ||
import { getFilePaths } from './get-file-paths'; | ||
|
||
it('returns R.Error when patterns return no files', () => { | ||
const disk = mockDisk(); | ||
const program = getConfig(disk, {}); | ||
disk.globSync.mockReturnValue([]); | ||
const message = | ||
'No package.json files matched the patterns: "package.json", "packages/*/package.json"'; | ||
expect(getFilePaths(disk, program)).toEqual(R.Error(new BaseError(message))); | ||
}); | ||
|
||
it('returns R.Ok when patterns return files', () => { | ||
const disk = mockDisk(); | ||
const program = getConfig(disk, {}); | ||
const root = ['/fake/dir/package.json']; | ||
const packages = [ | ||
'/fake/dir/packages/a/package.json', | ||
'/fake/dir/packages/b/package.json', | ||
]; | ||
disk.globSync.mockImplementation((pattern) => { | ||
if (pattern === 'package.json') return root; | ||
if (pattern === 'packages/*/package.json') return packages; | ||
throw new Error('Unexpected pattern in test'); | ||
}); | ||
expect(getFilePaths(disk, program)).toEqual(R.Ok([...root, ...packages])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/lib/get-context/get-package-json-files/get-patterns/get-lerna-patterns.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import { mockDisk } from '../../../../../test/mock-disk'; | ||
import { BaseError } from '../../../error'; | ||
import { getLernaPatterns } from './get-lerna-patterns'; | ||
|
||
it('returns an R.Ok of strings when found', () => { | ||
const disk = mockDisk(); | ||
disk.readFileSync.mockReturnValue(JSON.stringify({ packages: ['a', 'b'] })); | ||
expect(getLernaPatterns(disk)()).toEqual(R.Ok(['a', 'b'])); | ||
}); | ||
|
||
it('returns an R.Error when disk throws', () => { | ||
const disk = mockDisk(); | ||
const thrownError = new BaseError( | ||
'Failed to read JSON file at /fake/dir/lerna.json', | ||
); | ||
disk.readFileSync.mockImplementation(() => { | ||
throw thrownError; | ||
}); | ||
expect(getLernaPatterns(disk)()).toEqual(R.Error(thrownError)); | ||
}); | ||
|
||
it('returns an R.Error when data is not valid JSON', () => { | ||
const disk = mockDisk(); | ||
const thrownError = new BaseError( | ||
'Failed to parse JSON file at /fake/dir/lerna.json', | ||
); | ||
disk.readFileSync.mockReturnValue('wut?'); | ||
expect(getLernaPatterns(disk)()).toEqual(R.Error(thrownError)); | ||
}); | ||
|
||
it('returns an R.Error when data is valid JSON but the wrong shape', () => { | ||
const disk = mockDisk(); | ||
disk.readFileSync.mockReturnValue(JSON.stringify({ packages: [1, 2] })); | ||
expect(getLernaPatterns(disk)()).toEqual( | ||
R.Error(new BaseError('no lerna patterns found')), | ||
); | ||
}); |
31 changes: 18 additions & 13 deletions
31
src/lib/get-context/get-package-json-files/get-patterns/get-lerna-patterns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import { isArrayOfStrings } from 'expect-more'; | ||
import * as E from 'fp-ts/lib/Either'; | ||
import { flow, pipe } from 'fp-ts/lib/function'; | ||
import * as O from 'fp-ts/lib/Option'; | ||
import { O, pipe, R } from '@mobily/ts-belt'; | ||
import { join } from 'path'; | ||
import { CWD } from '../../../../constants'; | ||
import type { Disk } from '../../../disk'; | ||
import { props } from './props'; | ||
import { BaseError } from '../../../error'; | ||
import { getArrayOfStrings } from './lib/get-array-of-strings'; | ||
import { readJsonSafe } from './read-json-safe'; | ||
|
||
export function getLernaPatterns(disk: Disk): () => O.Option<string[]> { | ||
return () => | ||
pipe( | ||
readJsonSafe(disk)(join(CWD, 'lerna.json')), | ||
E.map(flow(props('contents.packages'), O.filter(isArrayOfStrings))), | ||
E.match( | ||
(): O.Option<string[]> => O.none, | ||
(value) => value, | ||
export function getLernaPatterns( | ||
disk: Disk, | ||
): () => R.Result<string[], BaseError> { | ||
const getPackages = getArrayOfStrings('packages'); | ||
|
||
return function getLernaPatterns() { | ||
return pipe( | ||
join(CWD, 'lerna.json'), | ||
readJsonSafe(disk), | ||
R.flatMap(({ contents }) => | ||
pipe( | ||
getPackages(contents), | ||
O.toResult(new BaseError('no lerna patterns found')), | ||
), | ||
), | ||
); | ||
}; | ||
} |
Oops, something went wrong.