Updated setup generator with a pre-commit script
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import type { PlopTypes } from '@turbo/gen';
|
import type {PlopTypes} from '@turbo/gen';
|
||||||
import { execSync } from 'node:child_process';
|
import {execSync} from 'node:child_process';
|
||||||
|
|
||||||
export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) {
|
export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) {
|
||||||
plop.setGenerator('setup', {
|
plop.setGenerator('setup', {
|
||||||
@@ -10,6 +10,12 @@ export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) {
|
|||||||
name: 'projectName',
|
name: 'projectName',
|
||||||
message: 'What is the name of the project?',
|
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: [
|
actions: [
|
||||||
{
|
{
|
||||||
@@ -24,9 +30,10 @@ export function createSetupGenerator(plop: PlopTypes.NodePlopAPI) {
|
|||||||
return JSON.stringify(pkg, null, 2);
|
return JSON.stringify(pkg, null, 2);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
async () => {
|
async (answers: any) => {
|
||||||
try {
|
try {
|
||||||
setupRemote();
|
setupRemote();
|
||||||
|
setupPreCommit({setupHealthCheck: answers.setupHealthCheck});
|
||||||
|
|
||||||
return 'Project setup complete';
|
return 'Project setup complete';
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -38,7 +45,36 @@ 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() {
|
function setupRemote() {
|
||||||
|
try {
|
||||||
// Setup remote upstream
|
// Setup remote upstream
|
||||||
const getRemoteUrl = execSync('git remote get-url origin', {
|
const getRemoteUrl = execSync('git remote get-url origin', {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
@@ -49,7 +85,8 @@ function setupRemote() {
|
|||||||
console.log(`Setting upstream remote to ${currentRemote} ...`);
|
console.log(`Setting upstream remote to ${currentRemote} ...`);
|
||||||
|
|
||||||
if (currentRemote && currentRemote.includes('github.com')) {
|
if (currentRemote && currentRemote.includes('github.com')) {
|
||||||
execSync(`git remote delete origin`, {
|
|
||||||
|
execSync(`git remote remove origin`, {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -59,6 +96,9 @@ function setupRemote() {
|
|||||||
} else {
|
} else {
|
||||||
console.error('Your current remote is not GitHub');
|
console.error('Your current remote is not GitHub');
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.info('No current remote found. Skipping upstream remote setup.');
|
||||||
|
}
|
||||||
|
|
||||||
// Run license script
|
// Run license script
|
||||||
try {
|
try {
|
||||||
@@ -66,7 +106,7 @@ function setupRemote() {
|
|||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('License script failed. Aborting package generation.');
|
console.error(`License script failed. Aborting package generation. Error: ${error}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user