Skip to content

Commit 95e8cba

Browse files
committed
fix: semver-workflow readme is now compatible with init
1 parent fa2827e commit 95e8cba

File tree

5 files changed

+68
-71
lines changed

5 files changed

+68
-71
lines changed

src/actions/verify/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface PackageJson {
99
version?: string
1010
description?: string
1111
author?: string
12+
license?: string
1213
source?: string
1314
exports?: {
1415
[index: string]: Record<string, string> | string | undefined

src/configs/default-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function defaultSourceJs(pkg: PackageJson) {
1414
* import {myPlugin} from '${pkg.name}'
1515
*
1616
* export const defineConfig({
17-
* /...
17+
* //...
1818
* plugins: [
1919
* myPlugin({})
2020
* ]
@@ -49,7 +49,7 @@ export function defaultSourceTs(pkg: PackageJson) {
4949
* import {myPlugin} from '${pkg.name}'
5050
*
5151
* export const defineConfig({
52-
* /...
52+
* //...
5353
* plugins: [
5454
* myPlugin()
5555
* ]

src/npm/package.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {InjectOptions, PackageData} from '../actions/inject'
1414
import {expectedScripts} from '../actions/verify/validations'
1515
import {PackageJson} from '../actions/verify/types'
1616
import {forcedDevPackageVersions, forcedPackageVersions} from '../configs/forced-package-versions'
17-
1817
const defaultDependencies = [incompatiblePluginPackage]
1918

2019
const defaultDevDependencies = ['rimraf', 'react', 'sanity']
@@ -248,21 +247,18 @@ export async function writePackageJson(data: PackageData, options: InjectOptions
248247
...(await resolveLatestVersions(defaultPeerDependencies)),
249248
})
250249

251-
const alwaysOnTop = {
250+
const source = flags.typescript ? './src/index.ts' : './src/index.js'
251+
252+
// order should be compatible with prettier-plugin-packagejson
253+
const forcedOrder = {
252254
name: pluginName,
253255
version: prev.version ?? '1.0.0',
254256
description: description || '',
257+
keywords: prev.keywords ?? ['sanity', 'sanity-plugin'],
258+
...urlsFromOrigin(gitOrigin),
259+
...repoFromOrigin(gitOrigin),
260+
license: license ? license.id : 'UNLICENSED',
255261
author: user?.email ? `${user.name} <${user.email}>` : user?.name,
256-
license: license ? license.id : 'UNLICENSE',
257-
}
258-
259-
const source = flags.typescript ? './src/index.ts' : './src/index.js'
260-
const manifest: PackageJson = {
261-
...alwaysOnTop,
262-
// Use already configured values by default
263-
...(prev || {}),
264-
// but we override these to enforce standardization
265-
source,
266262
exports: {
267263
'.': {
268264
...(flags.typescript ? {types: './lib/src/index.d.ts'} : {}),
@@ -275,23 +271,24 @@ export async function writePackageJson(data: PackageData, options: InjectOptions
275271
},
276272
main: './lib/index.js',
277273
module: './lib/index.esm.js',
274+
source,
278275
...(flags.typescript ? {types: './lib/src/index.d.ts'} : {}),
279276
files: ['src', 'lib', 'v2-incompatible.js', 'sanity.json'],
280-
engines: {
281-
node: '>=14',
282-
},
283277
scripts: {...prev.scripts},
284-
repository: {...prev.repository},
285-
286-
// We're de-declaring properties because of key order in package.json
287-
...alwaysOnTop,
288278
dependencies: sortKeys(dependencies),
289279
devDependencies: sortKeys(devDependencies),
290280
peerDependencies: sortKeys(peerDependencies),
291-
/* eslint-enable no-dupe-keys */
281+
engines: {
282+
node: '>=14',
283+
},
284+
}
292285

293-
...repoFromOrigin(gitOrigin),
294-
...urlsFromOrigin(gitOrigin),
286+
const manifest: PackageJson = {
287+
...forcedOrder,
288+
// Use already configured values by default (if not otherwise specified)
289+
...(prev || {}),
290+
// We're de-declaring properties because of key order in package.json
291+
...forcedOrder,
295292
}
296293

297294
// we use types, not typings
@@ -313,14 +310,15 @@ function urlsFromOrigin(gitOrigin?: string): {bugs?: {url: string}; homepage?: s
313310
}
314311

315312
return {
313+
homepage: `https://github.com/${details.user}/${details.repo}#readme`,
316314
bugs: {
317315
url: `https://github.com/${details.user}/${details.repo}/issues`,
318316
},
319-
homepage: `https://github.com/${details.user}/${details.repo}#readme`,
320317
}
321318
}
322319

323320
function repoFromOrigin(gitOrigin?: string) {
321+
console.log(gitOrigin)
324322
if (!gitOrigin) {
325323
return {}
326324
}
@@ -367,13 +365,13 @@ export async function addBuildScripts(manifest: PackageJson, options: InjectOpti
367365
return false
368366
}
369367
return addPackageJsonScripts(manifest, options, (scripts) => {
370-
scripts.clean = addScript(`rimraf lib`, scripts.clean)
371-
scripts.lint = addScript(`eslint .`, scripts.lint)
372368
scripts.prebuild = addScript('npm run clean && ' + expectedScripts.prebuild, scripts.prebuild)
373369
scripts.build = addScript(expectedScripts.build, scripts.build)
374-
scripts.watch = addScript(expectedScripts.watch, scripts.watch)
370+
scripts.clean = addScript(`rimraf lib`, scripts.clean)
375371
scripts['link-watch'] = addScript(expectedScripts['link-watch'], scripts['link-watch'])
372+
scripts.lint = addScript(`eslint .`, scripts.lint)
376373
scripts.prepublishOnly = addScript(expectedScripts.prepublishOnly, scripts.prepublishOnly)
374+
scripts.watch = addScript(expectedScripts.watch, scripts.watch)
377375
return scripts
378376
})
379377
}

src/presets/semver-workflow.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import path from 'path'
1515
import {readFile, writeFile} from '../util/files'
1616
import {errorToUndefined} from '../util/errorToUndefined'
1717
import {PackageJson} from '../actions/verify/types'
18+
import {developTestSnippet, getLicenseText, installationSnippet} from '../util/readme'
19+
import {getUserInfo} from '../util/user'
1820

1921
export const semverWorkflowPreset: Preset = {
2022
name: 'semver-workflow',
@@ -71,10 +73,12 @@ async function updateReadme(options: InjectOptions) {
7173
const readmePath = path.join(basePath, 'README.md')
7274
const readme = (await readFile(readmePath, 'utf8').catch(errorToUndefined)) ?? ''
7375

74-
const {v3Banner, install, usage, developFooter} = await readmeSnippets(options)
76+
const {v3Banner, install, usage, developTest, license, releaseSnippet} = await readmeSnippets(
77+
options
78+
)
7579

7680
const prependSections = missingSections(readme, [v3Banner, install, usage])
77-
const appendSections = missingSections(readme, [developFooter])
81+
const appendSections = missingSections(readme, [license, developTest, releaseSnippet])
7882

7983
if (prependSections.length || appendSections.length) {
8084
const updatedReadme = [...prependSections, readme, ...appendSections]
@@ -87,6 +91,7 @@ async function updateReadme(options: InjectOptions) {
8791

8892
async function readmeSnippets(options: InjectOptions) {
8993
const pkg = await getPackage(options)
94+
const user = await getUserInfo(options, pkg)
9095

9196
const bestEffortUrl = readmeBaseurl(pkg)
9297

@@ -98,37 +103,15 @@ async function readmeSnippets(options: InjectOptions) {
98103
> For the v2 version, please refer to the [v2-branch](${bestEffortUrl}).
99104
`
100105

101-
const install = outdent`
102-
## Installation
103-
104-
\`\`\`
105-
npm install --save ${pkg.name}@studio-v3
106-
\`\`\`
107-
108-
or
109-
110-
\`\`\`
111-
yarn add ${pkg.name}@studio-v3
112-
\`\`\`
113-
`
106+
const install = installationSnippet(pkg.name ?? 'unknown')
114107

115108
const usage = outdent`
116109
## Usage
117110
`
118111

119-
const developFooter = outdent`
120-
## License
121-
122-
MIT-licensed. See LICENSE.
123-
124-
## Develop & test
125-
126-
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
127-
with default configuration for build & watch scripts.
128-
129-
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
130-
on how to run this plugin with hotreload in the studio.
112+
const license = getLicenseText(typeof pkg.license === 'string' ? pkg.license : undefined, user)
131113

114+
const releaseSnippet = outdent`
132115
### Release new version
133116
134117
Run ["CI & Release" workflow](${bestEffortUrl}/actions/workflows/main.yml).
@@ -141,7 +124,9 @@ async function readmeSnippets(options: InjectOptions) {
141124
v3Banner,
142125
install,
143126
usage,
144-
developFooter,
127+
license,
128+
developTest: developTestSnippet(),
129+
releaseSnippet,
145130
}
146131
}
147132

src/util/readme.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@ export function generateReadme(data: PackageData) {
1010
return outdent`
1111
# ${pluginName}
1212
13-
## Installation
14-
15-
\`\`\`
16-
npm install --save ${pluginName}
17-
\`\`\`
18-
19-
or
20-
21-
\`\`\`
22-
yarn add ${pluginName}
23-
\`\`\`
13+
${installationSnippet(pluginName ?? 'unknown')}
2414
2515
## Usage
2616
Add it as a plugin in sanity.config.ts (or .js):
@@ -30,13 +20,36 @@ export function generateReadme(data: PackageData) {
3020
import {myPlugin} from '${pluginName}'
3121
3222
export const defineConfig({
33-
/...
23+
//...
3424
plugins: [
3525
myPlugin({})
3626
]
3727
})
3828
\`\`\`
3929
${getLicenseText(license?.id, user?.name ? (user as User) : undefined)}
30+
${developTestSnippet()}
31+
`
32+
}
33+
34+
export function installationSnippet(packageName: string) {
35+
return outdent`
36+
## Installation
37+
38+
\`\`\`
39+
npm install --save ${packageName}@studio-v3
40+
\`\`\`
41+
`
42+
}
43+
44+
export function developTestSnippet() {
45+
return outdent`
46+
## Develop & test
47+
48+
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
49+
with default configuration for build & watch scripts.
50+
51+
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
52+
on how to run this plugin with hotreload in the studio.
4053
`
4154
}
4255

@@ -50,11 +63,11 @@ export function getLicenseText(licenseId?: string, user?: User) {
5063

5164
let licenseText = '## License\n'
5265
if (licenseName && user?.name) {
53-
licenseText = `${licenseText}\n${licenseName} © ${user?.name}\nSee LICENSE`
66+
licenseText = `${licenseText}\n[${licenseName}](LICENSE) © ${user?.name}\n`
5467
} else if (licenseName) {
55-
licenseText = `${licenseText}\n${licenseName}\nSee LICENSE`
68+
licenseText = `${licenseText}\n[${licenseName}](LICENSE)\n`
5669
} else {
57-
licenseText = `${licenseText}\nSee LICENSE`
70+
licenseText = `${licenseText}\nSee [LICENSE](LICENSE)`
5871
}
5972

6073
return licenseText

0 commit comments

Comments
 (0)