@sanity/pkg-utils 1.3.1 → 1.5.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 (104) hide show
  1. package/README.md +8 -0
  2. package/dist/__printPackageTree-3ff3ecd3.cjs +76 -0
  3. package/dist/__printPackageTree-3ff3ecd3.cjs.map +1 -0
  4. package/dist/__resolveBuildContext-85cfca8d.cjs +337 -0
  5. package/dist/__resolveBuildContext-85cfca8d.cjs.map +1 -0
  6. package/dist/_build-683d9922.cjs +168 -0
  7. package/dist/_build-683d9922.cjs.map +1 -0
  8. package/dist/_buildAction-f593323a.cjs +44 -0
  9. package/dist/_buildAction-f593323a.cjs.map +1 -0
  10. package/dist/_check-6843c26d.cjs +118 -0
  11. package/dist/_check-6843c26d.cjs.map +1 -0
  12. package/dist/_checkAction-0bca6a14.cjs +29 -0
  13. package/dist/_checkAction-0bca6a14.cjs.map +1 -0
  14. package/dist/_index-5765fada.cjs +584 -0
  15. package/dist/_index-5765fada.cjs.map +1 -0
  16. package/dist/_watch-ec1b35d6.cjs +217 -0
  17. package/dist/_watch-ec1b35d6.cjs.map +1 -0
  18. package/dist/_watchAction-a388fc00.cjs +43 -0
  19. package/dist/_watchAction-a388fc00.cjs.map +1 -0
  20. package/dist/cli.cjs +14 -31
  21. package/dist/cli.cjs.map +1 -1
  22. package/dist/index.cjs +43 -16
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +282 -152
  25. package/dist/index.js +1055 -5198
  26. package/dist/index.js.map +1 -1
  27. package/package.json +37 -10
  28. package/src/cli/buildAction.ts +8 -0
  29. package/src/cli/checkAction.ts +9 -0
  30. package/src/cli/index.ts +42 -0
  31. package/src/cli/watchAction.ts +8 -0
  32. package/src/{core → node/_core}/_defaults.ts +1 -3
  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 +55 -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 +16 -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/{build/_parseWebTarget.ts → node/_resolveBrowserTarget.ts} +2 -2
  54. package/src/{build/_parseBrowserslistVersions.ts → node/_resolveBrowserslistVersions.ts} +1 -1
  55. package/src/node/_resolveBuildContext.ts +104 -0
  56. package/src/node/_resolveBuildTasks.ts +129 -0
  57. package/src/{build/_parseNodeTarget.ts → node/_resolveNodeTarget.ts} +2 -2
  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 +30 -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 +57 -0
  66. package/src/node/_tasks/dts/_dtsTask.ts +40 -0
  67. package/src/node/_tasks/dts/_dtsWatchTask.ts +43 -0
  68. package/src/node/_tasks/dts/_extractTypes.ts +164 -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 +73 -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 +136 -0
  80. package/src/node/index.ts +6 -0
  81. package/src/node/watch.ts +56 -0
  82. package/dist/_build-5bf997de.cjs +0 -5549
  83. package/dist/_build-5bf997de.cjs.map +0 -1
  84. package/src/build/_parseTasks.ts +0 -87
  85. package/src/build/_types.ts +0 -26
  86. package/src/build/build.ts +0 -101
  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/config/_resolveConfigProperty.ts +0 -14
  101. package/src/core/config/types.ts +0 -52
  102. package/src/core/pkg/_loadPkg.ts +0 -13
  103. package/src/core/pkg/_parseExports.ts +0 -40
  104. 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,136 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import path from 'path'
4
+ import chalk from 'chalk'
5
+ import esbuild, {BuildFailure} from 'esbuild'
6
+ import {_loadConfig, _loadPkgWithReporting} from './_core'
7
+ import {_fileExists, _printPackageTree} from './_printPackageTree'
8
+ import {_resolveBuildContext} from './_resolveBuildContext'
9
+
10
+ /** @public */
11
+ export async function check(options: {
12
+ cwd: string
13
+ strict?: boolean
14
+ tsconfig?: string
15
+ }): Promise<void> {
16
+ const {cwd, strict, tsconfig = 'tsconfig.json'} = options
17
+
18
+ const pkg = await _loadPkgWithReporting({cwd})
19
+ const config = await _loadConfig({cwd})
20
+ const ctx = await _resolveBuildContext({config, cwd, pkg, tsconfig})
21
+
22
+ _printPackageTree(ctx)
23
+
24
+ if (strict) {
25
+ const missingFiles: string[] = []
26
+
27
+ // Check if there are missing files
28
+ for (const [, exp] of Object.entries(ctx.exports || {})) {
29
+ if (exp.source && !_fileExists(path.resolve(cwd, exp.source))) {
30
+ missingFiles.push(exp.source)
31
+ }
32
+
33
+ if (exp.require && !_fileExists(path.resolve(cwd, exp.require))) {
34
+ missingFiles.push(exp.require)
35
+ }
36
+
37
+ if (exp.import && !_fileExists(path.resolve(cwd, exp.import))) {
38
+ missingFiles.push(exp.import)
39
+ }
40
+
41
+ if (exp.types && !_fileExists(path.resolve(cwd, exp.types))) {
42
+ missingFiles.push(exp.types)
43
+ }
44
+ }
45
+
46
+ if (missingFiles.length) {
47
+ throw new Error('missing files')
48
+ }
49
+
50
+ // Check if the files are resolved
51
+ const _paths: {require: string[]; import: string[]} = {
52
+ require: [],
53
+ import: [],
54
+ }
55
+
56
+ for (const [exportPath, exp] of Object.entries(ctx.exports || {})) {
57
+ if (!exp._exported) continue
58
+ if (exp.require) _paths.require.push(exportPath)
59
+ if (exp.import) _paths.import.push(exportPath)
60
+ }
61
+
62
+ const external = Object.keys(pkg.dependencies || {}).concat(
63
+ Object.keys(pkg.devDependencies || {})
64
+ )
65
+
66
+ if (_paths.import.length) {
67
+ const code = _paths.import.map((id) => `import('${id}')`).join('\n')
68
+
69
+ try {
70
+ const esbuildResult = await esbuild.build({
71
+ external,
72
+ stdin: {
73
+ contents: code,
74
+ loader: 'js',
75
+ resolveDir: cwd,
76
+ },
77
+ format: 'esm',
78
+ bundle: true,
79
+ logLevel: 'silent',
80
+ // otherwise output maps to stdout as we're using stdin
81
+ outfile: '/dev/null',
82
+ platform: 'node',
83
+ })
84
+
85
+ if (esbuildResult.warnings.length > 0) {
86
+ console.warn(...esbuildResult.warnings)
87
+ }
88
+ } catch (err) {
89
+ const {errors} = err as BuildFailure
90
+
91
+ for (const _err of errors) {
92
+ if (_err.location) {
93
+ console.log(chalk.red('error'), _err.detail || _err.text)
94
+ console.log(`${_err.location.line} | ${_err.location.lineText}`)
95
+ console.log(
96
+ 'in',
97
+ `./${_err.location.file}:${_err.location.line}:${_err.location.column}`
98
+ )
99
+ } else {
100
+ console.log(_err.detail || _err.text)
101
+ }
102
+
103
+ console.log()
104
+ }
105
+
106
+ process.exit(1)
107
+ }
108
+ }
109
+
110
+ if (_paths.require.length) {
111
+ const code = _paths.require.map((id) => `require('${id}')`).join('\n')
112
+
113
+ const esbuildResult = await esbuild.build({
114
+ external,
115
+ stdin: {
116
+ contents: code,
117
+ loader: 'js',
118
+ resolveDir: cwd,
119
+ },
120
+ format: 'cjs',
121
+ bundle: true,
122
+ // otherwise output maps to stdout as we're using stdin
123
+ outfile: '/dev/null',
124
+ platform: 'node',
125
+ })
126
+
127
+ if (esbuildResult.errors.length > 0) {
128
+ throw new Error(esbuildResult.errors.join(', '))
129
+ }
130
+
131
+ if (esbuildResult.warnings.length > 0) {
132
+ console.warn(...esbuildResult.warnings)
133
+ }
134
+ }
135
+ }
136
+ }
@@ -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
+ }