Skip to content

Commit

Permalink
ci: update release process, remove complex code
Browse files Browse the repository at this point in the history
  • Loading branch information
Avivbens committed Jun 6, 2024
1 parent 7cba32f commit b444351
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: ['.eslintrc.js', 'project-scripts/pack.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
40 changes: 0 additions & 40 deletions .github/workflows/release-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,7 @@ jobs:
- name: 🖥️ Setup Env
uses: ./.github/workflows/install

# -------------------------
# Check next version
# -------------------------

- name: 🆚 Extract Next Release Version
id: extract-version
run: |
npx semantic-release --no-ci --dry-run --plugins @semantic-release/commit-analyzer,@semantic-release/release-notes-generator > output.txt
NEXT_VERSION=$(cat output.txt \
| grep "The next release version is" \
| node -e "console.log(require('fs').readFileSync(0, 'utf-8').match(/The next release version is (\d+\.\d+\.\d+(-beta\.\d+)?)/)?.[1] ?? '')")
echo "version=$NEXT_VERSION" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

# -------------------------
# Bail out if no version
# -------------------------

- name: 🔨 Build Package
if: steps.extract-version.outputs.version != ''
run: |
# update package.json version - for packing purposes
node -e "const fs = require('fs'); \
const pkg = require('./package.json'); \
pkg.version = '${{ steps.extract-version.outputs.version }}'; \
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 4));"
npm run pack
- name: 🤐 Archive pack file
if: steps.extract-version.outputs.version != ''
uses: thedoctor0/zip-release@master
with:
path: ./bin/*
filename: pack.zip

- name: 🚀 Release
if: steps.extract-version.outputs.version != ''
env:
# This is required to make semantic-release work with GitHub Actions
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
Expand Down
10 changes: 8 additions & 2 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"changelogFile": "CHANGELOG.md"
}
],
[
"@semantic-release/exec",
{
"prepareCmd": "npm run pack ${nextRelease.version}"
}
],
[
"@semantic-release/git",
{
Expand All @@ -35,12 +41,12 @@
{
"assets": [
{
"path": "./pack.zip",
"path": "dist/cli.zip",
"label": "cli-v${nextRelease.version}.zip",
"name": "cli-v${nextRelease.version}.zip"
}
]
}
]
]
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"homepage": "https://github.com/Avivbens/shell-config#readme",
"scripts": {
"build": "nest build",
"pack": "./project-scripts/pack.sh",
"pack": "./project-scripts/pack.js",
"pack-local": "./project-scripts/pack-local.sh",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint",
Expand Down Expand Up @@ -138,4 +138,4 @@
"^@commands/(.*)$": "<rootDir>/commands/$1"
}
}
}
}
2 changes: 1 addition & 1 deletion project-scripts/pack-local.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
npm run pack

cp -f ./bin/cli-* ./executable/shell-config
cp -f ./dist/cli-v* ./executable/shell-config
133 changes: 133 additions & 0 deletions project-scripts/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env node

const { cwd } = require('node:process')
const { resolve } = require('node:path')
const { exec } = require('node:child_process')
const { rm, readFile, writeFile } = require('node:fs/promises')
const { promisify } = require('node:util')

const execPromise = promisify(exec)

const DIRECTORIES_TO_DELETE = ['dist', 'bin']
const PACKAGE_JSON_FILE = 'package.json'
const PACKAGE_JSON_LOCK_FILE = 'package-lock.json'

const DEFAULT_OPTIONS = { lockfile: false }

function isCI() {
return (
process.env.CI === 'true' ||
process.env.GITHUB_ACTIONS === 'true' ||
process.env.GITLAB_CI === 'true' ||
process.env.CIRCLECI === 'true' ||
process.env.TF_BUILD === 'true' ||
process.env.APPVEYOR === 'True'
)
}

async function readPrettierConfig() {
try {
const filePath = resolve(cwd(), '.prettierrc')

const rawData = await readFile(filePath, 'utf-8')
const jsonData = JSON.parse(rawData)

return jsonData
} catch (error) {
console.error(`Error reading prettier config: ${error.stack}`)
throw error
}
}

async function readWorkflowPackageJson({ lockfile = false } = DEFAULT_OPTIONS) {
try {
const filePath = resolve(cwd(), lockfile ? PACKAGE_JSON_LOCK_FILE : PACKAGE_JSON_FILE)

const rawData = await readFile(filePath, 'utf-8')
const jsonData = JSON.parse(rawData)

return jsonData
} catch (error) {
console.error(`Error reading workflow package.json: ${error.stack}`)
throw error
}
}

async function writeWorkflowPackageJson(
data,
{ lockfile = false } = DEFAULT_OPTIONS,
) {
try {
const { tabWidth = 2 } = await readPrettierConfig()

const filePath = resolve(cwd(), lockfile ? PACKAGE_JSON_LOCK_FILE : PACKAGE_JSON_FILE)

const dataToWrite = JSON.stringify(data, null, tabWidth)

await writeFile(filePath, dataToWrite)
} catch (error) {
console.error(`Error writing workflow package.json: ${error.stack}`)
throw error
}
}

; (async () => {
try {

let [targetVersion] = process.argv.slice(2)
if (!targetVersion) {
if (isCI()) {
console.error('Please provide a version number!')
process.exit(1)
}

console.warn('No version number provided, using 1.0.0')
targetVersion = '1.0.0'
}

/**
* Clean up
* */
console.log('Cleaning up...')
const deletePrm = DIRECTORIES_TO_DELETE.map(dir => rm(dir, { recursive: true, force: true }))
await Promise.all(deletePrm)

/**
* Bump up versions
*/
if (isCI()) {
console.log('Bumping up versions...')

const packageJson = await readWorkflowPackageJson()
packageJson.version = targetVersion
await writeWorkflowPackageJson(packageJson)

const packageLockJson = await readWorkflowPackageJson({ lockfile: true })
packageLockJson.version = targetVersion
await writeWorkflowPackageJson(packageLockJson, { lockfile: true })
} else {
console.warn('Skipping version bumping because it is not running in CI')
}

/**
* Build
*/
console.log('Building...')
await execPromise('npm run build')

/**
* Pack
*/
console.log('Packing...')
await execPromise(`pkg . --output "dist/cli-v${targetVersion}"`)

/**
* ZIP
*/
console.log('Zipping...')
// await execPromise(`zip -9 dist/cli.zip dist/cli-v${targetVersion}`)
await execPromise(`cd dist && zip -9 -r "cli.zip" ./cli-v${targetVersion}`)
} catch (error) {
console.error(`Error: ${error.stack}`)
}
})()
8 changes: 0 additions & 8 deletions project-scripts/pack.sh

This file was deleted.

0 comments on commit b444351

Please sign in to comment.