Last active
March 22, 2023 21:11
-
-
Save josefaidt/cd031d1f4b1eb375b937de0dc958fe9a to your computer and use it in GitHub Desktop.
Amplify CLI command hook helper to auto-commit changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"include": ["./*.js", "./ambient.d.ts"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "module" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autogit } from './auto-git.js' | |
autogit(import.meta.url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is my fish script for "mkrep"