@sanity/pkg-utils 6.10.12 → 6.11.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.11.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.11.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,22 +28,131 @@ 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
+ buildTypes({
52
+ cwd,
53
+ logger,
54
+ outDir: tmpPath,
55
+ tsconfig: tsContext.config,
56
+ strict,
57
+ }).catch((err) => {
58
+ observer.error(err)
59
+ })
60
+
61
+ const host = ts.createWatchCompilerHost(
62
+ tsContext.configPath,
63
+ {
64
+ ...tsContext.config.options,
65
+ declaration: true,
66
+ declarationDir: tmpPath,
67
+ emitDeclarationOnly: true,
68
+ noEmit: false,
69
+ noEmitOnError: strict ? true : (tsContext.config.options.noEmitOnError ?? true),
70
+ outDir: tmpPath,
71
+ },
72
+ ts.sys,
73
+ ts.createEmitAndSemanticDiagnosticsBuilderProgram,
74
+ (diagnostic) => {
75
+ logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
76
+ },
77
+ (diagnostic) => {
78
+ logger.info(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
79
+ },
80
+ )
81
+
82
+ const origPostProgramCreate = host.afterProgramCreate
83
+
84
+ host.afterProgramCreate = async (program) => {
85
+ origPostProgramCreate?.(program)
86
+
87
+ const messages: ExtractorMessage[] = []
88
+ const results: {sourcePath: string; filePaths: string[]}[] = []
89
+
90
+ for (const entry of task.entries) {
91
+ const exportPath = entry.exportPath === '.' ? './index' : entry.exportPath
92
+
93
+ const sourceTypesPath = path.resolve(
94
+ tmpPath,
95
+ path.relative(rootDir, path.resolve(cwd, entry.sourcePath)).replace(/\.ts$/, '.d.ts'),
96
+ )
97
+
98
+ const targetPaths = entry.targetPaths.map((targetPath) => path.resolve(cwd, targetPath))
99
+ const filePaths = targetPaths.map((targetPath) => path.relative(outDir, targetPath))
100
+
101
+ try {
102
+ const result = await extractTypes({
103
+ bundledPackages: bundledPackages || [],
104
+ customTags: config?.extract?.customTags,
105
+ cwd,
106
+ distPath: outDir,
107
+ exportPath,
108
+ files,
109
+ filePaths,
110
+ projectPath: cwd,
111
+ rules: config?.extract?.rules,
112
+ sourceTypesPath: sourceTypesPath,
113
+ tsconfig: tsContext.config!,
114
+ tmpPath,
115
+ tsconfigPath: path.resolve(cwd, tsContext.configPath || 'tsconfig.json'),
116
+ })
117
+
118
+ messages.push(...result.messages)
119
+ results.push({sourcePath: path.resolve(cwd, entry.sourcePath), filePaths: targetPaths})
120
+ } catch (err) {
121
+ if (err instanceof DtsError) {
122
+ messages.push(...err.messages)
123
+ } else {
124
+ observer.error(err)
125
+
126
+ return
127
+ }
128
+ }
129
+ }
130
+
131
+ observer.next({type: 'dts', messages, results})
132
+ }
133
+
134
+ const watchProgram = ts.createWatchProgram(host)
135
+
136
+ return () => {
137
+ watchProgram.close()
138
+ rimraf.sync(tmpPath)
139
+ }
32
140
  })
33
141
  },
34
- complete: (ctx, _task, result) => {
142
+ complete: (ctx, task, result) => {
35
143
  const {logger} = ctx
36
144
 
37
145
  printExtractMessages(ctx, result.messages)
38
146
 
39
- logger.warn('watching typescript definitions is currently not supported')
40
- logger.log()
147
+ logger.success(
148
+ `build type definitions\n ${task.entries
149
+ .map(
150
+ (entry) =>
151
+ ` - ${chalk.cyan(entry.importId)}: ${chalk.yellow(entry.sourcePath)} ${chalk.gray('→')} ${chalk.yellow(entry.targetPaths.join(', '))}`,
152
+ )
153
+ .join('\n ')}`,
154
+ )
155
+ logger.log('')
41
156
  },
42
157
  error: (ctx, _task, err) => {
43
158
  const {logger} = ctx