@sanity/codegen 7.0.1 → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { error, log } from 'node:console';
|
|
2
2
|
import { isAbsolute, join, relative } from 'node:path';
|
|
3
3
|
import { styleText } from 'node:util';
|
|
4
|
-
import
|
|
4
|
+
import { watch } from 'chokidar';
|
|
5
5
|
import debounce from 'lodash-es/debounce.js';
|
|
6
6
|
import mean from 'lodash-es/mean.js';
|
|
7
7
|
import { prepareConfig } from '../utils/config.js';
|
|
@@ -85,7 +85,7 @@ const IGNORED_PATTERNS = [
|
|
|
85
85
|
// perform initial generation
|
|
86
86
|
debouncedGenerate();
|
|
87
87
|
// set up watcher
|
|
88
|
-
const watcher =
|
|
88
|
+
const watcher = watch(watchTargets, {
|
|
89
89
|
cwd: workDir,
|
|
90
90
|
ignored: IGNORED_PATTERNS,
|
|
91
91
|
ignoreInitial: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/actions/typegenWatch.ts"],"sourcesContent":["import {error, log} from 'node:console'\nimport {isAbsolute, join, relative} from 'node:path'\nimport {styleText} from 'node:util'\n\nimport
|
|
1
|
+
{"version":3,"sources":["../../src/actions/typegenWatch.ts"],"sourcesContent":["import {error, log} from 'node:console'\nimport {isAbsolute, join, relative} from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {type FSWatcher, watch} from 'chokidar'\nimport debounce from 'lodash-es/debounce.js'\nimport mean from 'lodash-es/mean.js'\n\nimport {TypegenWatchModeTraceAttributes} from '../typegen.telemetry.js'\nimport {prepareConfig} from '../utils/config.js'\nimport {runTypegenGenerate} from './typegenGenerate.js'\nimport {type RunTypegenOptions} from './types.js'\n\nconst IGNORED_PATTERNS = [\n '**/node_modules/**',\n '**/.git/**',\n '**/dist/**',\n '**/lib/**',\n '**/.sanity/**',\n]\n\n/** State for tracking generation status */\ninterface WatchState {\n isGenerating: boolean\n pendingGeneration: boolean\n}\n\n/** Return type for createTypegenRunner */\ninterface TypegenRunner {\n runGeneration: () => Promise<void>\n state: WatchState\n}\n\ntype WatcherStats = Omit<Extract<TypegenWatchModeTraceAttributes, {step: 'stopped'}>, 'step'>\n\n/**\n * Creates a typegen runner with concurrency control.\n * If generation is already running, queues one more generation to run after completion.\n * Multiple queued requests are coalesced into a single pending generation.\n */\nfunction createTypegenRunner(onGenerate: () => Promise<unknown>): TypegenRunner {\n const state: WatchState = {\n isGenerating: false,\n pendingGeneration: false,\n }\n\n async function runGeneration(): Promise<void> {\n if (state.isGenerating) {\n state.pendingGeneration = true\n return\n }\n\n state.isGenerating = true\n state.pendingGeneration = false\n\n try {\n await onGenerate()\n } finally {\n state.isGenerating = false\n\n // If a change came in during generation, run again\n if (state.pendingGeneration) {\n state.pendingGeneration = false\n await runGeneration()\n }\n }\n }\n\n return {runGeneration, state}\n}\n\n/**\n * Starts a file watcher that triggers typegen on changes.\n * Watches both query files (via patterns) and the schema JSON file.\n * Implements debouncing and concurrency control to prevent multiple generations.\n * @public\n */\nexport function runTypegenWatcher(options: RunTypegenOptions): {\n getStats: () => WatcherStats\n stop: () => Promise<void>\n watcher: FSWatcher\n} {\n const {config, workDir} = options\n const {path, schema} = prepareConfig(config)\n\n const stats = {\n failedCount: 0,\n startTime: Date.now(),\n successfulDurations: [] as number[],\n }\n\n const {runGeneration} = createTypegenRunner(async () => {\n try {\n const {duration} = await runTypegenGenerate({...options})\n stats.successfulDurations.push(duration)\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : err\n error(` ${styleText('red', '›')} ${errorMessage}`)\n stats.failedCount++\n }\n })\n\n // Debounced generation trigger\n const debouncedGenerate = debounce(runGeneration, 1000)\n\n // Build absolute patterns for query files and schema file\n const paths = Array.isArray(path) ? path : [path]\n const absoluteQueryPatterns = paths.map((pattern) =>\n isAbsolute(pattern) ? pattern : join(workDir, pattern),\n )\n const absoluteSchemaPath = isAbsolute(schema) ? schema : join(workDir, schema)\n const watchTargets = [...absoluteQueryPatterns, absoluteSchemaPath]\n\n // perform initial generation\n debouncedGenerate()\n\n // set up watcher\n const watcher = watch(watchTargets, {\n cwd: workDir,\n ignored: IGNORED_PATTERNS,\n ignoreInitial: true,\n })\n\n watcher.on('all', (event: string, filePath: string) => {\n const timestamp = new Date().toLocaleTimeString()\n const relativePath = isAbsolute(filePath) ? relative(workDir, filePath) : filePath\n log(`[${timestamp}] ${event}: ${relativePath}`)\n debouncedGenerate()\n })\n\n watcher.on('error', (err: Error) => {\n error(`Watcher error: ${err.message}`)\n })\n\n return {\n getStats: () => ({\n averageGenerationDuration: mean(stats.successfulDurations) || 0,\n generationFailedCount: stats.failedCount,\n generationSuccessfulCount: stats.successfulDurations.length,\n watcherDuration: Date.now() - stats.startTime,\n }),\n stop: async () => {\n await watcher.close()\n },\n watcher,\n }\n}\n"],"names":["error","log","isAbsolute","join","relative","styleText","watch","debounce","mean","prepareConfig","runTypegenGenerate","IGNORED_PATTERNS","createTypegenRunner","onGenerate","state","isGenerating","pendingGeneration","runGeneration","runTypegenWatcher","options","config","workDir","path","schema","stats","failedCount","startTime","Date","now","successfulDurations","duration","push","err","errorMessage","Error","message","debouncedGenerate","paths","Array","isArray","absoluteQueryPatterns","map","pattern","absoluteSchemaPath","watchTargets","watcher","cwd","ignored","ignoreInitial","on","event","filePath","timestamp","toLocaleTimeString","relativePath","getStats","averageGenerationDuration","generationFailedCount","generationSuccessfulCount","length","watcherDuration","stop","close"],"mappings":"AAAA,SAAQA,KAAK,EAAEC,GAAG,QAAO,eAAc;AACvC,SAAQC,UAAU,EAAEC,IAAI,EAAEC,QAAQ,QAAO,YAAW;AACpD,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAwBC,KAAK,QAAO,WAAU;AAC9C,OAAOC,cAAc,wBAAuB;AAC5C,OAAOC,UAAU,oBAAmB;AAGpC,SAAQC,aAAa,QAAO,qBAAoB;AAChD,SAAQC,kBAAkB,QAAO,uBAA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,OAAO;QAACA;KAAK;IACjD,MAAMkB,wBAAwBH,MAAMI,GAAG,CAAC,CAACC,UACvCxC,WAAWwC,WAAWA,UAAUvC,KAAKkB,SAASqB;IAEhD,MAAMC,qBAAqBzC,WAAWqB,UAAUA,SAASpB,KAAKkB,SAASE;IACvE,MAAMqB,eAAe;WAAIJ;QAAuBG;KAAmB;IAEnE,6BAA6B;IAC7BP;IAEA,iBAAiB;IACjB,MAAMS,UAAUvC,MAAMsC,cAAc;QAClCE,KAAKzB;QACL0B,SAASpC;QACTqC,eAAe;IACjB;IAEAH,QAAQI,EAAE,CAAC,OAAO,CAACC,OAAeC;QAChC,MAAMC,YAAY,IAAIzB,OAAO0B,kBAAkB;QAC/C,MAAMC,eAAepD,WAAWiD,YAAY/C,SAASiB,SAAS8B,YAAYA;QAC1ElD,IAAI,CAAC,CAAC,EAAEmD,UAAU,EAAE,EAAEF,MAAM,EAAE,EAAEI,cAAc;QAC9ClB;IACF;IAEAS,QAAQI,EAAE,CAAC,SAAS,CAACjB;QACnBhC,MAAM,CAAC,eAAe,EAAEgC,IAAIG,OAAO,EAAE;IACvC;IAEA,OAAO;QACLoB,UAAU,IAAO,CAAA;gBACfC,2BAA2BhD,KAAKgB,MAAMK,mBAAmB,KAAK;gBAC9D4B,uBAAuBjC,MAAMC,WAAW;gBACxCiC,2BAA2BlC,MAAMK,mBAAmB,CAAC8B,MAAM;gBAC3DC,iBAAiBjC,KAAKC,GAAG,KAAKJ,MAAME,SAAS;YAC/C,CAAA;QACAmC,MAAM;YACJ,MAAMhB,QAAQiB,KAAK;QACrB;QACAjB;IACF;AACF"}
|
package/dist/readConfig.js
CHANGED
|
@@ -29,11 +29,12 @@ import * as z from 'zod/mini';
|
|
|
29
29
|
*/ export async function readConfig(path) {
|
|
30
30
|
try {
|
|
31
31
|
const content = await readFile(path, 'utf8');
|
|
32
|
+
// eslint-disable-next-line import-x/no-named-as-default-member -- json5 is CJS and doesn't support named exports
|
|
32
33
|
const json = json5.parse(content);
|
|
33
34
|
return configDefinition.parseAsync(json);
|
|
34
35
|
} catch (error) {
|
|
35
36
|
if (typeof error === 'object' && error !== null && 'issues' in error && Array.isArray(error.issues)) {
|
|
36
|
-
throw new
|
|
37
|
+
throw new TypeError(`Error in config file\n ${error.issues.map((err)=>err.message).join('\n')}`, {
|
|
37
38
|
cause: error
|
|
38
39
|
});
|
|
39
40
|
}
|
package/dist/readConfig.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/readConfig.ts"],"sourcesContent":["import {readFile} from 'node:fs/promises'\n\nimport json5 from 'json5'\nimport * as z from 'zod/mini'\n\n/**\n * The formatter to use for generated code.\n * - `false` - Do not format generated code.\n * - `true` | `'prettier'` - Format with prettier.\n * - `'oxfmt'` - Format with oxfmt. Throws if oxfmt is not installed.\n * @public\n */\nexport type FormatGeneratedCode = 'oxfmt' | 'prettier' | boolean\n\n/**\n * @public\n */\nexport const configDefinition = z.object({\n formatGeneratedCode: z._default(z.union([z.boolean(), z.enum(['oxfmt', 'prettier'])]), true),\n generates: z._default(z.string(), './sanity.types.ts'),\n overloadClientMethods: z._default(z.boolean(), true),\n path: z._default(z.union([z.string(), z.array(z.string())]), [\n './src/**/*.{ts,tsx,js,jsx,mjs,cjs,astro,vue,svelte}',\n './app/**/*.{ts,tsx,js,jsx,mjs,cjs,astro,vue,svelte}',\n './sanity/**/*.{ts,tsx,js,jsx,mjs,cjs}',\n ]),\n schema: z._default(z.string(), './schema.json'),\n})\n\n/** @public */\nexport type TypeGenConfig = z.infer<typeof configDefinition>\n\n/**\n * @deprecated use TypeGenConfig\n * @public\n */\nexport type CodegenConfig = TypeGenConfig\n\n/**\n * Read, parse and process a config file\n * @internal\n */\nexport async function readConfig(path: string): Promise<TypeGenConfig> {\n try {\n const content = await readFile(path, 'utf8')\n const json = json5.parse(content)\n return configDefinition.parseAsync(json)\n } catch (error) {\n if (\n typeof error === 'object' &&\n error !== null &&\n 'issues' in error &&\n Array.isArray(error.issues)\n ) {\n throw new
|
|
1
|
+
{"version":3,"sources":["../src/readConfig.ts"],"sourcesContent":["import {readFile} from 'node:fs/promises'\n\nimport json5 from 'json5'\nimport * as z from 'zod/mini'\n\n/**\n * The formatter to use for generated code.\n * - `false` - Do not format generated code.\n * - `true` | `'prettier'` - Format with prettier.\n * - `'oxfmt'` - Format with oxfmt. Throws if oxfmt is not installed.\n * @public\n */\nexport type FormatGeneratedCode = 'oxfmt' | 'prettier' | boolean\n\n/**\n * @public\n */\nexport const configDefinition = z.object({\n formatGeneratedCode: z._default(z.union([z.boolean(), z.enum(['oxfmt', 'prettier'])]), true),\n generates: z._default(z.string(), './sanity.types.ts'),\n overloadClientMethods: z._default(z.boolean(), true),\n path: z._default(z.union([z.string(), z.array(z.string())]), [\n './src/**/*.{ts,tsx,js,jsx,mjs,cjs,astro,vue,svelte}',\n './app/**/*.{ts,tsx,js,jsx,mjs,cjs,astro,vue,svelte}',\n './sanity/**/*.{ts,tsx,js,jsx,mjs,cjs}',\n ]),\n schema: z._default(z.string(), './schema.json'),\n})\n\n/** @public */\nexport type TypeGenConfig = z.infer<typeof configDefinition>\n\n/**\n * @deprecated use TypeGenConfig\n * @public\n */\nexport type CodegenConfig = TypeGenConfig\n\n/**\n * Read, parse and process a config file\n * @internal\n */\nexport async function readConfig(path: string): Promise<TypeGenConfig> {\n try {\n const content = await readFile(path, 'utf8')\n // eslint-disable-next-line import-x/no-named-as-default-member -- json5 is CJS and doesn't support named exports\n const json = json5.parse(content)\n return configDefinition.parseAsync(json)\n } catch (error) {\n if (\n typeof error === 'object' &&\n error !== null &&\n 'issues' in error &&\n Array.isArray(error.issues)\n ) {\n throw new TypeError(\n `Error in config file\\n ${error.issues.map((err: {message: string}) => err.message).join('\\n')}`,\n {cause: error},\n )\n }\n if (typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT') {\n return configDefinition.parse({})\n }\n\n throw error\n }\n}\n"],"names":["readFile","json5","z","configDefinition","object","formatGeneratedCode","_default","union","boolean","enum","generates","string","overloadClientMethods","path","array","schema","readConfig","content","json","parse","parseAsync","error","Array","isArray","issues","TypeError","map","err","message","join","cause","code"],"mappings":"AAAA,SAAQA,QAAQ,QAAO,mBAAkB;AAEzC,OAAOC,WAAW,QAAO;AACzB,YAAYC,OAAO,WAAU;AAW7B;;CAEC,GACD,OAAO,MAAMC,mBAAmBD,EAAEE,MAAM,CAAC;IACvCC,qBAAqBH,EAAEI,QAAQ,CAACJ,EAAEK,KAAK,CAAC;QAACL,EAAEM,OAAO;QAAIN,EAAEO,IAAI,CAAC;YAAC;YAAS;SAAW;KAAE,GAAG;IACvFC,WAAWR,EAAEI,QAAQ,CAACJ,EAAES,MAAM,IAAI;IAClCC,uBAAuBV,EAAEI,QAAQ,CAACJ,EAAEM,OAAO,IAAI;IAC/CK,MAAMX,EAAEI,QAAQ,CAACJ,EAAEK,KAAK,CAAC;QAACL,EAAES,MAAM;QAAIT,EAAEY,KAAK,CAACZ,EAAES,MAAM;KAAI,GAAG;QAC3D;QACA;QACA;KACD;IACDI,QAAQb,EAAEI,QAAQ,CAACJ,EAAES,MAAM,IAAI;AACjC,GAAE;AAWF;;;CAGC,GACD,OAAO,eAAeK,WAAWH,IAAY;IAC3C,IAAI;QACF,MAAMI,UAAU,MAAMjB,SAASa,MAAM;QACrC,iHAAiH;QACjH,MAAMK,OAAOjB,MAAMkB,KAAK,CAACF;QACzB,OAAOd,iBAAiBiB,UAAU,CAACF;IACrC,EAAE,OAAOG,OAAO;QACd,IACE,OAAOA,UAAU,YACjBA,UAAU,QACV,YAAYA,SACZC,MAAMC,OAAO,CAACF,MAAMG,MAAM,GAC1B;YACA,MAAM,IAAIC,UACR,CAAC,uBAAuB,EAAEJ,MAAMG,MAAM,CAACE,GAAG,CAAC,CAACC,MAA2BA,IAAIC,OAAO,EAAEC,IAAI,CAAC,OAAO,EAChG;gBAACC,OAAOT;YAAK;QAEjB;QACA,IAAI,OAAOA,UAAU,YAAYA,UAAU,QAAQ,UAAUA,SAASA,MAAMU,IAAI,KAAK,UAAU;YAC7F,OAAO5B,iBAAiBgB,KAAK,CAAC,CAAC;QACjC;QAEA,MAAME;IACR;AACF"}
|
package/oclif.manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/codegen",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"description": "Codegen toolkit for Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"chokidar": "^3.6.0",
|
|
52
52
|
"debug": "^4.4.3",
|
|
53
53
|
"globby": "^11.1.0",
|
|
54
|
-
"groq": "^
|
|
54
|
+
"groq": "^6.0.0",
|
|
55
55
|
"groq-js": "^1.30.2",
|
|
56
56
|
"json5": "^2.2.3",
|
|
57
57
|
"lodash-es": "^4.18.1",
|
|
@@ -81,6 +81,7 @@
|
|
|
81
81
|
"lint-staged": "^16.4.0",
|
|
82
82
|
"oclif": "^4.23.10",
|
|
83
83
|
"oxfmt": "^0.53.0",
|
|
84
|
+
"pkg-pr-new": "^0.0.75",
|
|
84
85
|
"prettier-plugin-packagejson": "^3.0.2",
|
|
85
86
|
"rimraf": "^5.0.10",
|
|
86
87
|
"tinyglobby": "^0.2.17",
|