@sanity/plugin-kit 2.1.19 → 3.0.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 (228) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +53 -37
  3. package/assets/inject/semver-workflow/lint-staged.template.js +1 -1
  4. package/assets/inject/ui-workshop/src/CustomField.tsx +15 -0
  5. package/assets/inject/ui-workshop/src/__workshop__/index.tsx +14 -0
  6. package/assets/inject/ui-workshop/src/__workshop__/props.tsx +20 -0
  7. package/assets/inject/ui-workshop/workshop.config.ts +5 -0
  8. package/bin/plugin-kit.js +2 -0
  9. package/dist/_chunks/cli-9f3b6fb3.js +162 -0
  10. package/dist/_chunks/cli-9f3b6fb3.js.map +1 -0
  11. package/dist/_chunks/init-3ad3f71f.js +104 -0
  12. package/dist/_chunks/init-3ad3f71f.js.map +1 -0
  13. package/dist/_chunks/init-ed4b5e78.js +952 -0
  14. package/dist/_chunks/init-ed4b5e78.js.map +1 -0
  15. package/dist/_chunks/inject-c90dc62d.js +49 -0
  16. package/dist/_chunks/inject-c90dc62d.js.map +1 -0
  17. package/dist/_chunks/link-watch-7f10aeab.js +94 -0
  18. package/dist/_chunks/link-watch-7f10aeab.js.map +1 -0
  19. package/dist/_chunks/package-ad838e5c.js +2246 -0
  20. package/dist/_chunks/package-ad838e5c.js.map +1 -0
  21. package/dist/_chunks/package-fa6580ba.js +152 -0
  22. package/dist/_chunks/package-fa6580ba.js.map +1 -0
  23. package/dist/_chunks/ts-89567e57.js +167 -0
  24. package/dist/_chunks/ts-89567e57.js.map +1 -0
  25. package/dist/_chunks/verify-package-27b93f01.js +96 -0
  26. package/dist/_chunks/verify-package-27b93f01.js.map +1 -0
  27. package/dist/_chunks/verify-studio-3633e659.js +75 -0
  28. package/dist/_chunks/verify-studio-3633e659.js.map +1 -0
  29. package/dist/_chunks/version-d6a290e2.js +58 -0
  30. package/dist/_chunks/version-d6a290e2.js.map +1 -0
  31. package/dist/cli.js +9 -0
  32. package/dist/cli.js.map +1 -0
  33. package/dist/index.d.ts +1 -0
  34. package/dist/index.js +2 -0
  35. package/dist/index.js.map +1 -0
  36. package/package.json +30 -17
  37. package/src/actions/init.ts +2 -0
  38. package/src/actions/inject.ts +66 -87
  39. package/src/actions/link-watch.ts +8 -5
  40. package/src/actions/verify/types.ts +0 -23
  41. package/src/actions/verify/validations.ts +65 -39
  42. package/src/actions/verify/verify-common.ts +4 -4
  43. package/src/actions/verify-package.ts +13 -7
  44. package/src/actions/verify-studio.ts +7 -4
  45. package/src/cli.ts +1 -3
  46. package/src/cmds/index.ts +18 -6
  47. package/src/cmds/init.ts +1 -1
  48. package/src/cmds/inject.ts +6 -3
  49. package/src/cmds/link-watch.ts +0 -1
  50. package/src/configs/default-source.ts +10 -14
  51. package/src/configs/eslint.ts +51 -0
  52. package/src/configs/forced-package-versions.ts +8 -7
  53. package/src/configs/git.ts +68 -0
  54. package/src/configs/pkg-config.ts +31 -0
  55. package/src/configs/prettier.ts +11 -0
  56. package/src/configs/tsconfig.ts +79 -0
  57. package/src/constants.ts +2 -0
  58. package/src/index.ts +1 -4
  59. package/src/npm/package.ts +62 -26
  60. package/src/presets/presets.ts +3 -1
  61. package/src/presets/renovatebot.ts +10 -1
  62. package/src/presets/semver-workflow.ts +26 -14
  63. package/src/presets/ui-workshop.ts +97 -0
  64. package/src/presets/ui.ts +68 -0
  65. package/src/sanity/manifest.ts +2 -2
  66. package/src/util/files.ts +8 -8
  67. package/src/util/readme.ts +17 -15
  68. package/src/util/ts.ts +13 -0
  69. package/assets/inject/gitignore +0 -57
  70. package/assets/inject/package.config.template +0 -16
  71. package/assets/inject/template-tsconfig.json +0 -24
  72. package/lib/package.json +0 -128
  73. package/lib/src/actions/init.d.ts +0 -73
  74. package/lib/src/actions/init.js +0 -79
  75. package/lib/src/actions/init.js.map +0 -1
  76. package/lib/src/actions/inject.d.ts +0 -31
  77. package/lib/src/actions/inject.js +0 -350
  78. package/lib/src/actions/inject.js.map +0 -1
  79. package/lib/src/actions/link-watch.d.ts +0 -3
  80. package/lib/src/actions/link-watch.js +0 -87
  81. package/lib/src/actions/link-watch.js.map +0 -1
  82. package/lib/src/actions/verify/types.d.ts +0 -75
  83. package/lib/src/actions/verify/types.js +0 -3
  84. package/lib/src/actions/verify/types.js.map +0 -1
  85. package/lib/src/actions/verify/validations.d.ts +0 -27
  86. package/lib/src/actions/verify/validations.js +0 -410
  87. package/lib/src/actions/verify/validations.js.map +0 -1
  88. package/lib/src/actions/verify/verify-common.d.ts +0 -44
  89. package/lib/src/actions/verify/verify-common.js +0 -89
  90. package/lib/src/actions/verify/verify-common.js.map +0 -1
  91. package/lib/src/actions/verify-package.d.ts +0 -5
  92. package/lib/src/actions/verify-package.js +0 -73
  93. package/lib/src/actions/verify-package.js.map +0 -1
  94. package/lib/src/actions/verify-studio.d.ts +0 -5
  95. package/lib/src/actions/verify-studio.js +0 -55
  96. package/lib/src/actions/verify-studio.js.map +0 -1
  97. package/lib/src/cli.d.ts +0 -2
  98. package/lib/src/cli.js +0 -87
  99. package/lib/src/cli.js.map +0 -1
  100. package/lib/src/cmds/index.d.ts +0 -9
  101. package/lib/src/cmds/index.js +0 -11
  102. package/lib/src/cmds/index.js.map +0 -1
  103. package/lib/src/cmds/init.d.ts +0 -4
  104. package/lib/src/cmds/init.js +0 -95
  105. package/lib/src/cmds/init.js.map +0 -1
  106. package/lib/src/cmds/inject.d.ts +0 -4
  107. package/lib/src/cmds/inject.js +0 -76
  108. package/lib/src/cmds/inject.js.map +0 -1
  109. package/lib/src/cmds/link-watch.d.ts +0 -4
  110. package/lib/src/cmds/link-watch.js +0 -49
  111. package/lib/src/cmds/link-watch.js.map +0 -1
  112. package/lib/src/cmds/verify-package.d.ts +0 -4
  113. package/lib/src/cmds/verify-package.js +0 -38
  114. package/lib/src/cmds/verify-package.js.map +0 -1
  115. package/lib/src/cmds/verify-studio.d.ts +0 -4
  116. package/lib/src/cmds/verify-studio.js +0 -38
  117. package/lib/src/cmds/verify-studio.js.map +0 -1
  118. package/lib/src/cmds/verify.d.ts +0 -0
  119. package/lib/src/cmds/verify.js +0 -42
  120. package/lib/src/cmds/verify.js.map +0 -1
  121. package/lib/src/cmds/version.d.ts +0 -4
  122. package/lib/src/cmds/version.js +0 -55
  123. package/lib/src/cmds/version.js.map +0 -1
  124. package/lib/src/configs/banned-packages.d.ts +0 -2
  125. package/lib/src/configs/banned-packages.js +0 -30
  126. package/lib/src/configs/banned-packages.js.map +0 -1
  127. package/lib/src/configs/buildExtensions.d.ts +0 -1
  128. package/lib/src/configs/buildExtensions.js +0 -5
  129. package/lib/src/configs/buildExtensions.js.map +0 -1
  130. package/lib/src/configs/default-source.d.ts +0 -3
  131. package/lib/src/configs/default-source.js +0 -70
  132. package/lib/src/configs/default-source.js.map +0 -1
  133. package/lib/src/configs/forced-package-versions.d.ts +0 -16
  134. package/lib/src/configs/forced-package-versions.js +0 -14
  135. package/lib/src/configs/forced-package-versions.js.map +0 -1
  136. package/lib/src/configs/uselessFiles.d.ts +0 -1
  137. package/lib/src/configs/uselessFiles.js +0 -33
  138. package/lib/src/configs/uselessFiles.js.map +0 -1
  139. package/lib/src/constants.d.ts +0 -11
  140. package/lib/src/constants.js +0 -15
  141. package/lib/src/constants.js.map +0 -1
  142. package/lib/src/dependencies/find.d.ts +0 -0
  143. package/lib/src/dependencies/find.js +0 -195
  144. package/lib/src/dependencies/find.js.map +0 -1
  145. package/lib/src/dependencies/import-linter.d.ts +0 -3
  146. package/lib/src/dependencies/import-linter.js +0 -112
  147. package/lib/src/dependencies/import-linter.js.map +0 -1
  148. package/lib/src/index.d.ts +0 -2
  149. package/lib/src/index.js +0 -6
  150. package/lib/src/index.js.map +0 -1
  151. package/lib/src/npm/manager.d.ts +0 -7
  152. package/lib/src/npm/manager.js +0 -62
  153. package/lib/src/npm/manager.js.map +0 -1
  154. package/lib/src/npm/package.d.ts +0 -11
  155. package/lib/src/npm/package.js +0 -302
  156. package/lib/src/npm/package.js.map +0 -1
  157. package/lib/src/npm/publish.d.ts +0 -1
  158. package/lib/src/npm/publish.js +0 -14
  159. package/lib/src/npm/publish.js.map +0 -1
  160. package/lib/src/npm/resolveLatestVersions.d.ts +0 -3
  161. package/lib/src/npm/resolveLatestVersions.js +0 -37
  162. package/lib/src/npm/resolveLatestVersions.js.map +0 -1
  163. package/lib/src/presets/presets.d.ts +0 -8
  164. package/lib/src/presets/presets.js +0 -52
  165. package/lib/src/presets/presets.js.map +0 -1
  166. package/lib/src/presets/renovatebot.d.ts +0 -2
  167. package/lib/src/presets/renovatebot.js +0 -24
  168. package/lib/src/presets/renovatebot.js.map +0 -1
  169. package/lib/src/presets/semver-workflow.d.ts +0 -8
  170. package/lib/src/presets/semver-workflow.js +0 -166
  171. package/lib/src/presets/semver-workflow.js.map +0 -1
  172. package/lib/src/sanity/manifest.d.ts +0 -48
  173. package/lib/src/sanity/manifest.js +0 -263
  174. package/lib/src/sanity/manifest.js.map +0 -1
  175. package/lib/src/sharedFlags.d.ts +0 -15
  176. package/lib/src/sharedFlags.js +0 -17
  177. package/lib/src/sharedFlags.js.map +0 -1
  178. package/lib/src/util/command-parser.d.ts +0 -9
  179. package/lib/src/util/command-parser.js +0 -46
  180. package/lib/src/util/command-parser.js.map +0 -1
  181. package/lib/src/util/errorToUndefined.d.ts +0 -1
  182. package/lib/src/util/errorToUndefined.js +0 -11
  183. package/lib/src/util/errorToUndefined.js.map +0 -1
  184. package/lib/src/util/files.d.ts +0 -36
  185. package/lib/src/util/files.js +0 -264
  186. package/lib/src/util/files.js.map +0 -1
  187. package/lib/src/util/log.d.ts +0 -14
  188. package/lib/src/util/log.js +0 -36
  189. package/lib/src/util/log.js.map +0 -1
  190. package/lib/src/util/prompt.d.ts +0 -14
  191. package/lib/src/util/prompt.js +0 -75
  192. package/lib/src/util/prompt.js.map +0 -1
  193. package/lib/src/util/readme.d.ts +0 -8
  194. package/lib/src/util/readme.js +0 -92
  195. package/lib/src/util/readme.js.map +0 -1
  196. package/lib/src/util/request.d.ts +0 -1
  197. package/lib/src/util/request.js +0 -19
  198. package/lib/src/util/request.js.map +0 -1
  199. package/lib/src/util/user.d.ts +0 -13
  200. package/lib/src/util/user.js +0 -111
  201. package/lib/src/util/user.js.map +0 -1
  202. package/lib/test/cli.test.d.ts +0 -1
  203. package/lib/test/cli.test.js +0 -64
  204. package/lib/test/cli.test.js.map +0 -1
  205. package/lib/test/fixture-utils.d.ts +0 -25
  206. package/lib/test/fixture-utils.js +0 -67
  207. package/lib/test/fixture-utils.js.map +0 -1
  208. package/lib/test/init-verify-build.test.d.ts +0 -1
  209. package/lib/test/init-verify-build.test.js +0 -68
  210. package/lib/test/init-verify-build.test.js.map +0 -1
  211. package/lib/test/init.test.d.ts +0 -1
  212. package/lib/test/init.test.js +0 -169
  213. package/lib/test/init.test.js.map +0 -1
  214. package/lib/test/inject.test.d.ts +0 -1
  215. package/lib/test/inject.test.js +0 -75
  216. package/lib/test/inject.test.js.map +0 -1
  217. package/lib/test/run-test-command.d.ts +0 -1
  218. package/lib/test/run-test-command.js +0 -6
  219. package/lib/test/run-test-command.js.map +0 -1
  220. package/lib/test/semver-workflow.test.d.ts +0 -1
  221. package/lib/test/semver-workflow.test.js +0 -93
  222. package/lib/test/semver-workflow.test.js.map +0 -1
  223. package/lib/test/verify-package.test.d.ts +0 -1
  224. package/lib/test/verify-package.test.js +0 -87
  225. package/lib/test/verify-package.test.js.map +0 -1
  226. package/lib/test/version.test.d.ts +0 -1
  227. package/lib/test/version.test.js +0 -48
  228. package/lib/test/version.test.js.map +0 -1
@@ -18,8 +18,12 @@ import {
18
18
  } from '../util/files'
19
19
  import {InitFlags} from './init'
20
20
  import {PackageJson} from './verify/types'
21
- import outdent from 'outdent'
22
21
  import {injectPresets} from '../presets/presets'
22
+ import {tsconfigTemplateDist, tsconfigTemplate, tsconfigTemplateSettings} from '../configs/tsconfig'
23
+ import {pkgConfigTemplate} from '../configs/pkg-config'
24
+ import {gitignoreTemplate} from '../configs/git'
25
+ import {eslintignoreTemplate, eslintrcTemplate} from '../configs/eslint'
26
+ import {prettierignoreTemplate} from '../configs/prettier'
23
27
 
24
28
  const bannedFields = ['login', 'description', 'projecturl', 'email']
25
29
  const preferredLicenses = ['MIT', 'ISC', 'BSD-3-Clause']
@@ -31,7 +35,20 @@ const otherLicenses = Object.keys(licenses.list).filter((id) => {
31
35
  )
32
36
  })
33
37
 
34
- export type FromTo = {from: string | string[]; to: string | string[]}
38
+ export interface InjectCopyFromTo {
39
+ type: 'copy'
40
+ from: string | string[]
41
+ to: string | string[]
42
+ }
43
+
44
+ export interface InjectTemplate {
45
+ type: 'template'
46
+ to: string | string[]
47
+ value: string
48
+ force?: boolean
49
+ }
50
+
51
+ export type Injectable = InjectCopyFromTo | InjectTemplate
35
52
 
36
53
  export interface InjectOptions {
37
54
  basePath: string
@@ -41,6 +58,7 @@ export interface InjectOptions {
41
58
  devDependencies?: Record<string, string>
42
59
  peerDependencies?: Record<string, string>
43
60
  validate?: boolean
61
+ outDir: string
44
62
  }
45
63
 
46
64
  export interface PackageData {
@@ -111,11 +129,6 @@ async function injectBase(options: InjectOptions) {
111
129
  didWrite = await writeStaticAssets(options)
112
130
  info(didWrite.length > 0, 'Wrote static asset files: %s', didWrite.join(', '))
113
131
 
114
- didWrite = await writeEslintrc(options)
115
- info(didWrite, 'Wrote .eslintrc.js')
116
- didWrite = await writeEslintIgnore(options)
117
- info(didWrite, 'Wrote .eslintignore')
118
-
119
132
  didWrite = await addBuildScripts(newPkg, options)
120
133
  info(didWrite, 'Added build scripts to package.json')
121
134
 
@@ -140,61 +153,6 @@ async function writeReadme(data: PackageData, options: InjectOptions) {
140
153
  return true
141
154
  }
142
155
 
143
- async function writeEslintrc(options: InjectOptions) {
144
- if (!options.flags.eslint) {
145
- return false
146
- }
147
- const {basePath} = options
148
-
149
- const eslintrc = path.join(basePath, '.eslintrc')
150
-
151
- const config = {
152
- root: true,
153
- env: {
154
- node: true,
155
- browser: true,
156
- },
157
- extends: [
158
- 'sanity',
159
- options.flags.typescript && 'sanity/typescript',
160
- 'sanity/react',
161
- 'plugin:react-hooks/recommended',
162
- options.flags.prettier && 'plugin:prettier/recommended',
163
- ].filter(Boolean),
164
- }
165
-
166
- const content = JSON.stringify(config, null, 2)
167
- await writeFileWithOverwritePrompt(eslintrc, content, {
168
- encoding: 'utf8',
169
- force: options.flags.force,
170
- })
171
- return true
172
- }
173
-
174
- async function writeEslintIgnore(options: InjectOptions) {
175
- if (!options.flags.eslint) {
176
- return false
177
- }
178
- const {basePath} = options
179
-
180
- const eslintignore = path.join(basePath, '.eslintignore')
181
-
182
- const content = outdent`
183
- .eslintrc.js
184
- commitlint.config.js
185
- lib
186
- lint-staged.config.js
187
- package.config.ts
188
- ${options.flags.typescript ? '*.js' : ''}
189
- `.trim()
190
-
191
- await writeFileWithOverwritePrompt(eslintignore, content, {
192
- encoding: 'utf8',
193
- force: options.flags.force,
194
- })
195
- return true
196
- }
197
-
198
156
  async function writeLicense(
199
157
  {license}: PackageData,
200
158
  options: InjectOptions,
@@ -323,43 +281,64 @@ async function resolveProjectDescription(basePath: string, pkg: PackageJson | un
323
281
  }
324
282
  }
325
283
 
326
- export async function writeAssets(files: FromTo[], {basePath, flags}: InjectOptions) {
284
+ export async function writeAssets(injectables: Injectable[], {basePath, flags}: InjectOptions) {
327
285
  const assetsDir = await findAssetsDir()
328
286
 
329
287
  const from = (...segments: string[]) => path.join(assetsDir, 'inject', ...segments)
330
288
  const to = (...segments: string[]) => path.join(basePath, ...segments)
331
289
 
332
290
  const writes: string[] = []
333
- for (const file of files) {
334
- const fromPath = asArray(file.from)
335
- const toPath = asArray(file.to)
336
- if (await copyFileWithOverwritePrompt(from(...fromPath), to(...toPath), flags)) {
291
+ for (const injectable of injectables) {
292
+ if (injectable.type === 'copy') {
293
+ const fromPath = asArray(injectable.from)
294
+ const toPath = asArray(injectable.to)
295
+ if (await copyFileWithOverwritePrompt(from(...fromPath), to(...toPath), flags)) {
296
+ writes.push(path.join(...toPath))
297
+ }
298
+ continue
299
+ }
300
+
301
+ if (injectable.type === 'template') {
302
+ const toPath = asArray(injectable.to)
303
+
304
+ await writeFileWithOverwritePrompt(to(...toPath), `${injectable.value.trim()}\n`, {
305
+ default: 'n',
306
+ force: injectable.force || flags.force,
307
+ })
308
+
337
309
  writes.push(path.join(...toPath))
310
+ continue
338
311
  }
312
+
313
+ throw new Error(`Unknown operation type "${(injectable as any).type}"`)
339
314
  }
340
315
 
341
316
  return writes
342
317
  }
343
318
 
344
319
  async function writeStaticAssets(options: InjectOptions) {
345
- const {flags} = options
346
-
347
- const files: FromTo[] = [
348
- {from: 'editorconfig', to: '.editorconfig'},
349
- {from: 'sanity.json', to: 'sanity.json'},
350
- {from: 'v2-incompatible.js.template', to: 'v2-incompatible.js'},
351
- {
352
- from: 'package.config.template',
353
- to: options.flags.typescript ? 'package.config.ts' : 'package.config.js',
354
- },
355
- flags.gitignore && {from: 'gitignore', to: '.gitignore'},
356
- flags.typescript && {from: 'template-tsconfig.json', to: 'tsconfig.json'},
357
- flags.prettier && {from: 'prettierrc.json', to: '.prettierrc.json'},
320
+ const {outDir, flags} = options
321
+
322
+ const files: Injectable[] = [
323
+ flags.eslint && eslintrcTemplate({flags: options.flags}),
324
+ flags.eslint && eslintignoreTemplate({outDir, flags: options.flags}),
325
+ {type: 'copy', from: 'editorconfig', to: '.editorconfig'},
326
+ {type: 'copy', from: 'sanity.json', to: 'sanity.json'},
327
+ {type: 'copy', from: 'v2-incompatible.js.template', to: 'v2-incompatible.js'},
328
+ pkgConfigTemplate({outDir, flags: options.flags}),
329
+ flags.gitignore && gitignoreTemplate(),
330
+ flags.typescript && tsconfigTemplate({flags: options.flags}),
331
+ flags.typescript && tsconfigTemplateDist({outDir, flags: options.flags}),
332
+ flags.typescript && tsconfigTemplateSettings({flags: options.flags}),
333
+ flags.prettier && prettierignoreTemplate({outDir}),
334
+ flags.prettier && {type: 'copy', from: 'prettierrc.json', to: '.prettierrc'},
358
335
  ]
359
- .map((f) => (f ? (f as FromTo) : undefined))
360
- .filter((f): f is FromTo => !!f)
336
+ .map((f) => (f ? (f as Injectable) : undefined))
337
+ .filter((f): f is Injectable => !!f)
338
+
339
+ const result = writeAssets(files, options)
361
340
 
362
- return writeAssets(files, options)
341
+ return result
363
342
  }
364
343
 
365
344
  function asArray(input: string | string[]): string[] {
@@ -368,7 +347,7 @@ function asArray(input: string | string[]): string[] {
368
347
 
369
348
  /**
370
349
  * assets dir might be in higher or lower in the dir hierarchy depending on
371
- * if we run from lib or src
350
+ * if we run from `dist` or `src`
372
351
  */
373
352
  async function findAssetsDir(): Promise<string> {
374
353
  let maxBackpaddle = 3
@@ -408,13 +387,13 @@ async function addCompileDirToGitIgnore(data: PackageData, options: InjectOption
408
387
  return false
409
388
  }
410
389
 
411
- const lines = gitignore.split('\n')
390
+ const lines = gitignore.trim().split('\n')
412
391
  if (lines.includes(ignore)) {
413
392
  return false
414
393
  }
415
394
 
416
- lines.push('# Compiled plugin', ignore, '\n')
395
+ lines.push('', '# Compiled plugin', ignore)
417
396
 
418
- await writeFile(gitIgnorePath, lines.join('\n'), {encoding: 'utf8'})
397
+ await writeFile(gitIgnorePath, lines.join('\n') + '\n', {encoding: 'utf8'})
419
398
  return true
420
399
  }
@@ -26,9 +26,10 @@ import log from '../util/log'
26
26
  import {getPackage} from '../npm/package'
27
27
  import outdent from 'outdent'
28
28
  import {fileExists, mkdir} from '../util/files'
29
+ import {loadConfig as loadPackageConfig} from '@sanity/pkg-utils'
30
+ import {defaultOutDir} from '../constants'
29
31
 
30
32
  interface YalcWatchConfig {
31
- folder?: string
32
33
  command?: string
33
34
  extensions?: string
34
35
  }
@@ -42,22 +43,24 @@ export async function linkWatch({basePath}: {basePath: string}) {
42
43
  fs.readFileSync(path.join(basePath, 'package.json'), 'utf8')
43
44
  )
44
45
 
46
+ const packageConfig = await loadPackageConfig({cwd: basePath})
47
+ const outDir = packageConfig?.dist ?? defaultOutDir
48
+
45
49
  const watch: Required<YalcWatchConfig> = {
46
- folder: 'lib',
47
50
  command: 'npm run watch',
48
51
  extensions: 'ts,js,png,svg,gif,jpeg,css',
49
52
  ...packageJson.sanityPlugin?.linkWatch,
50
53
  }
51
54
 
52
55
  nodemon({
53
- watch: [watch.folder],
56
+ watch: [outDir],
54
57
  ext: watch.extensions,
55
58
  exec: 'yalc push --changed',
56
59
  //delay: 1000
57
60
  })
58
61
 
59
62
  // ensure the folder exits so it can be watched
60
- const folder = path.join(basePath, watch.folder)
63
+ const folder = path.join(basePath, outDir)
61
64
  if (!(await fileExists(folder))) {
62
65
  await mkdir(folder)
63
66
  }
@@ -70,7 +73,7 @@ export async function linkWatch({basePath}: {basePath: string}) {
70
73
  .on('start', function () {
71
74
  log.info(
72
75
  outdent`
73
- Watching ${watch.folder} for changes to files with extensions: ${watch.extensions}
76
+ Watching ${outDir} for changes to files with extensions: ${watch.extensions}
74
77
 
75
78
  To test this package in Sanity Studio or another package, in a separate shell run:
76
79
  cd /path/to/sanity/studio-or-package
@@ -34,29 +34,6 @@ export interface PackageJson {
34
34
  [index: string]: unknown
35
35
  }
36
36
 
37
- export interface CompilerOptions {
38
- jsx?: string
39
- moduleResolution?: string
40
- target?: string
41
- module?: string
42
- sourceMap?: boolean
43
- inlineSourceMap?: boolean
44
- esModuleInterop?: boolean
45
- skipLibCheck?: boolean
46
- isolatedModules?: boolean
47
- downlevelIteration?: boolean
48
- declaration?: boolean
49
- allowSyntheticDefaultImports?: boolean
50
- outDir?: string
51
- baseUrl?: string
52
- checkJs?: false
53
- }
54
-
55
- export interface TsConfig {
56
- compilerOptions?: CompilerOptions
57
- include?: string[]
58
- }
59
-
60
37
  export interface SanityV2Json {
61
38
  parts?: [
62
39
  {
@@ -6,34 +6,20 @@ import findBabelConfig from 'find-babel-config'
6
6
  import {incompatiblePluginPackage, urls} from '../../constants'
7
7
  import {deprecatedDevDeps, mergedPackages} from '../../configs/banned-packages'
8
8
  import path from 'path'
9
- import {fileExists, readFileContent, readJson5File} from '../../util/files'
9
+ import {fileExists, readJson5File} from '../../util/files'
10
10
  import chalk from 'chalk'
11
- import {CompilerOptions, PackageJson, SanityStudioJson, SanityV2Json, TsConfig} from './types'
11
+ import {PackageJson, SanityStudioJson, SanityV2Json} from './types'
12
+ import {ParsedCommandLine} from 'typescript'
12
13
 
13
14
  export const expectedScripts = {
14
- prebuild: `plugin-kit verify-package --silent && pkg-utils`,
15
- build: 'pkg-utils build',
16
- watch: 'pkg-utils watch',
15
+ build:
16
+ 'run-s clean && plugin-kit verify-package --silent && pkg-utils build --strict && pkg-utils --strict',
17
+ watch: 'pkg-utils watch --strict',
17
18
  'link-watch': 'plugin-kit link-watch',
18
- prepublishOnly: 'npm run build',
19
+ prepublishOnly: 'run-s build',
19
20
  }
20
21
 
21
22
  const expectedModulesFields = ['source', 'exports', 'main', 'module', 'files']
22
- const expectedCompilerOptions = {
23
- jsx: 'preserve',
24
- moduleResolution: 'node',
25
- target: 'esnext',
26
- module: 'esnext',
27
- sourceMap: false,
28
- inlineSourceMap: false,
29
- esModuleInterop: true,
30
- skipLibCheck: true,
31
- isolatedModules: true,
32
- downlevelIteration: true,
33
- declaration: true,
34
- allowSyntheticDefaultImports: true,
35
- rootDir: 'src',
36
- }
37
23
 
38
24
  export function validateNodeEngine(packageJson: PackageJson) {
39
25
  const nodeVersionRange = '>=14'
@@ -52,7 +38,8 @@ export function validateNodeEngine(packageJson: PackageJson) {
52
38
  }
53
39
  }
54
40
 
55
- export function validateModule(packageJson: PackageJson): string[] {
41
+ export function validateModule(packageJson: PackageJson, options: {outDir: string}): string[] {
42
+ const {outDir} = options
56
43
  const errors: string[] = []
57
44
 
58
45
  const missingFields = expectedModulesFields.filter((field) => !packageJson[field])
@@ -67,24 +54,24 @@ export function validateModule(packageJson: PackageJson): string[] {
67
54
  Example:
68
55
 
69
56
  Given a plugin with entry-point in src/index.ts, using default @sanity/pkg-utils build command,
70
- package.json should contain the following entries to ensure that commonjs and esm outputs are built into lib:
57
+ package.json should contain the following entries to ensure that commonjs and esm outputs are built into ${outDir}:
71
58
 
72
59
  "source": "./src/index.ts",
73
60
  "exports": {
74
61
  ".": {
75
- "types": "./lib/src/index.d.ts",
62
+ "types": "./${outDir}/index.d.ts",
76
63
  "source": "./src/index.ts",
77
- "import": "./lib/index.esm.js",
78
- "require": "./lib/index.js",
79
- "default": "./lib/index.js"
64
+ "import": "./${outDir}/index.esm.js",
65
+ "require": "./${outDir}/index.js",
66
+ "default": "./${outDir}/index.js"
80
67
  }
81
68
  },
82
- "main": "./lib/index.js",
83
- "module": "./lib/index.esm.js",
84
- "types": "./lib/src/index.d.ts",
69
+ "main": "./${outDir}/index.js",
70
+ "module": "./${outDir}/index.esm.js",
71
+ "types": "./${outDir}/index.d.ts",
85
72
  "files": [
86
- "src",
87
- "lib"
73
+ "${outDir}",
74
+ "src"
88
75
  ],
89
76
 
90
77
  Refer to @sanity/pkg-utils for more: https://github.com/sanity-io/pkg-utils#sanitypkg-utils
@@ -115,8 +102,6 @@ export function validateScripts(packageJson: PackageJson): string[] {
115
102
  .join(', ')}
116
103
 
117
104
  This checks for that the commands-strings includes these terms.
118
- For example, this will validate ok:
119
- "prebuild": "npm run clean && ${expectedScripts.prebuild}",
120
105
 
121
106
  Please add the following to your package.json "scripts":
122
107
 
@@ -127,11 +112,52 @@ export function validateScripts(packageJson: PackageJson): string[] {
127
112
  return errors
128
113
  }
129
114
 
130
- export async function validateTsConfig(tsConfig: TsConfig) {
115
+ export async function validateTsConfig(
116
+ ts: ParsedCommandLine,
117
+ options: {basePath: string; outDir: string; tsconfig: string}
118
+ ) {
119
+ const {basePath, outDir, tsconfig} = options
120
+
131
121
  const errors: string[] = []
132
- const options = tsConfig.compilerOptions ?? {}
122
+
123
+ const expectedCompilerOptions = {
124
+ moduleResolution: 'node',
125
+ target: 'esnext',
126
+ module: 'esnext',
127
+ emitDeclarationOnly: true,
128
+ esModuleInterop: true,
129
+ skipLibCheck: true,
130
+ isolatedModules: true,
131
+ downlevelIteration: true,
132
+ declaration: true,
133
+ allowSyntheticDefaultImports: true,
134
+ rootDir: '.',
135
+ outDir,
136
+ }
137
+
133
138
  const wrongEntries = Object.entries(expectedCompilerOptions).filter(([key, value]) => {
134
- const option = options[key as keyof CompilerOptions]
139
+ let option: any = ts.options[key]
140
+
141
+ if (key === 'rootDir' && typeof option === 'string') {
142
+ option = path.relative(basePath, option) || '.'
143
+ }
144
+
145
+ if (key === 'outDir' && typeof option === 'string') {
146
+ option = path.relative(basePath, option) || '.'
147
+ }
148
+
149
+ if (key === 'moduleResolution' && option === 2) {
150
+ option = 'node'
151
+ }
152
+
153
+ if (key === 'target' && option === 99) {
154
+ option = 'esnext'
155
+ }
156
+
157
+ if (key === 'module' && option === 99) {
158
+ option = 'esnext'
159
+ }
160
+
135
161
  return typeof value === 'string' && typeof option === 'string'
136
162
  ? value.toLowerCase() !== option?.toLowerCase()
137
163
  : value !== option
@@ -144,13 +170,13 @@ export async function validateTsConfig(tsConfig: TsConfig) {
144
170
 
145
171
  errors.push(
146
172
  outdent`
147
- Recommended tsconfig.json compilerOptions missing:
173
+ Recommended ${tsconfig} compilerOptions missing:
148
174
 
149
175
  The following fields had unexpected values: [${wrongEntries.map(([key]) => key).join(', ')}]
150
176
  Expected to find these values:
151
177
  ${expectedOutput}
152
178
 
153
- Please update your tsconfig.json accordingly.
179
+ Please update your ${tsconfig} accordingly.
154
180
  `.trimStart()
155
181
  )
156
182
  }
@@ -2,10 +2,10 @@ import log from '../../util/log'
2
2
  import {TypedFlags} from 'meow'
3
3
  import fs from 'fs'
4
4
  import sharedFlags from '../../sharedFlags'
5
- import {TsConfig} from './types'
6
5
  import chalk from 'chalk'
7
6
  import outdent from 'outdent'
8
7
  import {runCommand} from '../../util/command-parser'
8
+ import {ParsedCommandLine} from 'typescript'
9
9
 
10
10
  export const readFile = fs.promises.readFile
11
11
  const splitLine = `\n----------------------------------------------------------`
@@ -82,10 +82,10 @@ export function createValidator(
82
82
  }
83
83
  }
84
84
 
85
- export async function runTscMaybe(verifyConfig: VerifyPackageConfig, tsConfig?: TsConfig) {
86
- if (tsConfig && verifyConfig.tsc !== false) {
85
+ export async function runTscMaybe(verifyConfig: VerifyPackageConfig, ts?: ParsedCommandLine) {
86
+ if (ts && verifyConfig.tsc !== false) {
87
87
  log.info('All checks ok, running Typescript compiler.')
88
- const {code} = await runCommand('tsc --noEmit')
88
+ const {code} = await runCommand('tsc --build')
89
89
  if (code !== 0) {
90
90
  throw new Error('Compilation failed. See output above.\n\n' + disableCheckText('tsc'))
91
91
  }
@@ -1,6 +1,7 @@
1
+ import {loadConfig as loadPackageConfig} from '@sanity/pkg-utils'
1
2
  import {getPackage} from '../npm/package'
2
3
  import log from '../util/log'
3
- import {cliName, urls} from '../constants'
4
+ import {cliName, defaultOutDir, urls} from '../constants'
4
5
  import {validateImports} from '../dependencies/import-linter'
5
6
  import outdent from 'outdent'
6
7
  import {
@@ -9,7 +10,6 @@ import {
9
10
  VerifyFlags,
10
11
  VerifyPackageConfig,
11
12
  } from './verify/verify-common'
12
- import {readJson5File} from '../util/files'
13
13
  import {
14
14
  validateBabelConfig,
15
15
  validateModule,
@@ -23,27 +23,33 @@ import {
23
23
  validateSanityDependencies,
24
24
  validateSrcIndexFile,
25
25
  } from './verify/validations'
26
- import {PackageJson, TsConfig} from './verify/types'
26
+ import {PackageJson} from './verify/types'
27
27
  import chalk from 'chalk'
28
+ import {readTSConfig} from '../util/ts'
28
29
 
29
30
  export async function verifyPackage({basePath, flags}: {basePath: string; flags: VerifyFlags}) {
30
31
  let errors: string[] = []
31
32
 
32
33
  const packageJson: PackageJson = await getPackage({basePath, validate: false})
33
34
  const verifyConfig: VerifyPackageConfig = packageJson.sanityPlugin?.verifyPackage || {}
35
+ const packageConfig = await loadPackageConfig({cwd: basePath})
36
+ const outDir = packageConfig?.dist ?? defaultOutDir
37
+ const tsconfig = packageConfig?.tsconfig ?? 'tsconfig.json'
34
38
 
35
39
  const validation = createValidator(verifyConfig, flags, errors)
36
40
 
37
- const tsConfig = await readJson5File<TsConfig>({basePath, filename: 'tsconfig.json'})
41
+ const ts = await readTSConfig({basePath, filename: tsconfig})
38
42
 
39
43
  await validation('packageName', async () => validatePackageName(packageJson))
40
44
  await validation('pkg-utils', async () => validatePkgUtilsDependency(packageJson))
41
45
  await validation('srcIndex', async () => validateSrcIndexFile(basePath))
42
46
  await validation('scripts', async () => validateScripts(packageJson))
43
- await validation('module', async () => validateModule(packageJson))
47
+ await validation('module', async () => validateModule(packageJson, {outDir}))
44
48
  await validation('nodeEngine', async () => validateNodeEngine(packageJson))
45
49
 
46
- tsConfig && (await validation('tsconfig', async () => validateTsConfig(tsConfig)))
50
+ if (ts) {
51
+ await validation('tsconfig', async () => validateTsConfig(ts, {basePath, outDir, tsconfig}))
52
+ }
47
53
 
48
54
  await validation('sanityV2Json', async () => validatePluginSanityJson({basePath, packageJson}))
49
55
 
@@ -76,7 +82,7 @@ export async function verifyPackage({basePath, flags}: {basePath: string; flags:
76
82
  )
77
83
  }
78
84
 
79
- await runTscMaybe(verifyConfig, tsConfig)
85
+ await runTscMaybe(verifyConfig, ts)
80
86
 
81
87
  log.success(
82
88
  outdent`
@@ -1,6 +1,6 @@
1
+ import {loadConfig as loadPackageConfig} from '@sanity/pkg-utils'
1
2
  import {getPackage} from '../npm/package'
2
3
  import log from '../util/log'
3
- import {readJson5File} from '../util/files'
4
4
  import {cliName, urls} from '../constants'
5
5
  import {validateImports} from '../dependencies/import-linter'
6
6
  import outdent from 'outdent'
@@ -11,18 +11,21 @@ import {
11
11
  VerifyFlags,
12
12
  VerifyPackageConfig,
13
13
  } from './verify/verify-common'
14
- import {PackageJson, TsConfig} from './verify/types'
14
+ import {PackageJson} from './verify/types'
15
15
  import {validateSanityDependencies, validateStudioConfig} from './verify/validations'
16
+ import {readTSConfig} from '../util/ts'
16
17
 
17
18
  export async function verifyStudio({basePath, flags}: {basePath: string; flags: VerifyFlags}) {
18
19
  let errors: string[] = []
19
20
 
20
21
  const packageJson: PackageJson = await getPackage({basePath, validate: false})
21
22
  const verifyConfig: VerifyPackageConfig = packageJson.sanityPlugin?.verifyPackage || {}
23
+ const packageConfig = await loadPackageConfig({cwd: basePath})
24
+ const tsconfig = packageConfig?.tsconfig ?? 'tsconfig.json'
22
25
 
23
26
  const validation = createValidator(verifyConfig, flags, errors)
24
27
 
25
- const tsConfig = await readJson5File<TsConfig>({basePath, filename: 'tsconfig.json'})
28
+ const ts = await readTSConfig({basePath, filename: tsconfig})
26
29
 
27
30
  await validation('studioConfig', async () => validateStudioConfig({basePath}))
28
31
  await validation('dependencies', async () => validateSanityDependencies(packageJson))
@@ -45,7 +48,7 @@ export async function verifyStudio({basePath, flags}: {basePath: string; flags:
45
48
  )
46
49
  }
47
50
 
48
- await runTscMaybe(verifyConfig, tsConfig)
51
+ await runTscMaybe(verifyConfig, ts)
49
52
 
50
53
  log.success(
51
54
  outdent`
package/src/cli.ts CHANGED
@@ -1,5 +1,3 @@
1
- #!/usr/bin/env node
2
- 'use strict'
3
1
  import meow from 'meow'
4
2
  import log from './util/log'
5
3
  import commands from './cmds'
@@ -64,7 +62,7 @@ export async function cliEntry(argv = process.argv, autoExit = true) {
64
62
  }
65
63
 
66
64
  // Lazy-load command
67
- const cmd = require(commands[commandName as keyof typeof commands]).default
65
+ const cmd = commands[commandName as keyof typeof commands]
68
66
 
69
67
  try {
70
68
  log.setVerbosity(cli.flags)
package/src/cmds/index.ts CHANGED
@@ -1,8 +1,20 @@
1
1
  export default {
2
- init: require.resolve('./init'),
3
- inject: require.resolve('./inject'),
4
- 'link-watch': require.resolve('./link-watch'),
5
- 'verify-package': require.resolve('./verify-package'),
6
- 'verify-studio': require.resolve('./verify-studio'),
7
- version: require.resolve('./version'),
2
+ init: async (options: {argv: string[]}) => {
3
+ await (await import('./init')).default(options)
4
+ },
5
+ inject: async (options: {argv: string[]}) => {
6
+ await (await import('./inject')).default(options)
7
+ },
8
+ 'link-watch': async (options: {argv: string[]}) => {
9
+ await (await import('./link-watch')).default(options)
10
+ },
11
+ 'verify-package': async (options: {argv: string[]}) => {
12
+ await (await import('./verify-package')).default(options)
13
+ },
14
+ 'verify-studio': async (options: {argv: string[]}) => {
15
+ await (await import('./verify-studio')).default(options)
16
+ },
17
+ version: async (options: {argv: string[]}) => {
18
+ await (await import('./version')).default(options)
19
+ },
8
20
  }
package/src/cmds/init.ts CHANGED
@@ -31,7 +31,7 @@ Options
31
31
  --license [spdx] Use the license with the given SPDX identifier
32
32
  --force No promt when overwriting files
33
33
 
34
- --preset [preset-name] [beta] - Adds config and files from a named preset. --preset can be supplied multiple times.
34
+ --preset [preset-name] Adds config and files from a named preset. --preset can be supplied multiple times.
35
35
  The following presets are available:
36
36
  ${presetHelpList(30)}
37
37