@sanity/plugin-kit 0.0.1-studio-v3.1

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 (194) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +398 -0
  3. package/assets/splat/LICENSE +21 -0
  4. package/assets/splat/editorconfig +13 -0
  5. package/assets/splat/eslint.config.js +5 -0
  6. package/assets/splat/gitignore +55 -0
  7. package/assets/splat/npmignore +9 -0
  8. package/assets/splat/prettierrc.js +6 -0
  9. package/assets/splat/sanity.json +8 -0
  10. package/assets/splat/template-tsconfig.json +23 -0
  11. package/assets/splat/v2-incompatible.js.template +11 -0
  12. package/lib/package.json +127 -0
  13. package/lib/src/actions/init.d.ts +65 -0
  14. package/lib/src/actions/init.js +83 -0
  15. package/lib/src/actions/init.js.map +1 -0
  16. package/lib/src/actions/link-watch.d.ts +3 -0
  17. package/lib/src/actions/link-watch.js +69 -0
  18. package/lib/src/actions/link-watch.js.map +1 -0
  19. package/lib/src/actions/splat.d.ts +26 -0
  20. package/lib/src/actions/splat.js +296 -0
  21. package/lib/src/actions/splat.js.map +1 -0
  22. package/lib/src/actions/verify/types.d.ts +77 -0
  23. package/lib/src/actions/verify/types.js +3 -0
  24. package/lib/src/actions/verify/types.js.map +1 -0
  25. package/lib/src/actions/verify/validations.d.ts +28 -0
  26. package/lib/src/actions/verify/validations.js +379 -0
  27. package/lib/src/actions/verify/validations.js.map +1 -0
  28. package/lib/src/actions/verify/verify-common.d.ts +43 -0
  29. package/lib/src/actions/verify/verify-common.js +88 -0
  30. package/lib/src/actions/verify/verify-common.js.map +1 -0
  31. package/lib/src/actions/verify-package.d.ts +5 -0
  32. package/lib/src/actions/verify-package.js +72 -0
  33. package/lib/src/actions/verify-package.js.map +1 -0
  34. package/lib/src/actions/verify-studio.d.ts +5 -0
  35. package/lib/src/actions/verify-studio.js +55 -0
  36. package/lib/src/actions/verify-studio.js.map +1 -0
  37. package/lib/src/actions/verify.d.ts +0 -0
  38. package/lib/src/actions/verify.js +330 -0
  39. package/lib/src/actions/verify.js.map +1 -0
  40. package/lib/src/cli.d.ts +2 -0
  41. package/lib/src/cli.js +86 -0
  42. package/lib/src/cli.js.map +1 -0
  43. package/lib/src/cmds/index.d.ts +8 -0
  44. package/lib/src/cmds/index.js +12 -0
  45. package/lib/src/cmds/index.js.map +1 -0
  46. package/lib/src/cmds/init.d.ts +4 -0
  47. package/lib/src/cmds/init.js +90 -0
  48. package/lib/src/cmds/init.js.map +1 -0
  49. package/lib/src/cmds/link-watch.d.ts +4 -0
  50. package/lib/src/cmds/link-watch.js +49 -0
  51. package/lib/src/cmds/link-watch.js.map +1 -0
  52. package/lib/src/cmds/splat.d.ts +4 -0
  53. package/lib/src/cmds/splat.js +63 -0
  54. package/lib/src/cmds/splat.js.map +1 -0
  55. package/lib/src/cmds/verify-package.d.ts +4 -0
  56. package/lib/src/cmds/verify-package.js +38 -0
  57. package/lib/src/cmds/verify-package.js.map +1 -0
  58. package/lib/src/cmds/verify-studio.d.ts +4 -0
  59. package/lib/src/cmds/verify-studio.js +38 -0
  60. package/lib/src/cmds/verify-studio.js.map +1 -0
  61. package/lib/src/cmds/verify.d.ts +0 -0
  62. package/lib/src/cmds/verify.js +42 -0
  63. package/lib/src/cmds/verify.js.map +1 -0
  64. package/lib/src/cmds/version.d.ts +4 -0
  65. package/lib/src/cmds/version.js +55 -0
  66. package/lib/src/cmds/version.js.map +1 -0
  67. package/lib/src/configs/buildExtensions.d.ts +1 -0
  68. package/lib/src/configs/buildExtensions.js +5 -0
  69. package/lib/src/configs/buildExtensions.js.map +1 -0
  70. package/lib/src/configs/default-source.d.ts +3 -0
  71. package/lib/src/configs/default-source.js +70 -0
  72. package/lib/src/configs/default-source.js.map +1 -0
  73. package/lib/src/configs/merged-packages.d.ts +1 -0
  74. package/lib/src/configs/merged-packages.js +24 -0
  75. package/lib/src/configs/merged-packages.js.map +1 -0
  76. package/lib/src/configs/uselessFiles.d.ts +1 -0
  77. package/lib/src/configs/uselessFiles.js +33 -0
  78. package/lib/src/configs/uselessFiles.js.map +1 -0
  79. package/lib/src/constants.d.ts +11 -0
  80. package/lib/src/constants.js +15 -0
  81. package/lib/src/constants.js.map +1 -0
  82. package/lib/src/dependencies/find.d.ts +0 -0
  83. package/lib/src/dependencies/find.js +195 -0
  84. package/lib/src/dependencies/find.js.map +1 -0
  85. package/lib/src/dependencies/import-linter.d.ts +3 -0
  86. package/lib/src/dependencies/import-linter.js +112 -0
  87. package/lib/src/dependencies/import-linter.js.map +1 -0
  88. package/lib/src/index.d.ts +2 -0
  89. package/lib/src/index.js +6 -0
  90. package/lib/src/index.js.map +1 -0
  91. package/lib/src/npm/manager.d.ts +7 -0
  92. package/lib/src/npm/manager.js +62 -0
  93. package/lib/src/npm/manager.js.map +1 -0
  94. package/lib/src/npm/package.d.ts +8 -0
  95. package/lib/src/npm/package.js +288 -0
  96. package/lib/src/npm/package.js.map +1 -0
  97. package/lib/src/npm/publish.d.ts +1 -0
  98. package/lib/src/npm/publish.js +14 -0
  99. package/lib/src/npm/publish.js.map +1 -0
  100. package/lib/src/npm/resolveLatestVersions.d.ts +3 -0
  101. package/lib/src/npm/resolveLatestVersions.js +35 -0
  102. package/lib/src/npm/resolveLatestVersions.js.map +1 -0
  103. package/lib/src/sanity/manifest.d.ts +48 -0
  104. package/lib/src/sanity/manifest.js +263 -0
  105. package/lib/src/sanity/manifest.js.map +1 -0
  106. package/lib/src/sharedFlags.d.ts +15 -0
  107. package/lib/src/sharedFlags.js +17 -0
  108. package/lib/src/sharedFlags.js.map +1 -0
  109. package/lib/src/util/command-parser.d.ts +9 -0
  110. package/lib/src/util/command-parser.js +41 -0
  111. package/lib/src/util/command-parser.js.map +1 -0
  112. package/lib/src/util/errorToUndefined.d.ts +1 -0
  113. package/lib/src/util/errorToUndefined.js +11 -0
  114. package/lib/src/util/errorToUndefined.js.map +1 -0
  115. package/lib/src/util/files.d.ts +36 -0
  116. package/lib/src/util/files.js +253 -0
  117. package/lib/src/util/files.js.map +1 -0
  118. package/lib/src/util/log.d.ts +14 -0
  119. package/lib/src/util/log.js +36 -0
  120. package/lib/src/util/log.js.map +1 -0
  121. package/lib/src/util/prompt.d.ts +13 -0
  122. package/lib/src/util/prompt.js +75 -0
  123. package/lib/src/util/prompt.js.map +1 -0
  124. package/lib/src/util/readme.d.ts +5 -0
  125. package/lib/src/util/readme.js +73 -0
  126. package/lib/src/util/readme.js.map +1 -0
  127. package/lib/src/util/request.d.ts +1 -0
  128. package/lib/src/util/request.js +19 -0
  129. package/lib/src/util/request.js.map +1 -0
  130. package/lib/src/util/user.d.ts +10 -0
  131. package/lib/src/util/user.js +106 -0
  132. package/lib/src/util/user.js.map +1 -0
  133. package/lib/test/cli.test.d.ts +1 -0
  134. package/lib/test/cli.test.js +64 -0
  135. package/lib/test/cli.test.js.map +1 -0
  136. package/lib/test/fixture-utils.d.ts +25 -0
  137. package/lib/test/fixture-utils.js +67 -0
  138. package/lib/test/fixture-utils.js.map +1 -0
  139. package/lib/test/init-verify-build.test.d.ts +1 -0
  140. package/lib/test/init-verify-build.test.js +75 -0
  141. package/lib/test/init-verify-build.test.js.map +1 -0
  142. package/lib/test/init.test.d.ts +1 -0
  143. package/lib/test/init.test.js +137 -0
  144. package/lib/test/init.test.js.map +1 -0
  145. package/lib/test/run-test-command.d.ts +1 -0
  146. package/lib/test/run-test-command.js +6 -0
  147. package/lib/test/run-test-command.js.map +1 -0
  148. package/lib/test/verify-package.test.d.ts +1 -0
  149. package/lib/test/verify-package.test.js +81 -0
  150. package/lib/test/verify-package.test.js.map +1 -0
  151. package/lib/test/version.test.d.ts +1 -0
  152. package/lib/test/version.test.js +48 -0
  153. package/lib/test/version.test.js.map +1 -0
  154. package/package.json +127 -0
  155. package/src/actions/init.ts +104 -0
  156. package/src/actions/link-watch.ts +74 -0
  157. package/src/actions/splat.ts +366 -0
  158. package/src/actions/verify/types.ts +84 -0
  159. package/src/actions/verify/validations.ts +401 -0
  160. package/src/actions/verify/verify-common.ts +92 -0
  161. package/src/actions/verify-package.ts +87 -0
  162. package/src/actions/verify-studio.ts +55 -0
  163. package/src/actions/verify.ts +328 -0
  164. package/src/cli.ts +77 -0
  165. package/src/cmds/index.ts +9 -0
  166. package/src/cmds/init.ts +85 -0
  167. package/src/cmds/link-watch.ts +51 -0
  168. package/src/cmds/splat.ts +59 -0
  169. package/src/cmds/verify-package.ts +36 -0
  170. package/src/cmds/verify-studio.ts +36 -0
  171. package/src/cmds/verify.ts +40 -0
  172. package/src/cmds/version.ts +67 -0
  173. package/src/configs/buildExtensions.ts +1 -0
  174. package/src/configs/default-source.ts +68 -0
  175. package/src/configs/merged-packages.ts +20 -0
  176. package/src/configs/uselessFiles.ts +29 -0
  177. package/src/constants.ts +13 -0
  178. package/src/dependencies/find.ts +193 -0
  179. package/src/dependencies/import-linter.ts +103 -0
  180. package/src/index.ts +4 -0
  181. package/src/npm/manager.ts +44 -0
  182. package/src/npm/package.ts +370 -0
  183. package/src/npm/publish.ts +9 -0
  184. package/src/npm/resolveLatestVersions.ts +26 -0
  185. package/src/sanity/manifest.ts +340 -0
  186. package/src/sharedFlags.ts +14 -0
  187. package/src/util/command-parser.ts +31 -0
  188. package/src/util/errorToUndefined.ts +7 -0
  189. package/src/util/files.ts +249 -0
  190. package/src/util/log.ts +44 -0
  191. package/src/util/prompt.ts +70 -0
  192. package/src/util/readme.ts +72 -0
  193. package/src/util/request.ts +13 -0
  194. package/src/util/user.ts +110 -0
@@ -0,0 +1,70 @@
1
+ import {URL} from 'url'
2
+ import path from 'path'
3
+ import inquirer from 'inquirer'
4
+ // @ts-expect-error missing types
5
+ import validNpmName from 'validate-npm-package-name'
6
+ // @ts-expect-error missing types
7
+ import githubUrlToObject from 'github-url-to-object'
8
+ import {SplatOptions} from '../actions/splat'
9
+
10
+ export async function prompt(
11
+ message: string,
12
+ options: {
13
+ choices?: any
14
+ type?: string
15
+ default?: any
16
+ filter?: (val: any) => any
17
+ validate?: (val: any) => boolean | string
18
+ }
19
+ ) {
20
+ const type = options.choices ? 'list' : options.type
21
+ const result = await inquirer.prompt([{...options, type, message, name: 'single'}])
22
+ return result && result.single
23
+ }
24
+
25
+ prompt.separator = () => new inquirer.Separator()
26
+
27
+ export function promptForPackageName({basePath}: SplatOptions, defaultVal?: string) {
28
+ return prompt('Plugin name (sanity-plugin-...)', {
29
+ default: defaultVal || path.basename(basePath),
30
+ filter: (name) => {
31
+ const prefixless = name.trim().replace(/^sanity-plugin-/, '')
32
+ return name[0] === '@' ? name : `sanity-plugin-${prefixless}`
33
+ },
34
+ validate: (name) => {
35
+ const valid: {errors?: string[]} = validNpmName(name)
36
+ if (valid.errors) {
37
+ return valid.errors[0]
38
+ }
39
+
40
+ if (name[0] !== '@' && name.endsWith('plugin')) {
41
+ return `Name shouldn't include "plugin" multiple times (${name})`
42
+ }
43
+
44
+ return true
45
+ },
46
+ })
47
+ }
48
+
49
+ export function promptForRepoOrigin(options: SplatOptions, defaultVal?: string) {
50
+ return prompt('Git repository URL', {
51
+ default: defaultVal,
52
+ filter: (raw) => {
53
+ const url = (raw || '').trim()
54
+ const gh: {user: string; repo: string} | undefined = githubUrlToObject(url)
55
+ return gh ? `git+ssh://git@github.com/${gh.user}/${gh.repo}.git` : url
56
+ },
57
+ validate: (url) => {
58
+ if (!url) {
59
+ return true
60
+ }
61
+
62
+ try {
63
+ const parsed = new URL(url)
64
+ return parsed ? true : 'Invalid URL'
65
+ } catch (err) {
66
+ return 'Invalid URL'
67
+ }
68
+ },
69
+ })
70
+ }
@@ -0,0 +1,72 @@
1
+ import outdent from 'outdent'
2
+ // @ts-expect-error missing types
3
+ import licenses from '@rexxars/choosealicense-list'
4
+ import {PackageData} from '../actions/splat'
5
+ import {User} from './user'
6
+
7
+ export function generateReadme(data: PackageData) {
8
+ const {user, pluginName, license, description} = data
9
+
10
+ return outdent`
11
+ # ${pluginName}
12
+
13
+ ## Installation
14
+
15
+ \`\`\`
16
+ npm install --save ${pluginName}
17
+ \`\`\`
18
+
19
+ or
20
+
21
+ \`\`\`
22
+ yarn add ${pluginName}
23
+ \`\`\`
24
+
25
+ ## Usage
26
+ Add it as a plugin in sanity.config.ts (or .js):
27
+
28
+ \`\`\`
29
+ import {createConfig} from 'sanity'
30
+ import {myPlugin} from '${pluginName}'
31
+
32
+ export const createConfig({
33
+ /...
34
+ plugins: [
35
+ myPlugin({})
36
+ ]
37
+ })
38
+ \`\`\`
39
+ ${getLicenseText(license?.id, user?.name ? (user as User) : undefined)}
40
+ `
41
+ }
42
+
43
+ export function getLicenseText(licenseId?: string, user?: User) {
44
+ if (!licenseId) {
45
+ return ''
46
+ }
47
+
48
+ let licenseName: string | undefined = licenses.find(licenseId).title
49
+ licenseName = licenseName?.replace(/\s+license$/i, '')
50
+
51
+ let licenseText = '## License\n'
52
+ if (licenseName && user?.name) {
53
+ licenseText = `${licenseText}\n${licenseName} © ${user?.name}\nSee LICENSE`
54
+ } else if (licenseName) {
55
+ licenseText = `${licenseText}\n${licenseName}\nSee LICENSE`
56
+ } else {
57
+ licenseText = `${licenseText}\nSee LICENSE`
58
+ }
59
+
60
+ return licenseText
61
+ }
62
+
63
+ export function isDefaultGitHubReadme(readme: string) {
64
+ if (!readme) {
65
+ return false
66
+ }
67
+
68
+ const lines = readme.split('\n', 20).filter(Boolean)
69
+
70
+ // title + _optional_ description
71
+ return lines.length <= 2 && lines[0].startsWith('#')
72
+ }
@@ -0,0 +1,13 @@
1
+ // @ts-expect-error missing types
2
+ import getIt from 'get-it'
3
+ // @ts-expect-error missing types
4
+ import {jsonRequest, jsonResponse, httpErrors, headers, promise} from 'get-it/middleware'
5
+ import pkg from '../../package.json'
6
+
7
+ export const request = getIt([
8
+ promise({onlyBody: true}),
9
+ jsonRequest(),
10
+ jsonResponse(),
11
+ httpErrors(),
12
+ headers({'User-Agent': `${pkg.name}@${pkg.version}`}),
13
+ ])
@@ -0,0 +1,110 @@
1
+ import path from 'path'
2
+ import xdgBasedir from 'xdg-basedir'
3
+ // @ts-expect-error missing types
4
+ import gitUserInfo from 'git-user-info'
5
+ import {validate as isValidEmail} from 'email-validator'
6
+ import {readJsonFile} from './files'
7
+ import {request} from './request'
8
+ import {prompt} from './prompt'
9
+ import {SplatOptions} from '../actions/splat'
10
+ import {PackageJson} from '../actions/verify/types'
11
+
12
+ export interface User {
13
+ name: string
14
+ email?: string
15
+ }
16
+
17
+ export async function getUserInfo(
18
+ {requireUserConfirmation, flags}: SplatOptions,
19
+ pkg?: PackageJson
20
+ ): Promise<User | undefined> {
21
+ const userInfo =
22
+ getPackageUserInfo({author: flags.author ?? pkg?.author}) ||
23
+ (await getSanityUserInfo()) ||
24
+ ((await getGitUserInfo()) as User | undefined)
25
+ if (requireUserConfirmation) {
26
+ return promptForInfo(userInfo)
27
+ }
28
+
29
+ return userInfo
30
+ }
31
+
32
+ export function getPackageUserInfo(pkg?: {author?: string}): User | undefined {
33
+ if (!pkg || !pkg.author) {
34
+ return undefined
35
+ }
36
+
37
+ if (!pkg.author.includes('@')) {
38
+ return {name: pkg.author}
39
+ }
40
+
41
+ const [pre, ...post] = pkg.author.replace(/[<>[\]]/g, '').split(/@/)
42
+ const nameParts = pre.split(/\s+/)
43
+ const email = [nameParts[nameParts.length - 1], ...post].join('@')
44
+ const name = nameParts.slice(0, -1).join(' ')
45
+ return {name, email}
46
+ }
47
+
48
+ async function promptForInfo(defValue?: User) {
49
+ const name = await prompt('Author name', {
50
+ filter: filterString,
51
+ default: defValue && defValue.name,
52
+ validate: requiredString,
53
+ })
54
+
55
+ const email = await prompt('Author email', {
56
+ filter: filterString,
57
+ default: defValue && defValue.email,
58
+ validate: validOrEmptyEmail,
59
+ })
60
+
61
+ return {name, email}
62
+ }
63
+
64
+ async function getSanityUserInfo(): Promise<User | undefined> {
65
+ try {
66
+ const data = await readJsonFile<{authToken?: string}>(
67
+ path.join(xdgBasedir.config ?? '', 'sanity', 'config.json')
68
+ )
69
+ const token = data?.authToken
70
+
71
+ if (!token) {
72
+ return undefined
73
+ }
74
+
75
+ const user = await request({
76
+ url: 'https://api.sanity.io/v1/users/me',
77
+ headers: {Authorization: `Bearer ${token}`},
78
+ })
79
+
80
+ if (!user) {
81
+ return undefined
82
+ }
83
+
84
+ const {name, email} = user
85
+ return {name, email}
86
+ } catch (err) {
87
+ return undefined
88
+ }
89
+ }
90
+
91
+ function getGitUserInfo(): User | undefined {
92
+ const user = gitUserInfo()
93
+ return user ? {name: user.name, email: user.email} : undefined
94
+ }
95
+
96
+ function filterString(val: string) {
97
+ return (val || '').trim()
98
+ }
99
+
100
+ function requiredString(value: string) {
101
+ return value.length > 1 ? true : 'Required'
102
+ }
103
+
104
+ function validOrEmptyEmail(value: string): true | string {
105
+ if (!value) {
106
+ return true
107
+ }
108
+
109
+ return isValidEmail(value) ? true : 'Must either be a valid email or empty'
110
+ }