Skip to content

Commit

Permalink
feat(git): properly handle commits and tagging (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Soukup <soukup@u.plus>
  • Loading branch information
sladg and sladg committed Aug 30, 2022
1 parent 68f69fe commit c84725d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 69 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ And includes CLI and custom server handler to integrate with ApiGw.
- [Packaging](#packaging)
- [Server handler](#server-handler-1)
- [Static assets](#static-assets)
- [Versioning](#versioning)
- [Guess](#guess)
- [Shipit](#shipit)
- [TODO](#todo)
- [Disclaimer](#disclaimer)

Expand Down Expand Up @@ -280,6 +283,23 @@ Cloudfront paths used:
- `_next/*`
- `assets/*`

# Versioning

This package exposes two CLI functions intended to deal with versioning your application and releasing.

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.

## Guess

Simple CLI command that takes commit message and current version and outputs (stdout) next version based on keywords inside commit message.

## Shipit

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).

Simply call `@sladg/next-lambda shipit` on any branch and be done.


# TODO

- Explain scripts used for packaging Next app,
Expand Down
29 changes: 23 additions & 6 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import path from 'path'
import packageJson from '../package.json'
import { simpleGit } from 'simple-git'
import { Command } from 'commander'
import { bumpCalculator, bumpMapping, BumpType, isValidTag } from './utils'
import { bumpCalculator, bumpMapping, BumpType, isValidTag, replaceVersionInCommonFiles } from './utils'

const exec = util.promisify(child_exec)

const skipCiFlag = '[skip ci]'

const scriptDir = path.dirname(__filename)
const scriptPath = path.resolve(`${scriptDir}/../../scripts/pack-nextjs.sh`)
const handlerPath = path.resolve(`${scriptDir}/../server-handler/index.js`)
Expand Down Expand Up @@ -86,6 +88,7 @@ program

const latestCommit = log.latest?.hash
const latestTag = tags.latest ?? '0.0.0'
const currentTag = latestTag.replace(tagPrefix, '')

if (!isValidTag(latestTag, tagPrefix)) {
throw new Error(`Invalid tag found - ${latestTag}!`)
Expand Down Expand Up @@ -120,17 +123,31 @@ program
bumps.push(BumpType.Patch)
}

const nextTag = bumps.reduce((acc, curr) => bumpCalculator(acc, curr), latestTag.replace(tagPrefix, ''))
const nextTag = bumps.reduce((acc, curr) => bumpCalculator(acc, curr), currentTag)
const nextTagWithPrefix = tagPrefix + nextTag
const releaseBranch = `${releaseBranchPrefix}${nextTagWithPrefix}`

console.log(`Next version is - ${nextTagWithPrefix}!`)

const replacementResults = replaceVersionInCommonFiles(currentTag, nextTag)
console.log(`Replaced version in files.`, replacementResults)

// Commit changed files (versions) and create a release commit with skip ci flag.
await git
//
.add('./*')
.commit(`Release: ${nextTagWithPrefix} ${skipCiFlag}`)
.push(remote.name, branch.current)

// As current branch commit includes skip ci flag, we want to ommit this flag for release branch so pipeline can run (omitting infinite loop).
// So we are overwriting last commit message and pushing to release branch.
await git
//
.raw(`--message Release: ${nextTagWithPrefix}`, '--amend')
.push(remote.name, `${branch.current}:${releaseBranch}`)

// Create tag and push it to master.
// @Note: CI/CD should not be listening for tags in master, it should listen to release branch.
await git.addTag(nextTagWithPrefix)
await git.pushTags()
await git.push(remote.name, `${branch.current}:${releaseBranch}`)
await git.addTag(nextTagWithPrefix).pushTags()

console.log(`Successfuly tagged and created new branch - ${releaseBranch}`)
})
Expand Down
39 changes: 39 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { IncomingMessage, ServerResponse } from 'http'
import { replaceInFileSync } from 'replace-in-file'
import { NextUrlWithParsedQuery } from 'next/dist/server/request-meta'
import { Readable } from 'stream'

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

throw new Error(`Unknown bump type - ${bumpType}!`)
}

export const replaceVersionInCommonFiles = (oldVersion: string, newVersion: string) => {
const results = replaceInFileSync({
allowEmptyPaths: true,
ignore: [
'**/node_modules',
'**/.venv',
'**/vendor',
'**/.git',
//
],
files: [
'package.json',
'package-lock.json',
'package-lock.json', // duplicate because lock file contains two occurences.
// 'yarn.lock', Yarn3 lock file does not contain version from package.json
'composer.json',
// 'composer.lock', Composer2 lock file does not include version from composer.json
'pyproject.toml',
'**/__init__.py',
],
from: [
`"version": "${oldVersion}"`, // npm/php style
`"version":"${oldVersion}"`, // uglified npm/php style
`version = "${oldVersion}"`, // python style
`__version__ = '${oldVersion}'`, // python style
],
to: [
`"version": "${newVersion}"`,
`"version":"${newVersion}"`,
`version = "${newVersion}"`,
`__version__ = '${newVersion}'`,
//
],
})

return results
}
Loading

0 comments on commit c84725d

Please sign in to comment.