fix(hook): Windows support (#176)

This commit is contained in:
hiroki osame
2023-04-02 05:54:11 -04:00
committed by GitHub
parent f6d43f242c
commit 225558394b
6 changed files with 107 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { fileURLToPath, pathToFileURL } from 'url';
import { green, red } from 'kolorist';
import { command } from 'cleye';
import { assertGitRepo } from '../utils/git.js';
@@ -10,14 +10,24 @@ import { KnownError, handleCliError } from '../utils/error.js';
const hookName = 'prepare-commit-msg';
const symlinkPath = `.git/hooks/${hookName}`;
export const isCalledFromGitHook = process.argv[1].endsWith(`/${symlinkPath}`);
const hookPath = fileURLToPath(new URL('cli.mjs', import.meta.url));
export const isCalledFromGitHook = (
process.argv[1]
.replace(/\\/g, '/') // Replace Windows back slashes with forward slashes
.endsWith(`/${symlinkPath}`)
);
const isWindows = process.platform === 'win32';
const windowsHook = `
#!/usr/bin/env node
import(${JSON.stringify(pathToFileURL(hookPath))})
`.trim();
export default command({
name: 'hook',
parameters: ['<install/uninstall>'],
}, (argv) => {
const hookPath = fileURLToPath(new URL('cli.mjs', import.meta.url));
(async () => {
await assertGitRepo();
@@ -29,7 +39,7 @@ export default command({
// If the symlink is broken, it will throw an error
// eslint-disable-next-line @typescript-eslint/no-empty-function
const realpath = await fs.realpath(symlinkPath).catch(() => {});
if (realpath === hookPath) {
if (realpath === hookPath) {
console.warn('The hook is already installed');
return;
}
@@ -37,8 +47,16 @@ export default command({
}
await fs.mkdir(path.dirname(symlinkPath), { recursive: true });
await fs.symlink(hookPath, symlinkPath, 'file');
await fs.chmod(symlinkPath, 0o755);
if (isWindows) {
await fs.writeFile(
symlinkPath,
windowsHook,
);
} else {
await fs.symlink(hookPath, symlinkPath, 'file');
await fs.chmod(symlinkPath, 0o755);
}
console.log(`${green('✔')} Hook installed`);
return;
}
@@ -48,10 +66,19 @@ export default command({
console.warn('Hook is not installed');
return;
}
const realpath = await fs.realpath(symlinkPath);
if (realpath !== hookPath) {
console.warn('Hook is not installed');
return;
if (isWindows) {
const scriptContent = await fs.readFile(symlinkPath, 'utf8');
if (scriptContent !== windowsHook) {
console.warn('Hook is not installed');
return;
}
} else {
const realpath = await fs.realpath(symlinkPath);
if (realpath !== hookPath) {
console.warn('Hook is not installed');
return;
}
}
await fs.rm(symlinkPath);

View File

@@ -50,14 +50,32 @@ export default () => (async () => {
} finally {
s.stop('Changes analyzed');
}
/**
* When `--no-edit` is passed in, the base commit message is empty,
* and even when you use pass in comments via #, they are ignored.
*
* Note: `--no-edit` cannot be detected in argvs so this is the only way to check
*/
const baseMessage = await fs.readFile(messageFilePath, 'utf8');
const supportsComments = baseMessage !== '';
const hasMultipleMessages = messages.length > 1;
let instructions = `# 🤖 AI generated commit${hasMultipleMessages ? 's' : ''}\n`;
let instructions = '';
if (supportsComments) {
instructions = `# 🤖 AI generated commit${hasMultipleMessages ? 's' : ''}\n`;
}
if (hasMultipleMessages) {
instructions += '# Select one of the following messages by uncommeting:\n';
if (supportsComments) {
instructions += '# Select one of the following messages by uncommeting:\n';
}
instructions += `\n${messages.map(message => `# ${message}`).join('\n')}`;
} else {
instructions += '# Edit the message below and commit:\n';
if (supportsComments) {
instructions += '# Edit the message below and commit:\n';
}
instructions += `\n${messages[0]}\n`;
}