@sanity/codegen 5.8.0 → 5.9.0-watch-mode.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.
- package/dist/actions/streamProcessor.js +3 -2
- package/dist/actions/streamProcessor.js.map +1 -1
- package/dist/actions/typegenGenerate.js +3 -2
- package/dist/actions/typegenGenerate.js.map +1 -1
- package/dist/actions/typegenGenerate.worker.js +1344 -14
- package/dist/actions/typegenGenerate.worker.js.map +1 -1
- package/dist/actions/typegenWatch.js +11 -5
- package/dist/actions/typegenWatch.js.map +1 -1
- package/dist/actions/types.js.map +1 -1
- package/dist/commands/typegen/generate.js +56 -47
- package/dist/commands/typegen/generate.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/utils/config.js +11 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/debug.js +4 -0
- package/dist/utils/debug.js.map +1 -0
- package/oclif.manifest.json +2 -2
- package/package.json +5 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { debug } from 'node:console';
|
|
2
1
|
import { writeFile } from 'node:fs/promises';
|
|
3
2
|
import { spinner } from '@sanity/cli-core/ux';
|
|
4
3
|
import { format, resolveConfig as resolvePrettierConfig } from 'prettier';
|
|
5
4
|
import { count } from '../utils/count.js';
|
|
5
|
+
import { debug } from '../utils/debug.js';
|
|
6
6
|
import { formatPath } from '../utils/formatPath.js';
|
|
7
7
|
import { getMessage } from '../utils/getMessage.js';
|
|
8
8
|
import { percent } from '../utils/percent.js';
|
|
@@ -20,8 +20,8 @@ import { generatedFileWarning } from './generatedFileWarning.js';
|
|
|
20
20
|
const start = Date.now();
|
|
21
21
|
const { formatGeneratedCode, generates, schema } = options;
|
|
22
22
|
let code = '';
|
|
23
|
+
const spin = spinner().start(`Loading schema…`);
|
|
23
24
|
try {
|
|
24
|
-
const spin = spinner().start(`Loading schema…`);
|
|
25
25
|
await receiver.event.loadedSchema();
|
|
26
26
|
spin.succeed(`Schema loaded from ${formatPath(schema ?? '')}`);
|
|
27
27
|
spin.start('Generating schema types…');
|
|
@@ -97,6 +97,7 @@ import { generatedFileWarning } from './generatedFileWarning.js';
|
|
|
97
97
|
code
|
|
98
98
|
};
|
|
99
99
|
} catch (err) {
|
|
100
|
+
spin.fail();
|
|
100
101
|
debug('error generating types', err);
|
|
101
102
|
throw err;
|
|
102
103
|
} finally{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/actions/streamProcessor.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/actions/streamProcessor.ts"],"sourcesContent":["import {writeFile} from 'node:fs/promises'\n\nimport {spinner} from '@sanity/cli-core/ux'\nimport {WorkerChannelReceiver} from '@sanity/worker-channels'\nimport {format, resolveConfig as resolvePrettierConfig} from 'prettier'\n\nimport {TypeGenConfig} from '../readConfig.js'\nimport {count} from '../utils/count.js'\nimport {debug} from '../utils/debug.js'\nimport {formatPath} from '../utils/formatPath.js'\nimport {getMessage} from '../utils/getMessage.js'\nimport {percent} from '../utils/percent.js'\nimport {generatedFileWarning} from './generatedFileWarning.js'\nimport {TypegenWorkerChannel} from './types.js'\n\n/**\n * Processes the event stream from a typegen worker thread.\n *\n * Listens to worker channel events, displays progress via CLI spinners,\n * writes the generated types to disk, and optionally formats with prettier.\n *\n * @param receiver - Worker channel receiver for typegen events\n * @param options - Typegen configuration options\n * @returns Generation result containing the generated code and statistics\n */\nexport async function processTypegenWorkerStream(\n receiver: WorkerChannelReceiver<TypegenWorkerChannel>,\n options: TypeGenConfig,\n) {\n const start = Date.now()\n const {formatGeneratedCode, generates, schema} = options\n let code = ''\n\n const spin = spinner().start(`Loading schema…`)\n\n try {\n await receiver.event.loadedSchema()\n spin.succeed(`Schema loaded from ${formatPath(schema ?? '')}`)\n\n spin.start('Generating schema types…')\n const {expectedFileCount} = await receiver.event.typegenStarted()\n const {schemaTypeDeclarations} = await receiver.event.generatedSchemaTypes()\n const schemaTypesCount = schemaTypeDeclarations.length\n\n spin.text = 'Generating query types…'\n\n let queriesCount = 0\n let evaluatedFiles = 0\n let filesWithErrors = 0\n let queryFilesCount = 0\n let typeNodesGenerated = 0\n let unknownTypeNodesGenerated = 0\n let emptyUnionTypeNodesGenerated = 0\n\n for await (const {errors, queries} of receiver.stream.evaluatedModules()) {\n evaluatedFiles++\n queriesCount += queries.length\n queryFilesCount += queries.length > 0 ? 1 : 0\n filesWithErrors += errors.length > 0 ? 1 : 0\n\n for (const {stats} of queries) {\n typeNodesGenerated += stats.allTypes\n unknownTypeNodesGenerated += stats.unknownTypes\n emptyUnionTypeNodesGenerated += stats.emptyUnions\n }\n\n for (const error of errors) {\n spin.fail(getMessage(error))\n }\n\n if (!spin.isSpinning) {\n spin.start()\n }\n\n spin.text =\n `Generating query types… (${percent(evaluatedFiles / expectedFileCount)})\\n` +\n ` └─ Processed ${count(evaluatedFiles)} of ${count(expectedFileCount, 'files')}. ` +\n `Found ${count(queriesCount, 'queries', 'query')} from ${count(queryFilesCount, 'files')}.`\n }\n\n const result = await receiver.event.typegenComplete()\n code = `${generatedFileWarning}${result.code}`\n await writeFile(generates, code)\n\n let formattingError = false\n if (formatGeneratedCode) {\n spin.text = `Formatting generated types with prettier…`\n\n try {\n const prettierConfig = await resolvePrettierConfig(generates)\n const formattedCode = await format(code, {\n ...prettierConfig,\n parser: 'typescript' as const,\n })\n await writeFile(generates, formattedCode)\n } catch (err) {\n formattingError = true\n spin.warn(`Failed to format generated types with prettier: ${getMessage(err)}`)\n }\n }\n\n if (filesWithErrors > 0) {\n spin.warn(`Encountered errors in ${count(filesWithErrors, 'files')} while generating types`)\n }\n\n const stats = {\n duration: Date.now() - start,\n emptyUnionTypeNodesGenerated,\n filesWithErrors,\n outputSize: Buffer.byteLength(code),\n queriesCount,\n queryFilesCount,\n schemaTypesCount,\n typeNodesGenerated,\n unknownTypeNodesGenerated,\n unknownTypeNodesRatio:\n typeNodesGenerated > 0 ? unknownTypeNodesGenerated / typeNodesGenerated : 0,\n }\n\n let successText =\n `Successfully generated types to ${formatPath(generates)} in ${Number(stats.duration).toFixed(0)}ms` +\n `\\n └─ ${count(queriesCount, 'queries', 'query')} and ${count(schemaTypesCount, 'schema types', 'schema type')}` +\n `\\n └─ found queries in ${count(queryFilesCount, 'files', 'file')} after evaluating ${count(evaluatedFiles, 'files', 'file')}`\n\n if (formatGeneratedCode) {\n successText += `\\n └─ ${formattingError ? 'an error occured during formatting' : 'formatted the generated code with prettier'}`\n }\n\n spin.succeed(successText)\n\n return {\n ...stats,\n code,\n }\n } catch (err) {\n spin.fail()\n debug('error generating types', err)\n throw err\n } finally {\n receiver.unsubscribe()\n }\n}\n"],"names":["writeFile","spinner","format","resolveConfig","resolvePrettierConfig","count","debug","formatPath","getMessage","percent","generatedFileWarning","processTypegenWorkerStream","receiver","options","start","Date","now","formatGeneratedCode","generates","schema","code","spin","event","loadedSchema","succeed","expectedFileCount","typegenStarted","schemaTypeDeclarations","generatedSchemaTypes","schemaTypesCount","length","text","queriesCount","evaluatedFiles","filesWithErrors","queryFilesCount","typeNodesGenerated","unknownTypeNodesGenerated","emptyUnionTypeNodesGenerated","errors","queries","stream","evaluatedModules","stats","allTypes","unknownTypes","emptyUnions","error","fail","isSpinning","result","typegenComplete","formattingError","prettierConfig","formattedCode","parser","err","warn","duration","outputSize","Buffer","byteLength","unknownTypeNodesRatio","successText","Number","toFixed","unsubscribe"],"mappings":"AAAA,SAAQA,SAAS,QAAO,mBAAkB;AAE1C,SAAQC,OAAO,QAAO,sBAAqB;AAE3C,SAAQC,MAAM,EAAEC,iBAAiBC,qBAAqB,QAAO,WAAU;AAGvE,SAAQC,KAAK,QAAO,oBAAmB;AACvC,SAAQC,KAAK,QAAO,oBAAmB;AACvC,SAAQC,UAAU,QAAO,yBAAwB;AACjD,SAAQC,UAAU,QAAO,yBAAwB;AACjD,SAAQC,OAAO,QAAO,sBAAqB;AAC3C,SAAQC,oBAAoB,QAAO,4BAA2B;AAG9D;;;;;;;;;CASC,GACD,OAAO,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B,MAAMe,QAAQ,EAAEO,OAAO,CAAC,GAAG,EAAE,CAAC,GACpG,CAAC,OAAO,EAAE5D,MAAM2B,cAAc,WAAW,SAAS,KAAK,EAAE3B,MAAMwB,kBAAkB,gBAAgB,gBAAgB,GACjH,CAAC,wBAAwB,EAAExB,MAAM8B,iBAAiB,SAAS,QAAQ,kBAAkB,EAAE9B,MAAM4B,gBAAgB,SAAS,SAAS;QAEjI,IAAIhB,qBAAqB;YACvB8C,eAAe,CAAC,OAAO,EAAEX,kBAAkB,uCAAuC,8CAA8C;QAClI;QAEA/B,KAAKG,OAAO,CAACuC;QAEb,OAAO;YACL,GAAGpB,KAAK;YACRvB;QACF;IACF,EAAE,OAAOoC,KAAK;QACZnC,KAAK2B,IAAI;QACT1C,MAAM,0BAA0BkD;QAChC,MAAMA;IACR,SAAU;QACR5C,SAASsD,WAAW;IACtB;AACF"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { debug } from 'node:console';
|
|
2
1
|
import { mkdir } from 'node:fs/promises';
|
|
3
2
|
import { dirname, isAbsolute, join } from 'node:path';
|
|
4
3
|
import { env } from 'node:process';
|
|
5
4
|
import { Worker } from 'node:worker_threads';
|
|
6
5
|
import { WorkerChannelReceiver } from '@sanity/worker-channels';
|
|
6
|
+
import { prepareConfig } from '../utils/config.js';
|
|
7
|
+
import { debug } from '../utils/debug.js';
|
|
7
8
|
import { processTypegenWorkerStream } from './streamProcessor.js';
|
|
8
9
|
/**
|
|
9
10
|
* Runs a single typegen generation.
|
|
@@ -15,7 +16,7 @@ import { processTypegenWorkerStream } from './streamProcessor.js';
|
|
|
15
16
|
* @returns Generation result containing the generated code and statistics
|
|
16
17
|
*/ export async function runTypegenGenerate(options) {
|
|
17
18
|
const { config, workDir } = options;
|
|
18
|
-
const { formatGeneratedCode, generates, overloadClientMethods, path, schema } = config;
|
|
19
|
+
const { formatGeneratedCode, generates, overloadClientMethods, path, schema } = prepareConfig(config);
|
|
19
20
|
const outputPath = isAbsolute(generates) ? generates : join(workDir, generates);
|
|
20
21
|
// create output dir if it isn't there
|
|
21
22
|
const outputDir = dirname(outputPath);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/actions/typegenGenerate.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/actions/typegenGenerate.ts"],"sourcesContent":["import {mkdir} from 'node:fs/promises'\nimport {dirname, isAbsolute, join} from 'node:path'\nimport {env} from 'node:process'\nimport {Worker} from 'node:worker_threads'\n\nimport {WorkerChannelReceiver} from '@sanity/worker-channels'\n\nimport {prepareConfig} from '../utils/config.js'\nimport {debug} from '../utils/debug.js'\nimport {processTypegenWorkerStream} from './streamProcessor.js'\nimport {\n type GenerationResult,\n type RunTypegenOptions,\n TypegenGenerateTypesWorkerData,\n TypegenWorkerChannel,\n} from './types.js'\n\n/**\n * Runs a single typegen generation.\n *\n * This is the programmatic API for generating TypeScript types from GROQ queries.\n * It spawns a worker thread to perform the generation and displays progress via CLI spinners.\n *\n * @param options - Configuration options including typegen config and working directory\n * @returns Generation result containing the generated code and statistics\n */\nexport async function runTypegenGenerate(options: RunTypegenOptions): Promise<GenerationResult> {\n const {config, workDir} = options\n\n const {formatGeneratedCode, generates, overloadClientMethods, path, schema} =\n prepareConfig(config)\n\n const outputPath = isAbsolute(generates) ? generates : join(workDir, generates)\n\n // create output dir if it isn't there\n const outputDir = dirname(outputPath)\n await mkdir(outputDir, {recursive: true})\n\n // set up worker\n const workerPath = new URL('../actions/typegenGenerate.worker.js', import.meta.url)\n const workerData: TypegenGenerateTypesWorkerData = {\n overloadClientMethods,\n schemaPath: schema,\n searchPath: path,\n workDir,\n }\n const worker = new Worker(workerPath, {env, workerData})\n\n try {\n const result = await processTypegenWorkerStream(\n WorkerChannelReceiver.from<TypegenWorkerChannel>(worker),\n {\n formatGeneratedCode,\n generates: outputPath,\n overloadClientMethods,\n path,\n schema,\n },\n )\n\n return result\n } catch (err) {\n debug('error generating types', err)\n throw err\n } finally {\n worker.terminate()\n }\n}\n"],"names":["mkdir","dirname","isAbsolute","join","env","Worker","WorkerChannelReceiver","prepareConfig","debug","processTypegenWorkerStream","runTypegenGenerate","options","config","workDir","formatGeneratedCode","generates","overloadClientMethods","path","schema","outputPath","outputDir","recursive","workerPath","URL","url","workerData","schemaPath","searchPath","worker","result","from","err","terminate"],"mappings":"AAAA,SAAQA,KAAK,QAAO,mBAAkB;AACtC,SAAQC,OAAO,EAAEC,UAAU,EAAEC,IAAI,QAAO,YAAW;AACnD,SAAQC,GAAG,QAAO,eAAc;AAChC,SAAQC,MAAM,QAAO,sBAAqB;AAE1C,SAAQC,qBAAqB,QAAO,0BAAyB;AAE7D,SAAQC,aAAa,QAAO,qBAAoB;AAChD,SAAQC,KAAK,QAAO,oBAAmB;AACvC,SAAQC,0BAA0B,QAAO,uBAAsB;AAQ/D;;;;;;;;CAQC,GACD,OAAO,eAAeC,mBAAmBC,OAA0B;IACjE,MAAM,EAACC,MAAM,EAAEC,OAAO,EAAC,GAAGF;IAE1B,MAAM,EAACG,mBAAmB,EAAEC,SAAS,EAAEC,qBAAqB,EAAEC,IAAI,EAAEC,MAAM,EAAC,GACzEX,cAAcK;IAEhB,MAAMO,aAAajB,WAAWa,aAAaA,YAAYZ,KAAKU,SAASE;IAErE,sCAAsC;IACtC,MAAMK,YAAYnB,QAAQkB;IAC1B,MAAMnB,MAAMoB,WAAW;QAACC,WAAW;IAAI;IAEvC,gBAAgB;IAChB,MAAMC,aAAa,IAAIC,IAAI,wCAAwC,YAAYC,GAAG;IAClF,MAAMC,aAA6C;QACjDT;QACAU,YAAYR;QACZS,YAAYV;QACZJ;IACF;IACA,MAAMe,SAAS,IAAIvB,OAAOiB,YAAY;QAAClB;QAAKqB;IAAU;IAEtD,IAAI;QACF,MAAMI,SAAS,MAAMpB,2BACnBH,sBAAsBwB,IAAI,CAAuBF,SACjD;YACEd;YACAC,WAAWI;YACXH;YACAC;YACAC;QACF;QAGF,OAAOW;IACT,EAAE,OAAOE,KAAK;QACZvB,MAAM,0BAA0BuB;QAChC,MAAMA;IACR,SAAU;QACRH,OAAOI,SAAS;IAClB;AACF"}
|