From 95e8cba76582474271734f5e9259d9e5ce85acda Mon Sep 17 00:00:00 2001 From: Snorre Eskeland Brekke Date: Mon, 14 Nov 2022 15:18:50 +0100 Subject: [PATCH] fix: semver-workflow readme is now compatible with init --- src/actions/verify/types.ts | 1 + src/configs/default-source.ts | 4 +-- src/npm/package.ts | 50 ++++++++++++++++------------------ src/presets/semver-workflow.ts | 41 +++++++++------------------- src/util/readme.ts | 43 +++++++++++++++++++---------- 5 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/actions/verify/types.ts b/src/actions/verify/types.ts index 7c757471..b6cc7384 100644 --- a/src/actions/verify/types.ts +++ b/src/actions/verify/types.ts @@ -9,6 +9,7 @@ export interface PackageJson { version?: string description?: string author?: string + license?: string source?: string exports?: { [index: string]: Record | string | undefined diff --git a/src/configs/default-source.ts b/src/configs/default-source.ts index dc11da9f..4810ad60 100644 --- a/src/configs/default-source.ts +++ b/src/configs/default-source.ts @@ -14,7 +14,7 @@ export function defaultSourceJs(pkg: PackageJson) { * import {myPlugin} from '${pkg.name}' * * export const defineConfig({ - * /... + * //... * plugins: [ * myPlugin({}) * ] @@ -49,7 +49,7 @@ export function defaultSourceTs(pkg: PackageJson) { * import {myPlugin} from '${pkg.name}' * * export const defineConfig({ - * /... + * //... * plugins: [ * myPlugin() * ] diff --git a/src/npm/package.ts b/src/npm/package.ts index 18c61962..200f5018 100644 --- a/src/npm/package.ts +++ b/src/npm/package.ts @@ -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'] @@ -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'} : {}), @@ -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 @@ -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 {} } @@ -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 }) } diff --git a/src/presets/semver-workflow.ts b/src/presets/semver-workflow.ts index 8a002609..8ba299a9 100644 --- a/src/presets/semver-workflow.ts +++ b/src/presets/semver-workflow.ts @@ -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', @@ -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] @@ -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) @@ -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). @@ -141,7 +124,9 @@ async function readmeSnippets(options: InjectOptions) { v3Banner, install, usage, - developFooter, + license, + developTest: developTestSnippet(), + releaseSnippet, } } diff --git a/src/util/readme.ts b/src/util/readme.ts index 15699df8..2ca5f1f3 100644 --- a/src/util/readme.ts +++ b/src/util/readme.ts @@ -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): @@ -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. ` } @@ -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