Skip to content

Upgrade Changesets CLI #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"changelog": "../scripts/changeset-changelog-generator.js",
"commit": false,
"linked": [["@contentlayer/*", "contentlayer", "next-contentlayer"]],
"fixed": [["@contentlayer/*", "contentlayer", "next-contentlayer"]],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
Expand Down
48 changes: 48 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-fix-csb-deps.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = {
name: 'fix-csb-deps',
factory: (require) => {
const crypto = require('crypto')

// https://github.com/yarnpkg/berry/blob/5411f76bcd89d1d6f430f4bd0e16146ce9fdd370/packages/yarnpkg-core/sources/hashUtils.ts#L5-L26
function makeHash(...args) {
const hash = crypto.createHash(`sha512`)

let acc = ``
for (const arg of args) {
if (typeof arg === `string`) {
acc += arg
} else if (arg) {
if (acc) {
hash.update(acc)
acc = ``
}

hash.update(arg)
}
}

if (acc) hash.update(acc)

return hash.digest(`hex`)
}

return {
hooks: {
reduceDependency(dependency, project, locator, initialDependency, { resolver, resolveOptions }) {
if (dependency.range.startsWith('https://pkg.csb.dev/') && !dependency.range.endsWith('/_pkg.tgz')) {
const newRange = `${dependency.range}/_pkg.tgz`
const fixedDescriptor = {
identHash: dependency.identHash,
scope: dependency.scope,
name: dependency.name,
descriptorHash: makeHash(dependency.identHash, newRange),
range: newRange,
}
return fixedDescriptor
}
return dependency
},
},
}
},
}
2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ plugins:
spec: '@yarnpkg/plugin-workspace-tools'
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: '@yarnpkg/plugin-version'
- path: .yarn/plugins/@yarnpkg/plugin-fix-csb-deps.cjs
spec: .yarn/plugins/@yarnpkg/plugin-fix-csb-deps.cjs

yarnPath: .yarn/releases/yarn-3.0.2.cjs

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Contentlayer
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"lint:eslint:fix": "eslint packages --ext .ts --fix",
"lint:eslint:check": "eslint packages --ext .ts --max-warnings=0",
"lint:prettier:fix": "prettier packages --write",
"lint:prettier:check": "prettier packages --check"
"lint:prettier:check": "prettier packages --check",
"changeset": "node ./scripts/changeset.js"
},
"devDependencies": {
"@changesets/cli": "^2.17.0",
"@changesets/cli": "https://pkg.csb.dev/changesets/changesets/commit/f65e0a7a/@changesets/cli/_pkg.tgz",
"@effect-ts/tracing-plugin": "^0.14.21",
"@playwright/test": "^1.15.1",
"@types/prettier": "^2.3.2",
Expand Down
8 changes: 8 additions & 0 deletions scripts/changeset-changelog-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const cliChangelogGenerator = require('@changesets/cli/changelog').default

module.exports = {
default: {
getDependencyReleaseLine: () => '',
getReleaseLine: cliChangelogGenerator.getReleaseLine,
},
}
74 changes: 74 additions & 0 deletions scripts/changeset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node
'use strict'
const { spawnSync } = require('child_process')
const fs = require('fs')
const path = require('path')

const [command, ...flags] = process.argv.slice(2)

if (command) {
const changesetResult = spawnSync(
process.argv[0],
[path.join(__dirname, '..', 'node_modules', '.bin', 'changeset'), command, ...flags],
{ stdio: 'inherit' },
)

if (changesetResult.status !== 0) {
process.exit(changesetResult.status)
}

if (command === 'version') {
const untrackedFilesResult = spawnSync('git', ['ls-files', '--others', '--exclude-standard'])

if (untrackedFilesResult.status !== 0) {
process.exit(untrackedFilesResult.status)
}

const untrackedFiles = untrackedFilesResult.stdout.toString().trim().split('\n')

for (const file of untrackedFiles) {
const filePath = path.join(__dirname, '..', file)
if (!filePath.endsWith('/CHANGELOG.md')) {
continue
}
if (filePath.endsWith('/@contentlayer/core/CHANGELOG.md')) {
const changelog = fs.readFileSync(filePath, 'utf8')
const versionHeader = /^## \d+\.\d+\.\d+/gm

// we leverage statefulness of `g` regex here for the second `exec` to start of the `lastIndex` from the first match
const start = versionHeader.exec(changelog).index
// can't use optional chaining for now because GitHub Actions run on node12, see https://github.com/actions/github-script/pull/182#issuecomment-903966153
const endExecResult = versionHeader.exec(changelog)
const end = endExecResult ? endExecResult.index : changelog.length

const latestChangelogEntry = changelog.slice(start, end)
const [match, version] = latestChangelogEntry.match(/## (.+)\s*$/m)

const content = latestChangelogEntry.slice(match.length).trim()

if (content) {
const rootChangelogPath = path.join(__dirname, '..', 'CHANGELOG.md')
if (fs.existsSync(rootChangelogPath)) {
const rootChangelog = fs.readFileSync(rootChangelogPath, 'utf8')

fs.writeFileSync(rootChangelogPath, rootChangelog.replace('\n', `\n\n## ${version}\n\n${content}\n`))
} else {
fs.writeFileSync(rootChangelogPath, `# contentlayer\n\n## ${version}\n\n${content}\n`)
}
}
}
fs.unlinkSync(filePath)
}
}
} else {
const changesetName = new Date()
.toISOString()
.replace(/\.\d{3}Z$/, '')
.replace(/[^\d]/g, '')
.toString()

fs.writeFileSync(
path.join(__dirname, '..', '.changeset', `${changesetName}.md`),
`---\n'@contentlayer/core': patch\n---\n`,
)
}
Loading