Skip to content

Commit 74e2d32

Browse files
committed
onSuccess callback + prerelease
1 parent a374d24 commit 74e2d32

File tree

19 files changed

+80
-17
lines changed

19 files changed

+80
-17
lines changed

packages/@contentlayer/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/cli",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/client",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/core",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/core/src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ export type SourceProvideSchemaErrorJSON = ReturnType<SourceProvideSchemaError['
5656
export class HandledFetchDataError extends Tagged('HandledFetchDataError')<{}> {}
5757

5858
export class EsbuildBinNotFoundError extends Tagged('EsbuildBinNotFoundError')<{}> {}
59+
60+
export class SuccessCallbackError extends Tagged('SuccessCallbackError')<{
61+
readonly error: any
62+
}> {
63+
toString = () => `SuccessCallbackError: ${errorToString(this.error)}`
64+
}

packages/@contentlayer/core/src/generation/generate-dotpkg.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AbsolutePosixFilePath, RelativePosixFilePath } from '@contentlayer/utils'
22
import { filePathJoin, fs, relative } from '@contentlayer/utils'
33
import type { E, HasClock, HasConsole } from '@contentlayer/utils/effect'
4-
import { Array, Chunk, OT, pipe, S, T } from '@contentlayer/utils/effect'
4+
import { Array, Chunk, Either, OT, pipe, S, T } from '@contentlayer/utils/effect'
55
import type { GetContentlayerVersionError } from '@contentlayer/utils/node'
66
import { getContentlayerVersion } from '@contentlayer/utils/node'
77
import { camelCase } from 'camel-case'
@@ -12,10 +12,11 @@ import type { HasCwd } from '../cwd.js'
1212
import { getCwd } from '../cwd.js'
1313
import type { DataCache } from '../DataCache.js'
1414
import type { SourceProvideSchemaError } from '../errors.js'
15+
import { SuccessCallbackError } from '../errors.js'
1516
import * as esbuild from '../getConfig/esbuild.js'
1617
import type { Config } from '../getConfig/index.js'
1718
import type { SourceFetchDataError } from '../index.js'
18-
import type { PluginOptions, SourcePluginType } from '../plugin.js'
19+
import type { PluginOptions, SourcePluginType, SuccessCallback } from '../plugin.js'
1920
import type { DocumentTypeDef, SchemaDef } from '../schema/index.js'
2021
import { autogeneratedNote, getDataVariableName } from './common.js'
2122
import { renderTypes } from './generate-types.js'
@@ -42,6 +43,7 @@ type GenerateDotpkgError =
4243
| SourceFetchDataError
4344
| esbuild.EsbuildError
4445
| GetContentlayerVersionError
46+
| SuccessCallbackError
4547

4648
export type GenerateInfo = {
4749
documentCount: number
@@ -105,8 +107,35 @@ export const generateDotpkgStream = ({
105107
T.eitherMap(() => ({ documentCount: Object.keys(cache.cacheItemsMap).length })),
106108
),
107109
),
110+
S.mapEffect((generateInfo) =>
111+
pipe(
112+
successCallback(config.source.options.onSuccess),
113+
// TODO remove type casting
114+
T.fold(
115+
(error) => Either.left(error) as typeof generateInfo,
116+
() => generateInfo,
117+
),
118+
),
119+
),
120+
),
121+
),
122+
)
123+
}
124+
125+
const successCallback = (onSuccess: SuccessCallback | undefined) => {
126+
if (!onSuccess) return T.unit
127+
128+
return pipe(
129+
getCwd,
130+
T.map((cwd) => ArtifactsDir.getDirPath({ cwd })),
131+
T.tapSync((path) => console.log('successCallback', path)),
132+
T.chain((generatedPkgPath) =>
133+
T.tryCatchPromise(
134+
() => onSuccess(() => import(filePathJoin(generatedPkgPath, 'generated', 'index.mjs'))),
135+
(error) => new SuccessCallbackError({ error }),
108136
),
109137
),
138+
OT.withSpan('@contentlayer/core/generation:successCallback'),
110139
)
111140
}
112141

packages/@contentlayer/core/src/plugin.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type * as unified from 'unified'
88
import type { HasCwd } from './cwd.js'
99
import type { DataCache } from './DataCache.js'
1010
import type { SourceFetchDataError, SourceProvideSchemaError } from './errors.js'
11+
import type { GetDataExportsGen } from './gen.js'
1112
import type { SchemaDef, StackbitExtension } from './schema/index.js'
1213

1314
export type SourcePluginType = LiteralUnion<'local' | 'contentful' | 'sanity', string>
@@ -24,8 +25,11 @@ export type PluginOptions = {
2425
fieldOptions: FieldOptions
2526
disableImportAliasWarning: boolean
2627
experimental: PluginOptionsExperimental
28+
onSuccess: SuccessCallback | undefined
2729
}
2830

31+
export type SuccessCallback = (getData: () => Promise<GetDataExportsGen>) => Promise<void>
32+
2933
export type PluginOptionsExperimental = {
3034
enableDynamicBuild: boolean
3135
}
@@ -114,7 +118,6 @@ export type SourcePlugin = {
114118
type: SourcePluginType
115119
provideSchema: ProvideSchema
116120
fetchData: FetchData
117-
} & {
118121
options: PluginOptions
119122
extensions: PluginExtensions
120123
}
@@ -150,6 +153,7 @@ export type PartialArgs = {
150153
extensions?: PluginExtensions
151154
disableImportAliasWarning?: boolean
152155
experimental?: Partial<PluginOptionsExperimental>
156+
onSuccess?: SuccessCallback | undefined
153157
}
154158

155159
export const defaultFieldOptions: FieldOptions = {
@@ -165,11 +169,27 @@ export const processArgs = async <TArgs extends PartialArgs>(
165169
options: PluginOptions
166170
restArgs: Omit<
167171
TArgs,
168-
'extensions' | 'fieldOptions' | 'markdown' | 'mdx' | 'date' | 'disableImportAliasWarning' | 'experimental'
172+
| 'extensions'
173+
| 'fieldOptions'
174+
| 'markdown'
175+
| 'mdx'
176+
| 'date'
177+
| 'disableImportAliasWarning'
178+
| 'experimental'
179+
| 'onSuccess'
169180
>
170181
}> => {
171-
const { extensions, fieldOptions, markdown, mdx, date, disableImportAliasWarning, experimental, ...restArgs } =
172-
typeof argsOrArgsThunk === 'function' ? await argsOrArgsThunk(sourceKey) : argsOrArgsThunk
182+
const {
183+
extensions,
184+
fieldOptions,
185+
markdown,
186+
mdx,
187+
date,
188+
disableImportAliasWarning,
189+
experimental,
190+
onSuccess,
191+
...restArgs
192+
} = typeof argsOrArgsThunk === 'function' ? await argsOrArgsThunk(sourceKey) : argsOrArgsThunk
173193

174194
const options: PluginOptions = {
175195
markdown,
@@ -183,6 +203,7 @@ export const processArgs = async <TArgs extends PartialArgs>(
183203
experimental: {
184204
enableDynamicBuild: experimental?.enableDynamicBuild ?? false,
185205
},
206+
onSuccess,
186207
}
187208

188209
return { extensions: extensions ?? {}, options, restArgs }

packages/@contentlayer/experimental-source-files-stackbit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/experimental-source-files-stackbit",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": {
66
".": {

packages/@contentlayer/source-contentful/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-contentful",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/source-files/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-files",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": {
66
".": {

packages/@contentlayer/source-files/src/__test__/errors/aggregate.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const options: core.PluginOptions = {
1515
fieldOptions: { typeFieldName, bodyFieldName },
1616
disableImportAliasWarning: false,
1717
experimental: { enableDynamicBuild: false },
18+
onSuccess: undefined,
1819
}
1920
const flags: Flags = {
2021
onExtraFieldData: 'warn',

packages/@contentlayer/source-files/src/__test__/fetchData/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const runTest = async ({
4848
fieldOptions: core.defaultFieldOptions,
4949
disableImportAliasWarning: false,
5050
experimental: { enableDynamicBuild: false },
51+
onSuccess: undefined,
5152
}
5253

5354
const cache = yield* $(

packages/@contentlayer/source-files/src/__test__/mapping.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('getDataForFieldDef', () => {
5555
date: undefined,
5656
disableImportAliasWarning: false,
5757
experimental: { enableDynamicBuild: false },
58+
onSuccess: undefined,
5859
...options,
5960
},
6061
}),
@@ -140,6 +141,7 @@ test('getDataForFieldDef error', async () => {
140141
date: undefined,
141142
disableImportAliasWarning: false,
142143
experimental: { enableDynamicBuild: false },
144+
onSuccess: undefined,
143145
...options,
144146
},
145147
}),

packages/@contentlayer/source-files/src/__test__/type-generation/basic.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const renderTypeSource = async (documentTypes: DocumentTypes) => {
2828
date: undefined,
2929
disableImportAliasWarning: false,
3030
experimental: { enableDynamicBuild: false },
31+
onSuccess: undefined,
3132
},
3233
},
3334
})

packages/@contentlayer/source-files/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export type Args = {
6464
contentDirExclude?: string[]
6565
// NOTE https://github.com/parcel-bundler/watcher/issues/64
6666

67+
onSuccess?: core.SuccessCallback
68+
6769
/**
6870
* This is an experimental feature and should be ignored for now.
6971
*/

packages/@contentlayer/source-remote-files/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/source-remote-files",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": {
66
".": {

packages/@contentlayer/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/utils",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"exports": {
66
"./package.json": {

packages/contentlayer-stackbit-yaml-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentlayer-stackbit-yaml-generator",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"bin": "./dist/cli/index.js",
66
"exports": "./dist/lib/index.js",

packages/contentlayer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentlayer",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"bin": "./bin/cli.cjs",
55
"type": "module",
66
"engines": {

packages/next-contentlayer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-contentlayer",
3-
"version": "0.3.2",
3+
"version": "0.3.3-dev.1",
44
"type": "module",
55
"main": "./dist/index-cjs.cjs",
66
"sideEffects": false,

0 commit comments

Comments
 (0)