diff --git a/package.json b/package.json index 1da46fd..bfddca9 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,9 @@ "preview": "vite preview --open", "test": "linkinator ./S3 --config linkinator.config.json", "test:blog": "linkinator https://blog.hello.coop https://blog.hello.dev --config linkinator.config.json", - "review": "./scripts/review.mjs" + "pr": "./scripts/pr.sh", + "rebase": "git pull origin main --rebase", + "review": "npm run rebase && npm run test && npm run pr" }, "engines": { "node": "~18", diff --git a/scripts/pr.sh b/scripts/pr.sh new file mode 100755 index 0000000..9708e4f --- /dev/null +++ b/scripts/pr.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Function to check for errors +check_error() { + if [ -n "$1" ]; then + echo "Error: $1" + afplay /System/Library/Sounds/Funk.aiff + exit 1 + fi +} + +# Check if not in the main branch +prBranchName=$(git branch --show-current) +if [ "$prBranchName" = "main" ]; then + check_error "Cannot start a review from main branch. Please switch to your development branch and try again." +fi + +# Create PR +gh pr create --base main --title "$prBranchName" --body "" 2>&1 +pr_create_status=$? +if [ $pr_create_status -ne 0 ]; then + if [ "$(grep -q 'already exists' <<< "$pr_create_status"; echo $?)" -ne 0 ]; then + check_error "Error creating PR" + fi +fi + +# Play success sound (Only works on MacOS) +afplay /System/Library/Sounds/Glass.aiff + +# GitHub-Slack app sends a PR created Slack message to #content-updates channel +check_error + +# Exit with success +exit 0 diff --git a/scripts/pre-requisites.mjs b/scripts/pre-requisites.mjs deleted file mode 100644 index 5dc2d45..0000000 --- a/scripts/pre-requisites.mjs +++ /dev/null @@ -1,14 +0,0 @@ -import { execSync } from "child_process" - -const requiredPackages = ['gh', 'git'] - -for (const pkg of requiredPackages) { - try { - execSync(`which ${pkg}`, { stdio: 'ignore' }) - } catch (error) { - console.error(`${pkg} is not installed\n`) - process.exit(1) - } -} - -//TBD: Check version of requiredPackages + node and npm \ No newline at end of file diff --git a/scripts/pre-requisites.sh b/scripts/pre-requisites.sh new file mode 100644 index 0000000..b44397d --- /dev/null +++ b/scripts/pre-requisites.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +requiredPackages=("gh" "git") + +for pkg in "${requiredPackages[@]}"; do + if ! which "$pkg" &>/dev/null; then + echo "$pkg is not installed" + exit 1 + fi +done \ No newline at end of file diff --git a/scripts/review.mjs b/scripts/review.mjs deleted file mode 100755 index b745862..0000000 --- a/scripts/review.mjs +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env zx -try { - // $.verbose = false //do not log output of script run using zx - - //Is not in main branch - const prBranchName = (await $`git branch --show-current`).stdout.trim() - if(prBranchName === "main") - throw new Error("Cannot start a review from main branch.\nPlease switch to your development branch and try again.") - - //Get latest changes from main - await $`git pull origin main --rebase` - - //Run tests - await $`npm run test` - - //Create PR - let isExistingPR = false //for slack notification - try { - await $`gh pr create --base main --title ${prBranchName} --body ""` - } catch(p){ - if(!p.stderr.includes("already exists")) { //TBD: this is brittle to check for existing PR - throw p - } - //There is an existing PR. Do not die as author may have inteded to just update the PR. - isExistingPR = true - } - - //Play success sound to notify (Only works on MacOS) - await $`afplay /System/Library/Sounds/Glass.aiff` - - //GitHub-Slack app sends a PR created Slack message to #content-updates channel -} catch(p) { - if(p.stderr) { //Error thrown by zx - console.log(`Exit code: ${p.exitCode}`) - console.log(`Error: ${p.stderr}`) - } else { //Standard JS error obj - console.log(p) - } - //Play error sound to notify (Only works on MacOS) - await $`afplay /System/Library/Sounds/Funk.aiff` - process.exit(1) -} \ No newline at end of file