@sanity/pkg-utils 2.2.16 → 2.3.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 (84) hide show
  1. package/README.md +16 -7
  2. package/dist/_chunks/{build-ba8f4eab.cjs → build-0aed2000.cjs} +11 -8
  3. package/dist/_chunks/build-0aed2000.cjs.map +1 -0
  4. package/dist/_chunks/{buildAction-f35f5fe0.cjs → buildAction-196fba7b.cjs} +2 -2
  5. package/dist/_chunks/{buildAction-f35f5fe0.cjs.map → buildAction-196fba7b.cjs.map} +1 -1
  6. package/dist/_chunks/{check-c18ac13f.cjs → check-2deafe2a.cjs} +25 -31
  7. package/dist/_chunks/check-2deafe2a.cjs.map +1 -0
  8. package/dist/_chunks/{checkAction-be47c642.cjs → checkAction-7ac5a05e.cjs} +2 -2
  9. package/dist/_chunks/{checkAction-be47c642.cjs.map → checkAction-7ac5a05e.cjs.map} +1 -1
  10. package/dist/_chunks/{index-b295a336.cjs → index-984e18d1.cjs} +6 -3
  11. package/dist/_chunks/index-984e18d1.cjs.map +1 -0
  12. package/dist/_chunks/init-ab36343b.cjs +469 -0
  13. package/dist/_chunks/init-ab36343b.cjs.map +1 -0
  14. package/dist/_chunks/initAction-d18cf4f2.cjs +16 -0
  15. package/dist/_chunks/initAction-d18cf4f2.cjs.map +1 -0
  16. package/dist/_chunks/logger-c593f06b.cjs +97 -0
  17. package/dist/_chunks/logger-c593f06b.cjs.map +1 -0
  18. package/dist/_chunks/{resolveBuildContext-2239fc5e.cjs → resolveBuildContext-5ef90110.cjs} +13 -92
  19. package/dist/_chunks/resolveBuildContext-5ef90110.cjs.map +1 -0
  20. package/dist/_chunks/{watch-5bfb8b28.cjs → watch-63a93eb0.cjs} +10 -7
  21. package/dist/_chunks/watch-63a93eb0.cjs.map +1 -0
  22. package/dist/_chunks/{watchAction-788563ea.cjs → watchAction-95c0b481.cjs} +2 -2
  23. package/dist/_chunks/{watchAction-788563ea.cjs.map → watchAction-95c0b481.cjs.map} +1 -1
  24. package/dist/cli.cjs +17 -7
  25. package/dist/cli.cjs.map +1 -1
  26. package/dist/index.cjs +12 -7
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.ts +87 -0
  29. package/dist/index.js +465 -32
  30. package/dist/index.js.map +1 -1
  31. package/package.json +93 -79
  32. package/src/cli/index.ts +12 -3
  33. package/src/cli/initAction.ts +13 -0
  34. package/src/node/build.ts +1 -1
  35. package/src/node/check.ts +4 -2
  36. package/src/node/core/config/findConfigFile.ts +6 -14
  37. package/src/node/core/config/index.ts +1 -1
  38. package/src/node/core/config/loadConfig.ts +2 -1
  39. package/src/node/core/contexts/buildContext.ts +2 -1
  40. package/src/node/core/findCommonPath.test.ts +1 -0
  41. package/src/node/core/index.ts +3 -2
  42. package/src/node/core/pkg/loadPkg.ts +1 -0
  43. package/src/node/core/pkg/loadPkgWithReporting.ts +1 -0
  44. package/src/node/core/pkg/types.ts +13 -0
  45. package/src/node/core/pkg/validatePkg.ts +1 -0
  46. package/src/node/core/template/createFromTemplate.ts +85 -0
  47. package/src/node/core/template/define.ts +6 -0
  48. package/src/node/core/template/index.ts +3 -0
  49. package/src/node/core/template/types.ts +45 -0
  50. package/src/node/fileExists.ts +12 -0
  51. package/src/node/globFiles.ts +1 -1
  52. package/src/node/index.ts +1 -0
  53. package/src/node/init.ts +55 -0
  54. package/src/node/isEmptyDirectory.ts +5 -0
  55. package/src/node/printExtractMessages.ts +2 -1
  56. package/src/node/printPackageTree.ts +4 -13
  57. package/src/node/resolveBuildContext.ts +5 -4
  58. package/src/node/resolveBuildTasks.ts +2 -1
  59. package/src/node/resolveWatchTasks.ts +3 -2
  60. package/src/node/tasks/dts/buildTypes.ts +1 -0
  61. package/src/node/tasks/dts/createApiExtractorConfig.ts +1 -1
  62. package/src/node/tasks/dts/createTSDocConfig.ts +1 -1
  63. package/src/node/tasks/dts/doExtract.ts +2 -1
  64. package/src/node/tasks/dts/dtsTask.ts +1 -0
  65. package/src/node/tasks/dts/dtsWatchTask.ts +1 -0
  66. package/src/node/tasks/dts/extractTypes.ts +4 -3
  67. package/src/node/tasks/dts/getExtractMessagesConfig.ts +2 -1
  68. package/src/node/tasks/dts/printDiagnostic.ts +2 -1
  69. package/src/node/tasks/index.ts +1 -1
  70. package/src/node/tasks/node/reexportCjsTask.ts +3 -2
  71. package/src/node/tasks/rollup/resolveRollupConfig.ts +2 -1
  72. package/src/node/tasks/rollup/rollupTask.ts +2 -1
  73. package/src/node/tasks/rollup/rollupWatchTask.ts +2 -1
  74. package/src/node/tasks/types.ts +2 -1
  75. package/src/node/templates/default/index.ts +1 -0
  76. package/src/node/templates/default/template.ts +553 -0
  77. package/src/node/templates/index.ts +1 -0
  78. package/src/node/watch.ts +2 -1
  79. package/src/node/watchConfigFiles.ts +1 -0
  80. package/dist/_chunks/build-ba8f4eab.cjs.map +0 -1
  81. package/dist/_chunks/check-c18ac13f.cjs.map +0 -1
  82. package/dist/_chunks/index-b295a336.cjs.map +0 -1
  83. package/dist/_chunks/resolveBuildContext-2239fc5e.cjs.map +0 -1
  84. package/dist/_chunks/watch-5bfb8b28.cjs.map +0 -1
@@ -0,0 +1,55 @@
1
+ import {lstat} from 'fs/promises'
2
+ import {mkdirp} from 'mkdirp'
3
+ import {resolve} from 'path'
4
+
5
+ import {createFromTemplate} from './core/template'
6
+ import {fileExists} from './fileExists'
7
+ import {isEmptyDirectory} from './isEmptyDirectory'
8
+ import {createLogger} from './logger'
9
+ import {defaultTemplate} from './templates'
10
+
11
+ /** @public */
12
+ export async function init(options: {cwd: string; path: string}): Promise<void> {
13
+ if (!options.cwd) {
14
+ throw new Error('Missing required option: cwd')
15
+ }
16
+
17
+ if (!options.path) {
18
+ throw new Error('Missing required option: path')
19
+ }
20
+
21
+ const logger = createLogger()
22
+
23
+ const packagePath = resolve(options.cwd, options.path)
24
+
25
+ await ensurePackagePath(packagePath)
26
+
27
+ await createFromTemplate({
28
+ cwd: options.cwd,
29
+ logger,
30
+ template: defaultTemplate,
31
+ packagePath,
32
+ })
33
+ }
34
+
35
+ async function ensurePackagePath(packagePath: string): Promise<void> {
36
+ const exists = fileExists(packagePath)
37
+
38
+ if (!exists) {
39
+ await mkdirp(packagePath)
40
+
41
+ return
42
+ }
43
+
44
+ const dir = (await lstat(packagePath)).isDirectory()
45
+
46
+ if (!dir) {
47
+ throw new Error('the package path is a file, not a directory')
48
+ }
49
+
50
+ const empty = await isEmptyDirectory(packagePath)
51
+
52
+ if (!empty) {
53
+ throw new Error('the package directory is not empty')
54
+ }
55
+ }
@@ -0,0 +1,5 @@
1
+ import {readdir} from 'fs/promises'
2
+
3
+ export async function isEmptyDirectory(dirPath: string): Promise<boolean> {
4
+ return (await readdir(dirPath)).length === 0
5
+ }
@@ -1,6 +1,7 @@
1
- import path from 'path'
2
1
  import {ExtractorMessage} from '@microsoft/api-extractor'
3
2
  import chalk from 'chalk'
3
+ import path from 'path'
4
+
4
5
  import {BuildContext} from './core'
5
6
 
6
7
  export function printExtractMessages(ctx: BuildContext, messages: ExtractorMessage[]): void {
@@ -1,19 +1,10 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
1
  import chalk from 'chalk'
2
+ import path from 'path'
4
3
  import treeify from 'treeify'
5
- import {PkgExport, BuildContext} from './core'
6
- import {getFilesize} from './getFilesize'
7
-
8
- export function fileExists(file: string): boolean {
9
- try {
10
- fs.accessSync(file)
11
4
 
12
- return true
13
- } catch (_) {
14
- return false
15
- }
16
- }
5
+ import {BuildContext, PkgExport} from './core'
6
+ import {fileExists} from './fileExists'
7
+ import {getFilesize} from './getFilesize'
17
8
 
18
9
  function getFileInfo(cwd: string, filePath: string) {
19
10
  const p = path.resolve(cwd, filePath)
@@ -1,15 +1,16 @@
1
1
  import path from 'path'
2
+
2
3
  import {
3
- PkgConfigOptions,
4
- PkgExports,
5
- PkgRuntime,
6
4
  BuildContext,
7
5
  DEFAULT_BROWSERSLIST_QUERY,
8
6
  loadTSConfig,
9
7
  PackageJSON,
10
8
  parseExports,
11
- resolveConfigProperty,
9
+ PkgConfigOptions,
10
+ PkgExports,
12
11
  PkgExtMap,
12
+ PkgRuntime,
13
+ resolveConfigProperty,
13
14
  } from './core'
14
15
  import {findCommonDirPath, pathContains} from './core/findCommonPath'
15
16
  import {Logger} from './logger'
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs'
2
2
  import path from 'path'
3
- import {PkgExport, PkgFormat, PkgRuntime, BuildContext} from './core'
3
+
4
+ import {BuildContext, PkgExport, PkgFormat, PkgRuntime} from './core'
4
5
  import {BuildTask, DtsTask, NodeReExportFromCJSTask, RollupTask, RollupTaskEntry} from './tasks'
5
6
 
6
7
  /** @internal */
@@ -1,7 +1,8 @@
1
1
  import fs from 'fs'
2
2
  import path from 'path'
3
- import {PkgExport, PkgFormat, PkgRuntime, BuildContext} from './core'
4
- import {WatchTask, DtsWatchTask, RollupWatchTask, RollupTaskEntry} from './tasks'
3
+
4
+ import {BuildContext, PkgExport, PkgFormat, PkgRuntime} from './core'
5
+ import {DtsWatchTask, RollupTaskEntry, RollupWatchTask, WatchTask} from './tasks'
5
6
 
6
7
  /** @internal */
7
8
  export function resolveWatchTasks(ctx: BuildContext): WatchTask[] {
@@ -1,4 +1,5 @@
1
1
  import ts from 'typescript'
2
+
2
3
  import {Logger} from '../../logger'
3
4
  import {printDiagnostic} from './printDiagnostic'
4
5
 
@@ -1,5 +1,5 @@
1
- import path from 'path'
2
1
  import {IConfigFile, IExtractorMessagesConfig} from '@microsoft/api-extractor'
2
+ import path from 'path'
3
3
 
4
4
  export function createApiExtractorConfig(options: {
5
5
  distPath: string
@@ -1,5 +1,5 @@
1
- import {readFile} from 'fs/promises'
2
1
  import {TSDocConfigFile} from '@microsoft/tsdoc-config'
2
+ import {readFile} from 'fs/promises'
3
3
  import {parse} from 'jsonc-parser'
4
4
 
5
5
  /** @public */
@@ -1,6 +1,7 @@
1
- import path from 'path'
2
1
  import {ExtractorMessage} from '@microsoft/api-extractor'
2
+ import path from 'path'
3
3
  import {rimraf} from 'rimraf'
4
+
4
5
  import {BuildContext} from '../../core'
5
6
  import {buildTypes} from './buildTypes'
6
7
  import {DtsError} from './DtsError'
@@ -1,5 +1,6 @@
1
1
  import chalk from 'chalk'
2
2
  import {Observable} from 'rxjs'
3
+
3
4
  import {printExtractMessages} from '../../printExtractMessages'
4
5
  import {TaskHandler} from '../types'
5
6
  import {doExtract} from './doExtract'
@@ -1,5 +1,6 @@
1
1
  import chalk from 'chalk'
2
2
  import {Observable} from 'rxjs'
3
+
3
4
  import {printExtractMessages} from '../../printExtractMessages'
4
5
  import {TaskHandler} from '../types'
5
6
  import {doExtract} from './doExtract'
@@ -1,14 +1,15 @@
1
- import fs from 'fs/promises'
2
- import path from 'path'
3
1
  import {
4
2
  Extractor,
5
3
  ExtractorConfig,
6
4
  ExtractorMessage,
7
5
  ExtractorResult,
8
6
  } from '@microsoft/api-extractor'
7
+ import fs from 'fs/promises'
9
8
  import {mkdirp} from 'mkdirp'
9
+ import path from 'path'
10
10
  import prettier from 'prettier'
11
- import {PkgConfigOptions, BuildFile} from '../../core'
11
+
12
+ import {BuildFile, PkgConfigOptions} from '../../core'
12
13
  import {createApiExtractorConfig} from './createApiExtractorConfig'
13
14
  import {createTSDocConfig} from './createTSDocConfig'
14
15
  import {extractModuleBlocksFromTypes} from './extractModuleBlocks'
@@ -1,5 +1,6 @@
1
1
  import {ExtractorLogLevel, IExtractorMessagesConfig} from '@microsoft/api-extractor'
2
- import {PkgRuleLevel, PkgConfigOptions} from '../../core'
2
+
3
+ import {PkgConfigOptions, PkgRuleLevel} from '../../core'
3
4
 
4
5
  const LOG_LEVELS: Record<PkgRuleLevel, ExtractorLogLevel> = {
5
6
  error: 'error' as ExtractorLogLevel.Error,
@@ -1,6 +1,7 @@
1
- import path from 'path'
2
1
  import chalk from 'chalk'
2
+ import path from 'path'
3
3
  import ts from 'typescript'
4
+
4
5
  import {Logger} from '../../logger'
5
6
 
6
7
  export function printDiagnostic(options: {
@@ -3,9 +3,9 @@ import {nodeReexportCjsTask} from './node/reexportCjsTask'
3
3
  import {rollupTask, rollupWatchTask} from './rollup'
4
4
  import {BuildTaskHandlers, WatchTaskHandlers} from './types'
5
5
 
6
- export * from './types'
7
6
  export * from './dts'
8
7
  export * from './rollup'
8
+ export * from './types'
9
9
 
10
10
  /** @internal */
11
11
  export const buildTaskHandlers: BuildTaskHandlers = {
@@ -1,8 +1,9 @@
1
- import {writeFile} from 'fs/promises'
2
- import path from 'path'
3
1
  import chalk from 'chalk'
2
+ import {writeFile} from 'fs/promises'
4
3
  import {mkdirp} from 'mkdirp'
4
+ import path from 'path'
5
5
  import {from} from 'rxjs'
6
+
6
7
  import {BuildContext} from '../../core'
7
8
  import {NodeReExportFromCJSTask, TaskHandler} from '../types'
8
9
 
@@ -1,4 +1,3 @@
1
- import path from 'path'
2
1
  import alias from '@rollup/plugin-alias'
3
2
  import {getBabelOutputPlugin} from '@rollup/plugin-babel'
4
3
  import commonjs from '@rollup/plugin-commonjs'
@@ -6,8 +5,10 @@ import json from '@rollup/plugin-json'
6
5
  import {nodeResolve} from '@rollup/plugin-node-resolve'
7
6
  import replace from '@rollup/plugin-replace'
8
7
  import terser from '@rollup/plugin-terser'
8
+ import path from 'path'
9
9
  import {InputOptions, OutputOptions, Plugin} from 'rollup'
10
10
  import esbuild from 'rollup-plugin-esbuild'
11
+
11
12
  import {BuildContext, DEFAULT_BROWSERSLIST_QUERY, resolveConfigProperty} from '../../core'
12
13
  import {RollupTask, RollupWatchTask} from '../types'
13
14
 
@@ -1,7 +1,8 @@
1
- import path from 'path'
2
1
  import chalk from 'chalk'
2
+ import path from 'path'
3
3
  import {rollup} from 'rollup'
4
4
  import {Observable} from 'rxjs'
5
+
5
6
  import {BuildContext} from '../../core'
6
7
  import {RollupTask, TaskHandler} from '../types'
7
8
  import {createConsoleSpy} from './consoleSpy'
@@ -1,7 +1,8 @@
1
- import path from 'path'
2
1
  import chalk from 'chalk'
2
+ import path from 'path'
3
3
  import {RollupWatcherEvent, RollupWatchOptions, watch as rollupWatch} from 'rollup'
4
4
  import {Observable} from 'rxjs'
5
+
5
6
  import {RollupWatchTask, TaskHandler} from '../types'
6
7
  import {resolveRollupConfig} from './resolveRollupConfig'
7
8
 
@@ -1,6 +1,7 @@
1
1
  import {RollupWatcherEvent} from 'rollup'
2
2
  import {Observable} from 'rxjs'
3
- import {PkgRuntime, BuildContext} from '../core'
3
+
4
+ import {BuildContext, PkgRuntime} from '../core'
4
5
  import {DtsResult, DtsTask, DtsWatchTask} from './dts'
5
6
 
6
7
  /** @internal */
@@ -0,0 +1 @@
1
+ export * from './template'