Skip to content

Commit

Permalink
chore(tools): setup custom versioning and changelog scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjavi committed Aug 22, 2023
1 parent 52fa946 commit 1d34b77
Show file tree
Hide file tree
Showing 17 changed files with 525 additions and 760 deletions.
8 changes: 0 additions & 8 deletions .changeset/README.md

This file was deleted.

27 changes: 0 additions & 27 deletions .changeset/commits.js

This file was deleted.

10 changes: 0 additions & 10 deletions .changeset/config.json

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/publish-releases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish Releases

env:
NODE_VERSION: '>=18.5.0'
PNPM_VERSION: 8.6

on:
workflow_dispatch:
push:
tags:
- 'v*.*.*'

concurrency:
group: publishReleases-${{ github.ref }}
cancel-in-progress: true

jobs:
publishReleases:
name: 'Publish releases'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: 'Setup project'
uses: ./.github/actions/setup-project

- name: Publish pending releases
env:
GH_TOKEN: ${{ github.token }}
run: |
pnpm exec changelogen gh release
2 changes: 1 addition & 1 deletion .github/workflows/update-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
branch_name="update-deps/$(date +'%Y%m%d-%H%M%S')"
git checkout -b $branch_name
git add -A
git commit -m "chore: update dependencies"
git commit -m "chore(deps): update to latest versions"
git push origin $branch_name
gh pr create --title "chore: update dependencies 🪄" --base main --head $branch_name --label "update-deps" \
--body "This PR updates all package dependencies to their latest version. Please review the changes and merge if everything looks good."
9 changes: 9 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm prettier-check
pnpm type-check
pnpm lint
pnpm publint
pnpm build
pnpm test:ci
10 changes: 10 additions & 0 deletions .scripts/bump-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

# Check if there are uncommitted changes
if [ -n "$(git status -s)" ]; then
echo "Error: There are uncommitted changes in the repository."
exit 1
fi

node .scripts/set-version.cjs "$(node .scripts/get-semver.cjs ${1:-"patch"})" || exit 1
node .scripts/match-versions.cjs || exit 1
28 changes: 28 additions & 0 deletions .scripts/get-semver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs')
const path = require('path')
const semver = require('semver')

// Read the root package.json to get the current version
const CWD = process.cwd()
const rootPackagePath = path.resolve(path.join(CWD, 'package.json'))
const rootPackageContent = fs.readFileSync(rootPackagePath, 'utf-8')
const rootPackage = JSON.parse(rootPackageContent)
const currentVersion = rootPackage.version || '0.0.0'

// Get the desired level from the command line argument (default to "patch")
const desiredLevel = process.argv[2] || 'patch'

if (desiredLevel === 'current') {
console.log(currentVersion)
process.exit(0)
}

// Calculate the next version based on the desired level
const nextVersion = semver.inc(currentVersion, desiredLevel)

if (!nextVersion) {
console.error('Invalid version level specified. Please use "patch", "minor", or "major".')
process.exit(1)
}

console.log(nextVersion)
52 changes: 52 additions & 0 deletions .scripts/match-versions.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs')
const path = require('path')

const workspaces = ['packages']

// Read the root package.json to get the desired version
const CWD = process.cwd()
const rootPackagePath = path.resolve(path.join(CWD, 'package.json'))
const rootPackageContent = fs.readFileSync(rootPackagePath, 'utf-8')
const rootPackage = JSON.parse(rootPackageContent)
const desiredVersion = rootPackage.version

if (!desiredVersion) {
rootPackage.version = '0.0.1'
fs.writeFileSync(rootPackagePath, JSON.stringify(rootPackage, null, 2) + '\n')
}

function updateWorkspacePackages(workspaceDir) {
// Get a list of package directories
const packagesDir = path.resolve(path.join(CWD, workspaceDir))
const packageDirs = fs
.readdirSync(packagesDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)

// Update versions in each package's package.json
packageDirs.forEach(packageName => {
const packagePath = path.join(packagesDir, packageName, 'package.json')
if (!fs.existsSync(packagePath)) {
console.log(`No package.json found in ${packageName}`)
return
}

const packageContent = fs.readFileSync(packagePath, 'utf-8')
const packageData = JSON.parse(packageContent)

if (packageData.version !== desiredVersion) {
packageData.version = desiredVersion
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2) + '\n')
console.log(`Updated version in ${packageName}/package.json`)
} else {
console.log(`${packageName}/package.json is already up to date`)
}
})
}

function main() {
workspaces.forEach(updateWorkspacePackages)
console.log('Version update process complete.')
}

main()
26 changes: 26 additions & 0 deletions .scripts/set-version.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs')
const path = require('path')
const semver = require('semver')

// Read the root package.json
const CWD = process.cwd()
const rootPackagePath = path.resolve(path.join(CWD, 'package.json'))
const rootPackageContent = fs.readFileSync(rootPackagePath, 'utf-8')
const rootPackage = JSON.parse(rootPackageContent)

// Get the version from the command line argument
const desiredVersion = process.argv[2]

// Check if the desired version is a valid semver
if (!semver.valid(desiredVersion)) {
console.error('Invalid version format. Please provide a valid semver version.')
process.exit(1)
}

// Set the desired version in the package.json
rootPackage.version = desiredVersion

// Write the updated package.json back to the file
fs.writeFileSync(rootPackagePath, JSON.stringify(rootPackage, null, 2) + '\n')

console.log(`Version updated to: ${desiredVersion}`)
23 changes: 23 additions & 0 deletions .scripts/tag-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env sh

# Check if there are changes in package.json & **/package.json
if git diff --quiet --exit-code -- package.json '**/package.json'; then
echo "Error: None of the package.json files have been changed."
exit 1
fi

CURRENT_VERSION=$(node .scripts/get-semver.cjs current)
# FIRST_TAG=$(git tag | sort -V | head -n 1)

echo "Current Version: ${CURRENT_VERSION}"

# Prepend content to CHANGELOG.md
existing_CHANGELOG=$(cat CHANGELOG.md)
new_CHANGELOG=$(pnpm exec changelogen -r "${CURRENT_VERSION}")

echo "${new_CHANGELOG}\n${existing_CHANGELOG}" > CHANGELOG.md

# Create a new commit and tag with the current version
git add CHANGELOG.md package.json **/package.json pnpm-lock.yaml
git commit -m "chore(release): bump version to v${CURRENT_VERSION}" || exit 1
git tag -a "v${CURRENT_VERSION}" -m "v${CURRENT_VERSION}" || exit 1
32 changes: 29 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,37 @@ npm install -g pnpm@8

## Developing

To get started, use the Quick Start guide found in the [README](README.md) to install dependencies
and start the dev server.

The different packages can be found in `packages/*`, and that's where you'll be mainly working.

### Quick Start

Here are the basic commands you'll need to get started:

```sh

# Install dependencies
pnpm install

# Start the dev server
pnpm dev

# Build dist files
pnpm build

# Run tests
pnpm test

# Lint
pnpm lint

# Format
pnpm format

# Type check
pnpm type-check

```

## Testing

We use `jest` to run tests. You can run all tests with:
Expand Down
24 changes: 23 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const fs = require('fs')

const folderNamesUnderPackages = fs
.readdirSync('./packages')
.filter(file => fs.statSync(`./packages/${file}`).isDirectory())

/**
* @type {import('@commitlint/types').UserConfig}
*/
Expand All @@ -18,12 +24,28 @@ module.exports = {
'fix',
'perf',
'refactor',
'release',
'revert',
'style',
'test',
],
],
'scope-empty': [2, 'never'],
'scope-enum': [
2,
'always',
[
'general',
'release',
'deps',
'config',
'setup',
'readme',
'tools',
'workflow',
// packages:
...folderNamesUnderPackages,
],
],
},
ignores: [commit => commit.includes('update deps')],
}
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "storylite-monorepo",
"version": "0.3.0",
"private": true,
"homepage": "https://github.com/itsjavi/storylite#readme",
"bugs": {
Expand All @@ -10,9 +10,6 @@
"author": "Javi Aguilar https://itsjavi.com",
"scripts": {
"build": "turbo build",
"changeset": "changeset",
"changeset.apply": "changeset version && pnpm i --no-frozen-lockfile && git add .",
"changeset.release": "pnpm build && pnpm publint && changeset publish",
"clear-cache": "rm -rf packages/*/.turbo && rm -rf packages/*/.next && jest --clearCache",
"dev": "turbo run build && turbo run dev --no-deps --no-cache --parallel --concurrency 20",
"format": "pnpm lint-fix && pnpm prettier-fix",
Expand All @@ -29,7 +26,15 @@
"test": "jest --passWithNoTests",
"test:ci": "jest --ci --passWithNoTests --coverage",
"test:coverage": "jest --passWithNoTests --coverage",
"type-check": "turbo type-check"
"type-check": "turbo type-check",
"version:bump": ".scripts/bump-version.sh patch && .scripts/tag-version.sh",
"version:bump-major": ".scripts/bump-version.sh major && .scripts/tag-version.sh",
"version:bump-minor": ".scripts/bump-version.sh minor && .scripts/tag-version.sh",
"version:bump-patch": "pnpm ws:version",
"version:publish": "pnpm run version:publish-gh-refs && pnpm run version:publish-npm",
"version:publish-gh-refs": "git push && git push --tags",
"version:publish-gh-releases": "pnpm exec changelogen gh release",
"version:publish-npm": "pnpm -r exec pnpm publish"
},
"lint-staged": {
"*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}": [
Expand All @@ -41,22 +46,23 @@
},
"prettier": "@r1stack/coding-style/prettier",
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@r1stack/coding-style": "^0.3.0",
"@r1stack/coding-style": "^0.3.7",
"@swc/core": "^1.3.78",
"@swc/jest": "^0.2.29",
"@testing-library/jest-dom": "^6.0.1",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.3",
"@types/node": "^20.5.1",
"changelogen": "^0.5.4",
"eslint": "^8.47.0",
"husky": "^8.0.3",
"jest": "^29.6.3",
"jest-environment-jsdom": "^29.6.3",
"lint-staged": "^14.0.1",
"prettier": "^3.0.2",
"semver": "^7.5.4",
"turbo": "^1.10.12",
"typescript": "^5.1.6"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/storylite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
"type-check": "tsc --noEmit"
},
"devDependencies": {
"@r1stack/coding-style": "^0.3.0",
"@r1stack/coding-style": "^0.3.7",
"@types/node": "^20.5.1",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"lucide-react": "^0.268.0",
"publint": "^0.2.1",
"publint": "^0.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.15.0",
Expand Down
Loading

0 comments on commit 1d34b77

Please sign in to comment.