From d5f23cd49f05bc5d0965f5effa649048087d77ba Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Sun, 22 Sep 2024 20:21:25 +0200 Subject: [PATCH] Updated setup generator with a pre-commit script --- turbo/generators/templates/setup/generator.ts | 80 ++++++++++++++----- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/turbo/generators/templates/setup/generator.ts b/turbo/generators/templates/setup/generator.ts index 0922fe55d..4d9327a9b 100644 --- a/turbo/generators/templates/setup/generator.ts +++ b/turbo/generators/templates/setup/generator.ts @@ -1,5 +1,5 @@ -import type { PlopTypes } from '@turbo/gen'; -import { execSync } from 'node:child_process'; +import type {PlopTypes} from '@turbo/gen'; +import {execSync} from 'node:child_process'; export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) { plop.setGenerator('setup', { @@ -10,6 +10,12 @@ export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) { name: 'projectName', message: 'What is the name of the project?', }, + { + type: 'confirm', + name: 'setupHealthCheck', + message: 'Do you want to setup a pre-commit hook for health checks?', + default: false, + } ], actions: [ { @@ -24,9 +30,10 @@ export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) { return JSON.stringify(pkg, null, 2); }, }, - async () => { + async (answers: any) => { try { setupRemote(); + setupPreCommit({setupHealthCheck: answers.setupHealthCheck}); return 'Project setup complete'; } catch (error) { @@ -38,26 +45,59 @@ export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) { }); } +function setupPreCommit(params: { + setupHealthCheck: boolean; +}) { + try { + const filePath = '.git/hooks/pre-commit'; + + const healthCheckCommands = params.setupHealthCheck + ? `pnpm run lint:fix\npnpm run typecheck\n`.trim() + : ``; + + const licenseCommand = `pnpm run --filter license dev`; + const fileContent = `#!/bin/bash\n${healthCheckCommands}${licenseCommand}`; + + // write file + execSync(`echo "${fileContent}" > ${filePath}`, { + stdio: 'inherit', + }); + + // make file executable + execSync(`chmod +x ${filePath}`, { + stdio: 'inherit', + }); + } catch (error) { + console.error('Pre-commit hook setup failed. Aborting package generation.'); + process.exit(1); + } +} + function setupRemote() { - // Setup remote upstream - const getRemoteUrl = execSync('git remote get-url origin', { - stdio: 'inherit', - }); - - const currentRemote = getRemoteUrl.toString().trim(); - - console.log(`Setting upstream remote to ${currentRemote} ...`); - - if (currentRemote && currentRemote.includes('github.com')) { - execSync(`git remote delete origin`, { + try { + // Setup remote upstream + const getRemoteUrl = execSync('git remote get-url origin', { stdio: 'inherit', }); - execSync(`git remote set-url upstream ${currentRemote}`, { - stdio: 'inherit', - }); - } else { - console.error('Your current remote is not GitHub'); + const currentRemote = getRemoteUrl.toString().trim(); + + console.log(`Setting upstream remote to ${currentRemote} ...`); + + if (currentRemote && currentRemote.includes('github.com')) { + + execSync(`git remote remove origin`, { + stdio: 'inherit', + }); + + execSync(`git remote set-url upstream ${currentRemote}`, { + stdio: 'inherit', + }); + } else { + console.error('Your current remote is not GitHub'); + } + } catch (error) { + console.info('No current remote found. Skipping upstream remote setup.'); } // Run license script @@ -66,7 +106,7 @@ function setupRemote() { stdio: 'inherit', }); } catch (error) { - console.error('License script failed. Aborting package generation.'); + console.error(`License script failed. Aborting package generation. Error: ${error}`); process.exit(1); } }