@sanity/pkg-utils 6.10.12 → 6.10.13-canary.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/pkg-utils",
3
- "version": "6.10.12",
3
+ "version": "6.10.13-canary.0",
4
4
  "description": "Simple utilities for modern npm packages.",
5
5
  "keywords": [
6
6
  "sanity-io",
@@ -146,7 +146,7 @@
146
146
  "typescript": "5.5.4",
147
147
  "vitest": "^2.0.5",
148
148
  "vitest-github-actions-reporter": "^0.11.1",
149
- "@sanity/pkg-utils": "6.10.12"
149
+ "@sanity/pkg-utils": "6.10.13-canary.0"
150
150
  },
151
151
  "peerDependencies": {
152
152
  "typescript": "5.4.x || 5.5.x"
@@ -1,10 +1,16 @@
1
+ import path from 'node:path'
2
+
3
+ import type {ExtractorMessage} from '@microsoft/api-extractor'
1
4
  import chalk from 'chalk'
5
+ import rimraf from 'rimraf'
2
6
  import {Observable} from 'rxjs'
7
+ import ts from 'typescript'
3
8
 
4
9
  import {printExtractMessages} from '../../printExtractMessages'
5
10
  import type {TaskHandler} from '../types'
6
- import {doExtract} from './doExtract'
11
+ import {buildTypes} from './buildTypes'
7
12
  import {DtsError} from './DtsError'
13
+ import {extractTypes} from './extractTypes'
8
14
  import type {DtsResult, DtsWatchTask} from './types'
9
15
 
10
16
  /** @internal */
@@ -22,21 +28,132 @@ export const dtsWatchTask: TaskHandler<DtsWatchTask, DtsResult> = {
22
28
  }),
23
29
  ].join('\n'),
24
30
  exec: (ctx, task) => {
31
+ const {config, cwd, files, logger, strict, ts: tsContext, bundledPackages} = ctx
32
+
25
33
  return new Observable((observer) => {
26
- doExtract(ctx, task)
27
- .then((result) => {
28
- observer.next(result)
29
- observer.complete()
30
- })
31
- .catch((err) => observer.error(err))
34
+ if (!tsContext.config || !tsContext.configPath) {
35
+ observer.next({type: 'dts', messages: [], results: []})
36
+ observer.complete()
37
+
38
+ return
39
+ }
40
+
41
+ const {outDir, rootDir = cwd} = tsContext.config.options
42
+
43
+ if (!outDir) {
44
+ observer.error(new Error('tsconfig.json is missing `compilerOptions.outDir`'))
45
+
46
+ return
47
+ }
48
+
49
+ const tmpPath = path.resolve(outDir, '__tmp__')
50
+
51
+ // Add this block to build types initially
52
+ buildTypes({
53
+ cwd,
54
+ logger,
55
+ outDir: tmpPath,
56
+ tsconfig: tsContext.config,
57
+ strict,
58
+ }).catch((err) => {
59
+ observer.error(err)
60
+ })
61
+
62
+ const host = ts.createWatchCompilerHost(
63
+ tsContext.configPath,
64
+ {
65
+ ...tsContext.config.options,
66
+ declaration: true,
67
+ declarationDir: tmpPath,
68
+ emitDeclarationOnly: true,
69
+ noEmit: false,
70
+ noEmitOnError: strict ? true : (tsContext.config.options.noEmitOnError ?? true),
71
+ outDir: tmpPath,
72
+ },
73
+ ts.sys,
74
+ ts.createEmitAndSemanticDiagnosticsBuilderProgram,
75
+ (diagnostic) => {
76
+ logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
77
+ },
78
+ (diagnostic) => {
79
+ logger.info(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
80
+ },
81
+ )
82
+
83
+ const origPostProgramCreate = host.afterProgramCreate
84
+
85
+ host.afterProgramCreate = async (program) => {
86
+ origPostProgramCreate?.(program)
87
+
88
+ const messages: ExtractorMessage[] = []
89
+ const results: {sourcePath: string; filePaths: string[]}[] = []
90
+
91
+ for (const entry of task.entries) {
92
+ const exportPath = entry.exportPath === '.' ? './index' : entry.exportPath
93
+
94
+ const sourceTypesPath = path.resolve(
95
+ tmpPath,
96
+ path.relative(rootDir, path.resolve(cwd, entry.sourcePath)).replace(/\.ts$/, '.d.ts'),
97
+ )
98
+
99
+ const targetPaths = entry.targetPaths.map((targetPath) => path.resolve(cwd, targetPath))
100
+ const filePaths = targetPaths.map((targetPath) => path.relative(outDir, targetPath))
101
+
102
+ try {
103
+ const result = await extractTypes({
104
+ bundledPackages: bundledPackages || [],
105
+ customTags: config?.extract?.customTags,
106
+ cwd,
107
+ distPath: outDir,
108
+ exportPath,
109
+ files,
110
+ filePaths,
111
+ projectPath: cwd,
112
+ rules: config?.extract?.rules,
113
+ sourceTypesPath: sourceTypesPath,
114
+ tsconfig: tsContext.config!,
115
+ tmpPath,
116
+ tsconfigPath: path.resolve(cwd, tsContext.configPath || 'tsconfig.json'),
117
+ })
118
+
119
+ messages.push(...result.messages)
120
+ results.push({sourcePath: path.resolve(cwd, entry.sourcePath), filePaths: targetPaths})
121
+ } catch (err) {
122
+ if (err instanceof DtsError) {
123
+ messages.push(...err.messages)
124
+ } else {
125
+ observer.error(err)
126
+
127
+ return
128
+ }
129
+ }
130
+ }
131
+
132
+ observer.next({type: 'dts', messages, results})
133
+ }
134
+
135
+ const watchProgram = ts.createWatchProgram(host)
136
+
137
+ return () => {
138
+ watchProgram.close()
139
+ rimraf.sync(tmpPath)
140
+ }
32
141
  })
33
142
  },
34
- complete: (ctx, _task, result) => {
143
+ complete: (ctx, task, result) => {
35
144
  const {logger} = ctx
36
145
 
37
146
  printExtractMessages(ctx, result.messages)
38
147
 
39
- logger.warn('watching typescript definitions is currently not supported')
148
+ logger.success(
149
+ `build type definitions\n ${task.entries
150
+ .map(
151
+ (entry) =>
152
+ ` - ${chalk.cyan(entry.importId)}: ${chalk.yellow(entry.sourcePath)} ${chalk.gray('→')} ${chalk.yellow(entry.targetPaths.join(', '))}`,
153
+ )
154
+ .join('\n ')}`,
155
+ )
156
+ logger.log('')
40
157
  logger.log()
41
158
  },
42
159
  error: (ctx, _task, err) => {