Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit c84725d

Browse files
committed
feat(git): properly handle commits and tagging (#19)
Co-authored-by: Jan Soukup <[email protected]>
1 parent 68f69fe commit c84725d

File tree

5 files changed

+175
-69
lines changed

5 files changed

+175
-69
lines changed

Diff for: README.md

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ And includes CLI and custom server handler to integrate with ApiGw.
1414
- [Packaging](#packaging)
1515
- [Server handler](#server-handler-1)
1616
- [Static assets](#static-assets)
17+
- [Versioning](#versioning)
18+
- [Guess](#guess)
19+
- [Shipit](#shipit)
1720
- [TODO](#todo)
1821
- [Disclaimer](#disclaimer)
1922

@@ -280,6 +283,23 @@ Cloudfront paths used:
280283
- `_next/*`
281284
- `assets/*`
282285

286+
# Versioning
287+
288+
This package exposes two CLI functions intended to deal with versioning your application and releasing.
289+
290+
Motivation behind is to get rid of huge dependencies and over-kill implementations such as @auto-it, release-it or semver. Those are bulky and unncessarily complex.
291+
292+
## Guess
293+
294+
Simple CLI command that takes commit message and current version and outputs (stdout) next version based on keywords inside commit message.
295+
296+
## Shipit
297+
298+
Similar to guess command, however, it automatically tags a commit on current branch and creates release branch for you so hooking up pipelines is as simple as it can be. Version is automatically bumped in common NPM and PHP files (package.json, package-lock.json and composer.json).
299+
300+
Simply call `@sladg/next-lambda shipit` on any branch and be done.
301+
302+
283303
# TODO
284304

285305
- Explain scripts used for packaging Next app,

Diff for: lib/cli.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import path from 'path'
55
import packageJson from '../package.json'
66
import { simpleGit } from 'simple-git'
77
import { Command } from 'commander'
8-
import { bumpCalculator, bumpMapping, BumpType, isValidTag } from './utils'
8+
import { bumpCalculator, bumpMapping, BumpType, isValidTag, replaceVersionInCommonFiles } from './utils'
99

1010
const exec = util.promisify(child_exec)
1111

12+
const skipCiFlag = '[skip ci]'
13+
1214
const scriptDir = path.dirname(__filename)
1315
const scriptPath = path.resolve(`${scriptDir}/../../scripts/pack-nextjs.sh`)
1416
const handlerPath = path.resolve(`${scriptDir}/../server-handler/index.js`)
@@ -86,6 +88,7 @@ program
8688

8789
const latestCommit = log.latest?.hash
8890
const latestTag = tags.latest ?? '0.0.0'
91+
const currentTag = latestTag.replace(tagPrefix, '')
8992

9093
if (!isValidTag(latestTag, tagPrefix)) {
9194
throw new Error(`Invalid tag found - ${latestTag}!`)
@@ -120,17 +123,31 @@ program
120123
bumps.push(BumpType.Patch)
121124
}
122125

123-
const nextTag = bumps.reduce((acc, curr) => bumpCalculator(acc, curr), latestTag.replace(tagPrefix, ''))
126+
const nextTag = bumps.reduce((acc, curr) => bumpCalculator(acc, curr), currentTag)
124127
const nextTagWithPrefix = tagPrefix + nextTag
125128
const releaseBranch = `${releaseBranchPrefix}${nextTagWithPrefix}`
126-
127129
console.log(`Next version is - ${nextTagWithPrefix}!`)
128130

131+
const replacementResults = replaceVersionInCommonFiles(currentTag, nextTag)
132+
console.log(`Replaced version in files.`, replacementResults)
133+
134+
// Commit changed files (versions) and create a release commit with skip ci flag.
135+
await git
136+
//
137+
.add('./*')
138+
.commit(`Release: ${nextTagWithPrefix} ${skipCiFlag}`)
139+
.push(remote.name, branch.current)
140+
141+
// As current branch commit includes skip ci flag, we want to ommit this flag for release branch so pipeline can run (omitting infinite loop).
142+
// So we are overwriting last commit message and pushing to release branch.
143+
await git
144+
//
145+
.raw(`--message Release: ${nextTagWithPrefix}`, '--amend')
146+
.push(remote.name, `${branch.current}:${releaseBranch}`)
147+
129148
// Create tag and push it to master.
130149
// @Note: CI/CD should not be listening for tags in master, it should listen to release branch.
131-
await git.addTag(nextTagWithPrefix)
132-
await git.pushTags()
133-
await git.push(remote.name, `${branch.current}:${releaseBranch}`)
150+
await git.addTag(nextTagWithPrefix).pushTags()
134151

135152
console.log(`Successfuly tagged and created new branch - ${releaseBranch}`)
136153
})

Diff for: lib/utils.ts

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'
22
import { IncomingMessage, ServerResponse } from 'http'
3+
import { replaceInFileSync } from 'replace-in-file'
34
import { NextUrlWithParsedQuery } from 'next/dist/server/request-meta'
45
import { Readable } from 'stream'
56

@@ -143,3 +144,41 @@ export const bumpCalculator = (version: string, bumpType: BumpType) => {
143144

144145
throw new Error(`Unknown bump type - ${bumpType}!`)
145146
}
147+
148+
export const replaceVersionInCommonFiles = (oldVersion: string, newVersion: string) => {
149+
const results = replaceInFileSync({
150+
allowEmptyPaths: true,
151+
ignore: [
152+
'**/node_modules',
153+
'**/.venv',
154+
'**/vendor',
155+
'**/.git',
156+
//
157+
],
158+
files: [
159+
'package.json',
160+
'package-lock.json',
161+
'package-lock.json', // duplicate because lock file contains two occurences.
162+
// 'yarn.lock', Yarn3 lock file does not contain version from package.json
163+
'composer.json',
164+
// 'composer.lock', Composer2 lock file does not include version from composer.json
165+
'pyproject.toml',
166+
'**/__init__.py',
167+
],
168+
from: [
169+
`"version": "${oldVersion}"`, // npm/php style
170+
`"version":"${oldVersion}"`, // uglified npm/php style
171+
`version = "${oldVersion}"`, // python style
172+
`__version__ = '${oldVersion}'`, // python style
173+
],
174+
to: [
175+
`"version": "${newVersion}"`,
176+
`"version":"${newVersion}"`,
177+
`version = "${newVersion}"`,
178+
`__version__ = '${newVersion}'`,
179+
//
180+
],
181+
})
182+
183+
return results
184+
}

0 commit comments

Comments
 (0)