-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add script to make versioning and publishing prereleases easier (
#2206) * chore: add prerelease script to make creating prereleases easier * chore: improve prerelease script and add ignore field on changeset config * docs: improve documentation around prereleases * docs: add note about prereleases being only for new packages
- Loading branch information
Showing
5 changed files
with
292 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import inquirer from 'inquirer' | ||
import semver from 'semver' | ||
import { getPackages } from '@manypkg/get-packages' | ||
import { exec } from 'child_process' | ||
|
||
const cwd = process.cwd() | ||
|
||
async function updatePackageJson(pkgJsonPath, version) { | ||
const pkgRaw = await fs.readFile(pkgJsonPath, { encoding: 'utf-8' }) | ||
const stringified = pkgRaw.replace(/("version".*?) (".*?")/i, `$1 "${version}"`) | ||
await fs.writeFile(pkgJsonPath, stringified) | ||
} | ||
|
||
async function ignorePackage(pkgName) { | ||
const changesetConfigPath = path.resolve(cwd, '.changeset/config.json') | ||
const rawConfig = await fs.readFile(changesetConfigPath, { encoding: 'utf-8' }) | ||
const jsonConfig = JSON.parse(rawConfig) | ||
const ignorePkgs = jsonConfig.ignore || [] | ||
if(!ignorePkgs.includes(pkgName)) { | ||
jsonConfig.ignore = [pkgName, ...ignorePkgs] | ||
const stringified = JSON.stringify(jsonConfig, null, 2) | ||
await fs.writeFile(changesetConfigPath, stringified, { encoding: 'utf-8' }) | ||
} | ||
} | ||
|
||
async function main() { | ||
const { packages } = await getPackages(cwd) | ||
const choices = packages.map(({ packageJson }) => ({ | ||
name: `${packageJson.name} (${packageJson.version})`, | ||
value: packageJson.name | ||
})).concat(new inquirer.Separator()) | ||
|
||
const { pkgName } = await inquirer.prompt([{ | ||
pageSize: 12, | ||
name: 'pkgName', | ||
message: 'Which package to make a pre-release?', | ||
type: 'list', | ||
choices | ||
}]) | ||
|
||
const { packageJson, dir } = packages.find(({ packageJson }) => packageJson.name === pkgName) | ||
const { version, name } = packageJson | ||
const prereleaseTag = semver.prerelease(version)?.[0] | ||
|
||
const { tag, publish } = await inquirer.prompt([{ | ||
name: 'tag', | ||
message: 'Which tag should be used for the pre-release?', | ||
type: 'list', | ||
choices: [{ | ||
name: 'alpha', | ||
}, { | ||
name: 'beta' | ||
}], | ||
default: prereleaseTag | ||
}, { | ||
name: 'publish', | ||
message: 'Should the package be published?', | ||
type: 'confirm' | ||
}]) | ||
|
||
const increase = prereleaseTag === tag ? 'prerelease' : 'preminor' | ||
const newVersion = semver.inc(version, increase, tag) | ||
await updatePackageJson(path.resolve(dir, 'package.json'), newVersion) | ||
|
||
// Avoid chageset publishing it, by adding the package to the ignore list | ||
await ignorePackage(name) | ||
|
||
if(publish) { | ||
// TODO: remove dry-run, kept here for testing before merging | ||
await exec(`npm publish ${dir} --tag ${tag} --dry-run`, (error, stdout, stderr) => { | ||
if(!error) { | ||
console.log(stdout) | ||
console.log(`${name}@${newVersion} published: \nhttps://www.npmjs.com/package/${name}\n`) | ||
} else { | ||
console.error(error) | ||
console.error(stderr) | ||
} | ||
}) | ||
} else { | ||
console.log(`Version for ${name} updated on package.json.`) | ||
} | ||
} | ||
|
||
main() |
Oops, something went wrong.
c75ecd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
forma-36 – ./
v4.f36.contentful.com
f36.contentful.com
forma-36.vercel.app
forma-36-git-main.colorfuldemo.com
forma-36.colorfuldemo.com