@sanity/pkg-utils 1.2.0 → 1.4.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.
Files changed (105) hide show
  1. package/README.md +8 -0
  2. package/dist/__printPackageTree-793a302b.cjs +75 -0
  3. package/dist/__printPackageTree-793a302b.cjs.map +1 -0
  4. package/dist/__resolveBuildContext-1b29cff2.cjs +336 -0
  5. package/dist/__resolveBuildContext-1b29cff2.cjs.map +1 -0
  6. package/dist/_build-08236974.cjs +169 -0
  7. package/dist/_build-08236974.cjs.map +1 -0
  8. package/dist/_buildAction-586dfb5f.cjs +43 -0
  9. package/dist/_buildAction-586dfb5f.cjs.map +1 -0
  10. package/dist/_check-9d180462.cjs +16 -0
  11. package/dist/_check-9d180462.cjs.map +1 -0
  12. package/dist/_index-c5bd1bc8.cjs +565 -0
  13. package/dist/_index-c5bd1bc8.cjs.map +1 -0
  14. package/dist/_statusAction-385b72dd.cjs +27 -0
  15. package/dist/_statusAction-385b72dd.cjs.map +1 -0
  16. package/dist/_watch-0623c005.cjs +217 -0
  17. package/dist/_watch-0623c005.cjs.map +1 -0
  18. package/dist/_watchAction-4b8d7301.cjs +42 -0
  19. package/dist/_watchAction-4b8d7301.cjs.map +1 -0
  20. package/dist/cli.cjs +14 -30
  21. package/dist/cli.cjs.map +1 -1
  22. package/dist/index.cjs +42 -16
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +278 -154
  25. package/dist/index.js +965 -5129
  26. package/dist/index.js.map +1 -1
  27. package/package.json +25 -9
  28. package/src/cli/buildAction.ts +8 -0
  29. package/src/cli/index.ts +41 -0
  30. package/src/cli/statusAction.ts +8 -0
  31. package/src/cli/watchAction.ts +8 -0
  32. package/src/node/_core/_defaults.ts +9 -0
  33. package/src/{core → node/_core}/_isRecord.ts +1 -3
  34. package/src/{core → node/_core}/config/_loadConfig.ts +1 -3
  35. package/src/node/_core/config/_resolveConfigProperty.ts +15 -0
  36. package/src/{core → node/_core}/config/defineConfig.ts +1 -3
  37. package/src/{core → node/_core}/config/index.ts +0 -0
  38. package/src/node/_core/config/types.ts +69 -0
  39. package/src/node/_core/contexts/buildContext.ts +34 -0
  40. package/src/node/_core/contexts/index.ts +1 -0
  41. package/src/{core → node/_core}/index.ts +2 -0
  42. package/src/node/_core/pkg/_loadPkg.ts +54 -0
  43. package/src/node/_core/pkg/_loadPkgWithReporting.ts +29 -0
  44. package/src/node/_core/pkg/_parseExports.ts +45 -0
  45. package/src/{core → node/_core}/pkg/_types.ts +17 -5
  46. package/src/{core → node/_core}/pkg/index.ts +1 -0
  47. package/src/{build/tasks/dts → node/_core/ts}/_loadTSConfig.ts +1 -0
  48. package/src/node/_core/ts/index.ts +1 -0
  49. package/src/node/_getFilesize.ts +8 -0
  50. package/src/node/_globFiles.ts +5 -0
  51. package/src/node/_printExtractMessages.ts +56 -0
  52. package/src/node/_printPackageTree.ts +72 -0
  53. package/src/node/_resolveBrowserTarget.ts +35 -0
  54. package/src/node/_resolveBrowserslistVersions.ts +60 -0
  55. package/src/node/_resolveBuildContext.ts +104 -0
  56. package/src/node/_resolveBuildTasks.ts +129 -0
  57. package/src/node/_resolveNodeTarget.ts +15 -0
  58. package/src/node/_resolveWatchTasks.ts +129 -0
  59. package/src/node/_spinner.ts +26 -0
  60. package/src/node/_tasks/_types.ts +57 -0
  61. package/src/node/_tasks/dts/_DtsError.ts +11 -0
  62. package/src/{build/tasks → node/_tasks}/dts/_buildTypes.ts +20 -16
  63. package/src/node/_tasks/dts/_createApiExtractorConfig.ts +41 -0
  64. package/src/{build/tasks → node/_tasks}/dts/_createTSDocConfig.ts +2 -6
  65. package/src/node/_tasks/dts/_doExtract.ts +46 -0
  66. package/src/node/_tasks/dts/_dtsTask.ts +38 -0
  67. package/src/node/_tasks/dts/_dtsWatchTask.ts +43 -0
  68. package/src/node/_tasks/dts/_extractTypes.ts +161 -0
  69. package/src/node/_tasks/dts/_types.ts +25 -0
  70. package/src/node/_tasks/dts/index.ts +3 -0
  71. package/src/node/_tasks/index.ts +19 -0
  72. package/src/node/_tasks/rollup/_resolveRollupConfig.ts +132 -0
  73. package/src/node/_tasks/rollup/_rollupTask.ts +71 -0
  74. package/src/node/_tasks/rollup/_rollupWatchTask.ts +78 -0
  75. package/src/node/_tasks/rollup/index.ts +2 -0
  76. package/src/node/_watchConfigFiles.ts +52 -0
  77. package/src/node/_watchFiles.ts +29 -0
  78. package/src/node/build.ts +47 -0
  79. package/src/node/check.ts +14 -0
  80. package/src/node/index.ts +6 -0
  81. package/src/node/watch.ts +56 -0
  82. package/dist/_build-e608bb7c.cjs +0 -5458
  83. package/dist/_build-e608bb7c.cjs.map +0 -1
  84. package/src/build/_parseTasks.ts +0 -87
  85. package/src/build/_types.ts +0 -25
  86. package/src/build/build.ts +0 -82
  87. package/src/build/index.ts +0 -2
  88. package/src/build/tasks/_types.ts +0 -8
  89. package/src/build/tasks/dts/_createApiExtractorConfig.ts +0 -88
  90. package/src/build/tasks/dts/_extractTypes.ts +0 -100
  91. package/src/build/tasks/dts/dts.ts +0 -59
  92. package/src/build/tasks/dts/index.ts +0 -1
  93. package/src/build/tasks/extract/_extractTask.ts +0 -49
  94. package/src/build/tasks/extract/index.ts +0 -1
  95. package/src/build/tasks/index.ts +0 -4
  96. package/src/build/tasks/rollup/_resolveRollupConfig.ts +0 -95
  97. package/src/build/tasks/rollup/index.ts +0 -1
  98. package/src/build/tasks/rollup/rollup.ts +0 -96
  99. package/src/cli.ts +0 -25
  100. package/src/core/_defaults.ts +0 -9
  101. package/src/core/config/_resolveConfigProperty.ts +0 -12
  102. package/src/core/config/types.ts +0 -56
  103. package/src/core/pkg/_loadPkg.ts +0 -13
  104. package/src/core/pkg/_parseExports.ts +0 -40
  105. package/src/index.ts +0 -2
@@ -0,0 +1,52 @@
1
+ import path from 'path'
2
+ import chalk from 'chalk'
3
+ import {Observable} from 'rxjs'
4
+ import {distinctUntilChanged, scan, startWith} from 'rxjs/operators'
5
+ import {_globFiles} from './_globFiles'
6
+ import {_watchFiles} from './_watchFiles'
7
+
8
+ export async function _watchConfigFiles(options: {cwd: string}): Promise<Observable<string[]>> {
9
+ const {cwd} = options
10
+
11
+ const initialFiles = await _globFiles([
12
+ path.resolve(cwd, 'package.json'),
13
+ path.resolve(cwd, 'package.config.cjs'),
14
+ path.resolve(cwd, 'package.config.js'),
15
+ path.resolve(cwd, 'package.config.ts'),
16
+ ])
17
+
18
+ const fileEvent$ = _watchFiles([
19
+ path.resolve(cwd, 'package.json'),
20
+ path.resolve(cwd, 'package.config.cjs'),
21
+ path.resolve(cwd, 'package.config.js'),
22
+ path.resolve(cwd, 'package.config.ts'),
23
+ ])
24
+
25
+ return fileEvent$.pipe(
26
+ scan((files, fileEvent) => {
27
+ if (fileEvent.type === 'add') {
28
+ return files.concat(fileEvent.file)
29
+ }
30
+
31
+ if (fileEvent.type === 'unlink') {
32
+ return files.filter((f) => f !== fileEvent.file)
33
+ }
34
+
35
+ if (fileEvent.type === 'change') {
36
+ /* eslint-disable no-console */
37
+ console.log(
38
+ '--------------------------------------------------------------------------------'
39
+ )
40
+ console.log(chalk.blue('info '), path.relative(cwd, fileEvent.file), 'changed')
41
+ console.log('')
42
+ /* eslint-enable no-console */
43
+
44
+ return files.slice(0) // trigger update
45
+ }
46
+
47
+ return files
48
+ }, initialFiles),
49
+ startWith(initialFiles),
50
+ distinctUntilChanged()
51
+ )
52
+ }
@@ -0,0 +1,29 @@
1
+ import chokidar from 'chokidar'
2
+ import {Observable} from 'rxjs'
3
+
4
+ export interface _FileEvent {
5
+ type: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
6
+ file: string
7
+ }
8
+
9
+ export function _watchFiles(patterns: string[]): Observable<_FileEvent> {
10
+ return new Observable((observer) => {
11
+ const watcher = chokidar.watch(patterns, {
12
+ ignoreInitial: true,
13
+ })
14
+
15
+ function _handleFileEvent(
16
+ type: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
17
+ file: string
18
+ ) {
19
+ observer.next({type, file})
20
+ }
21
+
22
+ watcher.on('all', _handleFileEvent)
23
+
24
+ return () => {
25
+ watcher.off('all', _handleFileEvent)
26
+ watcher.close()
27
+ }
28
+ })
29
+ }
@@ -0,0 +1,47 @@
1
+ import {_loadConfig, _loadPkgWithReporting} from './_core'
2
+ import {_printPackageTree} from './_printPackageTree'
3
+ import {_resolveBuildContext} from './_resolveBuildContext'
4
+ import {_resolveBuildTasks} from './_resolveBuildTasks'
5
+ import {_spinner} from './_spinner'
6
+ import {_BuildTask, _TaskHandler, _buildTaskHandlers} from './_tasks'
7
+
8
+ /** @public */
9
+ export async function build(options: {cwd: string; tsconfig?: string}): Promise<void> {
10
+ const {cwd, tsconfig = 'tsconfig.json'} = options
11
+
12
+ const pkg = await _loadPkgWithReporting({cwd})
13
+ const config = await _loadConfig({cwd})
14
+
15
+ const ctx = await _resolveBuildContext({config, cwd, pkg, tsconfig})
16
+
17
+ const buildTasks = _resolveBuildTasks(ctx)
18
+
19
+ for (const task of buildTasks) {
20
+ const handler = _buildTaskHandlers[task.type] as _TaskHandler<_BuildTask>
21
+ const taskName = handler.name(ctx, task)
22
+
23
+ const spinner = _spinner(taskName)
24
+
25
+ try {
26
+ const result = await handler.exec(ctx, task).toPromise()
27
+
28
+ spinner.complete()
29
+ ctx.logger.log()
30
+
31
+ handler.complete(ctx, task, result)
32
+ } catch (err) {
33
+ spinner.error()
34
+
35
+ if (err instanceof Error) {
36
+ ctx.logger.error(err.message.replaceAll(cwd, '.'))
37
+ ctx.logger.log()
38
+ }
39
+
40
+ handler.error(ctx, task, err)
41
+
42
+ process.exit(1)
43
+ }
44
+ }
45
+
46
+ _printPackageTree(ctx)
47
+ }
@@ -0,0 +1,14 @@
1
+ import {_loadConfig, _loadPkgWithReporting} from './_core'
2
+ import {_printPackageTree} from './_printPackageTree'
3
+ import {_resolveBuildContext} from './_resolveBuildContext'
4
+
5
+ /** @public */
6
+ export async function check(options: {cwd: string; tsconfig?: string}): Promise<void> {
7
+ const {cwd, tsconfig = 'tsconfig.json'} = options
8
+
9
+ const pkg = await _loadPkgWithReporting({cwd})
10
+ const config = await _loadConfig({cwd})
11
+ const ctx = await _resolveBuildContext({config, cwd, pkg, tsconfig})
12
+
13
+ _printPackageTree(ctx)
14
+ }
@@ -0,0 +1,6 @@
1
+ export * from './_core'
2
+ export * from './_resolveBuildTasks'
3
+ export * from './_tasks'
4
+ export * from './build'
5
+ export * from './check'
6
+ export * from './watch'
@@ -0,0 +1,56 @@
1
+ import path from 'path'
2
+ import {switchMap} from 'rxjs'
3
+ import {_loadConfig, _loadPkgWithReporting} from './_core'
4
+ import {_resolveBuildContext} from './_resolveBuildContext'
5
+ import {_resolveWatchTasks} from './_resolveWatchTasks'
6
+ import {_WatchTask, _TaskHandler, _watchTaskHandlers} from './_tasks'
7
+ import {_watchConfigFiles} from './_watchConfigFiles'
8
+
9
+ /** @public */
10
+ export async function watch(options: {cwd: string; tsconfig?: string}): Promise<void> {
11
+ const {cwd, tsconfig = 'tsconfig.json'} = options
12
+
13
+ const configFiles$ = await _watchConfigFiles({cwd})
14
+
15
+ const ctx$ = configFiles$.pipe(
16
+ switchMap(async (configFiles) => {
17
+ const _files = configFiles.map((f) => path.relative(cwd, f))
18
+
19
+ const packageJsonPath = _files.find((f) => f === 'package.json')
20
+
21
+ if (!packageJsonPath) {
22
+ throw new Error('missing package.json')
23
+ }
24
+
25
+ const pkg = await _loadPkgWithReporting({cwd})
26
+ const config = await _loadConfig({cwd})
27
+
28
+ return _resolveBuildContext({config, cwd, pkg, tsconfig})
29
+ })
30
+ )
31
+
32
+ ctx$.subscribe(async (ctx) => {
33
+ const watchTasks = _resolveWatchTasks(ctx)
34
+
35
+ for (const task of watchTasks) {
36
+ const handler = _watchTaskHandlers[task.type] as _TaskHandler<_WatchTask, any>
37
+ const result$ = handler.exec(ctx, task)
38
+
39
+ result$.subscribe({
40
+ error: (err) => {
41
+ ctx.logger.error(err)
42
+ ctx.logger.log()
43
+
44
+ process.exit(1)
45
+ },
46
+ next: (result) => {
47
+ handler.complete(ctx, task, result)
48
+ },
49
+ complete: () => {
50
+ ctx.logger.success(handler.name(ctx, task))
51
+ ctx.logger.log()
52
+ },
53
+ })
54
+ }
55
+ })
56
+ }