Skip to content

Commit

Permalink
feat: preset semantic-release now updates README.md when it exists (n…
Browse files Browse the repository at this point in the history
…aively)
  • Loading branch information
snorrees committed Oct 31, 2022
1 parent 9d030fb commit 0a33a25
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 25 deletions.
15 changes: 15 additions & 0 deletions docs/renovatebot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Preset: renovatebot

## Manual steps after inject

After injection, Renovate bot must be enabled for the repo on Github.

This can be done by adding the repo to Github Renovatebot app allow-list.

## What does it do?

Sets up the repo

* Adds Sanity dependabot preset dependency.
* Adds `renovate.json` to configure the above dependency for Renovatebot

40 changes: 16 additions & 24 deletions docs/presets.md → docs/semver-workflow.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# Presets
# Preset: semver-workflow

## semver-workflow
Add opinionated config and dependencies used by the Ecosystem team on Sanity to develop using
semantic-release driven workflow on Github for Sanity v3 plugins.

### Manual steps after inject
## Manual steps after inject

#### 1. Update `.github/workflows/main.yml` branches
### 1. Check README.md

The preset changes README.md in a naive manner.
Some text could be redundant or unnecessary depending on context and search for TODO.

Move text around until it looks good. Remember to change any v2 usage examples.

### 2. Update `.github/workflows/main.yml` branches
This differs from repo to repo.

In a typical plugin repo with a v2 and v3 version, it will typically look like this:
Expand All @@ -21,7 +29,7 @@ on:
branches: [main, v3]
```
#### 2. Update .releaserc.json
### 3. Update .releaserc.json
This differs from repo to repo.
In a typical plugin repo with a v2 and v3 version, it will typically look like this:
Expand All @@ -39,7 +47,7 @@ In a typical plugin repo with a v2 and v3 version, it will typically look like t
This assumes that the v2 version lives on `main` and the v3 versions livs on `v3`.
The v3 version will be a pre-release using `studio-v3` as npm tag, and `v3-studio` version suffix.

#### 3. Test workflow and remove `--dry-run`
## 4. Test workflow and remove `--dry-run`

The injected semantic-release command in `.github/workflows/main.yml` has `--dry-run` enabled.

Expand All @@ -51,27 +59,11 @@ If it is ok, remove the `--dry-run` flag from the workflow to perform a real rel
If the version is not what you expected, you might have to perform some
[troubleshooting](https://semantic-release.gitbook.io/semantic-release/support/troubleshooting).


### What does it do?
## What does it do?

Adds opinionated config and dependencies used by the Ecosystem team on Sanity to develop using
semantic-release driven workflow on Github.

* Adds husky and related files and dependencies to do pre-commit checks
* Adds semantic-release and preset dependencies to automate npm & Github releases

## renovate

### Manual steps after inject

After injection, Renovate bot must be enabled for the repo on Github.

This can be done by adding the repo to Github Renovatebot app allow-list.

### What does it do?

Sets up the repo

* Adds Sanity dependabot preset dependency.
* Adds `renovate.json` to configure the above dependency for Renovatebot

* Updates README.md with some standard texts
78 changes: 77 additions & 1 deletion src/presets/semver-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {FromTo, InjectOptions, writeAssets} from '../actions/inject'
import {resolveLatestVersions} from '../npm/resolveLatestVersions'
import {Preset} from './presets'
import {
addBuildScripts,
addPackageJsonScripts,
addScript,
getPackage,
Expand All @@ -12,6 +11,9 @@ import {
import log from '../util/log'
import outdent from 'outdent'
import chalk from 'chalk'
import path from 'path'
import {readFile, writeFile} from '../util/files'
import {errorToUndefined} from '../util/errorToUndefined'

export const semverWorkflowPreset: Preset = {
name: 'semver-workflow',
Expand All @@ -26,6 +28,7 @@ async function applyPreset(options: InjectOptions) {
await writeAssets(semverWorkflowFiles(), options)
await addPrepareScript(options)
await addDevDependencies(options)
await updateReadme(options)
}

async function addPrepareScript(options: InjectOptions) {
Expand Down Expand Up @@ -61,6 +64,79 @@ async function addDevDependencies(options: InjectOptions) {
)
}

async function updateReadme(options: InjectOptions) {
const {basePath} = options

const readmePath = path.join(basePath, 'README.md')
let readme = (await readFile(readmePath, 'utf8').catch(errorToUndefined)) ?? ''

if (readme) {
const {v3Banner, installUsage, developFooter} = await readmeSnippets(options)
readme = v3Banner + installUsage + readme + developFooter
await writeFile(readmePath, readme, {encoding: 'utf8'})
log.info('Updated README. Please review the changes.')
}
}

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

const v3Banner = outdent`
> **NOTE**
>
> This is the **Sanity Studio v3 version** of ${pkg.name}.
>
> For the v2 version, please refer to the [v2-branch](${pkg.repository?.url ?? 'TODO'}).
`

const installUsage = outdent`
## Installation
\`\`\`
npm install --save ${pkg.name}@studio-v3
\`\`\`
or
\`\`\`
yarn add ${pkg.name}@studio-v3
\`\`\`
## Usage
<TODO: Show usage here>
`

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.
### Release new version
Run ["CI & Release" workflow](${pkg.repository?.url ?? 'TODO'}/actions/workflows/main.yml).
Make sure to select the main branch and check "Release new version".
Semantic release will only release on configured branches, so it is safe to run release on any branch.
`

return {
v3Banner,
installUsage,
developFooter,
}
}

function semverWorkflowFiles(): FromTo[] {
return [
{from: ['.github', 'workflows', 'main.yml'], to: ['.github', 'workflows', 'main.yml']},
Expand Down

0 comments on commit 0a33a25

Please sign in to comment.