-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathutils.ts
More file actions
85 lines (75 loc) · 2.85 KB
/
utils.ts
File metadata and controls
85 lines (75 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { HasCwd } from '@contentlayer/core'
import * as core from '@contentlayer/core'
import { provideCwd } from '@contentlayer/core'
import { provideDummyTracing, unknownToPosixFilePath } from '@contentlayer/utils'
import type { HasClock, HasConsole, OT } from '@contentlayer/utils/effect'
import { pipe, provideTestConsole, T, These } from '@contentlayer/utils/effect'
import type { HasDocumentTypeMapState } from '../../fetchData/DocumentTypeMap.js'
import { provideDocumentTypeMapState } from '../../fetchData/DocumentTypeMap.js'
import { testOnly_makeContentTypeMap, testOnly_makefilePathPatternMap } from '../../fetchData/index.js'
import { makeCacheItemFromFilePath } from '../../fetchData/makeCacheItemFromFilePath.js'
import type { DocumentTypes } from '../../index.js'
import { makeSource } from '../../index.js'
export const runTest = async ({
documentTypes,
contentDirPath: contentDirPath_,
relativeFilePath: relativeFilePath_,
}: {
documentTypes: DocumentTypes
contentDirPath: string
relativeFilePath: string
}) => {
const eff = T.gen(function* ($) {
const relativeFilePath = unknownToPosixFilePath(relativeFilePath_)
const contentDirPath = unknownToPosixFilePath(contentDirPath_)
const esbuildHash = 'not-important-for-this-test'
const source = yield* $(T.tryPromise(() => makeSource({ contentDirPath, documentTypes })))
const coreSchemaDef = yield* $(source.provideSchema(esbuildHash))
const documentTypeDefs = (Array.isArray(documentTypes) ? documentTypes : Object.values(documentTypes)).map((_) =>
_.def(),
)
const filePathPatternMap = testOnly_makefilePathPatternMap(documentTypeDefs)
const contentTypeMap = testOnly_makeContentTypeMap(documentTypeDefs)
const options: core.PluginOptions = {
date: undefined,
markdown: undefined,
mdx: undefined,
fieldOptions: core.defaultFieldOptions,
disableImportAliasWarning: false,
}
const cache = yield* $(
pipe(
makeCacheItemFromFilePath({
relativeFilePath,
contentDirPath,
coreSchemaDef,
filePathPatternMap,
options,
previousCache: undefined,
contentTypeMap,
}),
// TODO: DocumentTypeMap is now explicitly returned from
// makeCacheItemFromFilePath in [1]. Verify that it is tested somewhere
// else, as we're throwing it away here.
T.map((_) => _.tuple[0]),
These.effectToEither,
),
)
return cache
})
return runMain(eff)
}
const runMain = async <E, A>(
eff: T.Effect<OT.HasTracer & HasClock & HasCwd & HasConsole & HasDocumentTypeMapState, E, A>,
) => {
const logMessages: string[] = []
const result = await pipe(
eff,
provideTestConsole(logMessages),
provideDocumentTypeMapState,
provideCwd,
provideDummyTracing,
T.runPromise,
)
return { logMessages, result }
}