Skip to content

Instantly share code, notes, and snippets.

@josefaidt
Last active March 22, 2023 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefaidt/cd031d1f4b1eb375b937de0dc958fe9a to your computer and use it in GitHub Desktop.
Save josefaidt/cd031d1f4b1eb375b937de0dc958fe9a to your computer and use it in GitHub Desktop.
Amplify CLI command hook helper to auto-commit changes
type HookData = {
amplify: {
environment: {
envName: string
projectPath: string
defaultEditor: string
}
command: string
subCommand: string
argv: string[]
}
}
type HookError = {
message: string
stack: string
}
type HookEvent = {
data: HookData
error: HookError
}
type HookStage = 'pre' | 'post'
type HookContext = {
url: string
}
import * as fs from 'node:fs'
import * as path from 'node:path'
import { exec } from 'node:child_process'
function run(command) {
return new Promise((resolve, reject) => {
const child = exec(command, (error, stdout, stderr) => {
if (error) {
reject(new Error(error, { cause: stdout }))
} else {
resolve({ stdout, stderr })
}
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
})
}
/**
* Get the lifecycle stage from the filename
* @returns {HookStage}
*/
function getLifecycleStage(lifecycleEventFileURL = import.meta.url) {
const url = new URL(lifecycleEventFileURL)
const filename = path.basename(url.pathname, '.js')
const [stage] = filename.split('-')
return stage
}
/**
* @returns {HookData}
*/
function getParameters() {
return JSON.parse(fs.readFileSync(0, { encoding: 'utf8' }))
}
/**
* @param {HookEvent} event
* @param {HookContext} context
*/
async function handler(event, context) {
const { data, error } = event
if (error) {
console.error(error)
process.exit(1)
}
const { command, environment } = data.amplify
const stage = getLifecycleStage(context.url)
const message = `${stage} ${command} ${environment.envName}`
try {
await run(`git add .; git commit -m "${message}"`)
} catch (error) {
if (error.cause.includes('nothing to commit')) {
console.log('[autogit] Skipping, nothing to commit')
} else {
console.error(error)
process.exit(1)
}
}
}
export function autogit(lifecycleEventFileURL) {
const event = getParameters()
handler(event, { url: lifecycleEventFileURL })
}
{
"include": ["./*.js", "./ambient.d.ts"]
}
{
"type": "module"
}
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
import { autogit } from './auto-git.js'
autogit(import.meta.url)
@josefaidt
Copy link
Author

gh gist clone git@gist.github.com:cd031d1f4b1eb375b937de0dc958fe9a.git amplify/hooks

@josefaidt
Copy link
Author

here is my fish script for "mkrep"

function mkrep
    set repro ~/Documents/projects/aws-amplify/reproductions/$argv[1]
    mkdir $repro
    cd $repro
    code $repro
    # setup new amplify project
    amplify init -y
    # pull in the auto-git hooks
    rm -rf amplify/hooks
    gh gist clone git@gist.github.com:cd031d1f4b1eb375b937de0dc958fe9a.git amplify/hooks
    # set up git
    git init
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment