Skip to content

Commit

Permalink
fix: semver-workflow readme is now compatible with init
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Nov 14, 2022
1 parent fa2827e commit 95e8cba
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 71 deletions.
1 change: 1 addition & 0 deletions src/actions/verify/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface PackageJson {
version?: string
description?: string
author?: string
license?: string
source?: string
exports?: {
[index: string]: Record<string, string> | string | undefined
Expand Down
4 changes: 2 additions & 2 deletions src/configs/default-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function defaultSourceJs(pkg: PackageJson) {
* import {myPlugin} from '${pkg.name}'
*
* export const defineConfig({
* /...
* //...
* plugins: [
* myPlugin({})
* ]
Expand Down Expand Up @@ -49,7 +49,7 @@ export function defaultSourceTs(pkg: PackageJson) {
* import {myPlugin} from '${pkg.name}'
*
* export const defineConfig({
* /...
* //...
* plugins: [
* myPlugin()
* ]
Expand Down
50 changes: 24 additions & 26 deletions src/npm/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {InjectOptions, PackageData} from '../actions/inject'
import {expectedScripts} from '../actions/verify/validations'
import {PackageJson} from '../actions/verify/types'
import {forcedDevPackageVersions, forcedPackageVersions} from '../configs/forced-package-versions'

const defaultDependencies = [incompatiblePluginPackage]

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

const alwaysOnTop = {
const source = flags.typescript ? './src/index.ts' : './src/index.js'

// order should be compatible with prettier-plugin-packagejson
const forcedOrder = {
name: pluginName,
version: prev.version ?? '1.0.0',
description: description || '',
keywords: prev.keywords ?? ['sanity', 'sanity-plugin'],
...urlsFromOrigin(gitOrigin),
...repoFromOrigin(gitOrigin),
license: license ? license.id : 'UNLICENSED',
author: user?.email ? `${user.name} <${user.email}>` : user?.name,
license: license ? license.id : 'UNLICENSE',
}

const source = flags.typescript ? './src/index.ts' : './src/index.js'
const manifest: PackageJson = {
...alwaysOnTop,
// Use already configured values by default
...(prev || {}),
// but we override these to enforce standardization
source,
exports: {
'.': {
...(flags.typescript ? {types: './lib/src/index.d.ts'} : {}),
Expand All @@ -275,23 +271,24 @@ export async function writePackageJson(data: PackageData, options: InjectOptions
},
main: './lib/index.js',
module: './lib/index.esm.js',
source,
...(flags.typescript ? {types: './lib/src/index.d.ts'} : {}),
files: ['src', 'lib', 'v2-incompatible.js', 'sanity.json'],
engines: {
node: '>=14',
},
scripts: {...prev.scripts},
repository: {...prev.repository},

// We're de-declaring properties because of key order in package.json
...alwaysOnTop,
dependencies: sortKeys(dependencies),
devDependencies: sortKeys(devDependencies),
peerDependencies: sortKeys(peerDependencies),
/* eslint-enable no-dupe-keys */
engines: {
node: '>=14',
},
}

...repoFromOrigin(gitOrigin),
...urlsFromOrigin(gitOrigin),
const manifest: PackageJson = {
...forcedOrder,
// Use already configured values by default (if not otherwise specified)
...(prev || {}),
// We're de-declaring properties because of key order in package.json
...forcedOrder,
}

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

return {
homepage: `https://github.com/${details.user}/${details.repo}#readme`,
bugs: {
url: `https://github.com/${details.user}/${details.repo}/issues`,
},
homepage: `https://github.com/${details.user}/${details.repo}#readme`,
}
}

function repoFromOrigin(gitOrigin?: string) {
console.log(gitOrigin)
if (!gitOrigin) {
return {}
}
Expand Down Expand Up @@ -367,13 +365,13 @@ export async function addBuildScripts(manifest: PackageJson, options: InjectOpti
return false
}
return addPackageJsonScripts(manifest, options, (scripts) => {
scripts.clean = addScript(`rimraf lib`, scripts.clean)
scripts.lint = addScript(`eslint .`, scripts.lint)
scripts.prebuild = addScript('npm run clean && ' + expectedScripts.prebuild, scripts.prebuild)
scripts.build = addScript(expectedScripts.build, scripts.build)
scripts.watch = addScript(expectedScripts.watch, scripts.watch)
scripts.clean = addScript(`rimraf lib`, scripts.clean)
scripts['link-watch'] = addScript(expectedScripts['link-watch'], scripts['link-watch'])
scripts.lint = addScript(`eslint .`, scripts.lint)
scripts.prepublishOnly = addScript(expectedScripts.prepublishOnly, scripts.prepublishOnly)
scripts.watch = addScript(expectedScripts.watch, scripts.watch)
return scripts
})
}
Expand Down
41 changes: 13 additions & 28 deletions src/presets/semver-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import path from 'path'
import {readFile, writeFile} from '../util/files'
import {errorToUndefined} from '../util/errorToUndefined'
import {PackageJson} from '../actions/verify/types'
import {developTestSnippet, getLicenseText, installationSnippet} from '../util/readme'
import {getUserInfo} from '../util/user'

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

const {v3Banner, install, usage, developFooter} = await readmeSnippets(options)
const {v3Banner, install, usage, developTest, license, releaseSnippet} = await readmeSnippets(
options
)

const prependSections = missingSections(readme, [v3Banner, install, usage])
const appendSections = missingSections(readme, [developFooter])
const appendSections = missingSections(readme, [license, developTest, releaseSnippet])

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

async function readmeSnippets(options: InjectOptions) {
const pkg = await getPackage(options)
const user = await getUserInfo(options, pkg)

const bestEffortUrl = readmeBaseurl(pkg)

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

const install = outdent`
## Installation
\`\`\`
npm install --save ${pkg.name}@studio-v3
\`\`\`
or
\`\`\`
yarn add ${pkg.name}@studio-v3
\`\`\`
`
const install = installationSnippet(pkg.name ?? 'unknown')

const usage = outdent`
## Usage
`

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

const releaseSnippet = outdent`
### Release new version
Run ["CI & Release" workflow](${bestEffortUrl}/actions/workflows/main.yml).
Expand All @@ -141,7 +124,9 @@ async function readmeSnippets(options: InjectOptions) {
v3Banner,
install,
usage,
developFooter,
license,
developTest: developTestSnippet(),
releaseSnippet,
}
}

Expand Down
43 changes: 28 additions & 15 deletions src/util/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ export function generateReadme(data: PackageData) {
return outdent`
# ${pluginName}
## Installation
\`\`\`
npm install --save ${pluginName}
\`\`\`
or
\`\`\`
yarn add ${pluginName}
\`\`\`
${installationSnippet(pluginName ?? 'unknown')}
## Usage
Add it as a plugin in sanity.config.ts (or .js):
Expand All @@ -30,13 +20,36 @@ export function generateReadme(data: PackageData) {
import {myPlugin} from '${pluginName}'
export const defineConfig({
/...
//...
plugins: [
myPlugin({})
]
})
\`\`\`
${getLicenseText(license?.id, user?.name ? (user as User) : undefined)}
${developTestSnippet()}
`
}

export function installationSnippet(packageName: string) {
return outdent`
## Installation
\`\`\`
npm install --save ${packageName}@studio-v3
\`\`\`
`
}

export function developTestSnippet() {
return outdent`
## Develop & test
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
with default configuration for build & watch scripts.
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
on how to run this plugin with hotreload in the studio.
`
}

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

let licenseText = '## License\n'
if (licenseName && user?.name) {
licenseText = `${licenseText}\n${licenseName} © ${user?.name}\nSee LICENSE`
licenseText = `${licenseText}\n[${licenseName}](LICENSE) © ${user?.name}\n`
} else if (licenseName) {
licenseText = `${licenseText}\n${licenseName}\nSee LICENSE`
licenseText = `${licenseText}\n[${licenseName}](LICENSE)\n`
} else {
licenseText = `${licenseText}\nSee LICENSE`
licenseText = `${licenseText}\nSee [LICENSE](LICENSE)`
}

return licenseText
Expand Down

0 comments on commit 95e8cba

Please sign in to comment.