From bdb8da49b77c6d689782e13ce1dc4c20fe0aed52 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Mon, 6 Mar 2023 21:33:00 +0800 Subject: [PATCH] =?UTF-8?q?*=20=F0=9F=9A=80=20chore(cli.ts):=20change=20fi?= =?UTF-8?q?le=20mode=20from=20100644=20to=20100755=20This=20commit=20chang?= =?UTF-8?q?es=20the=20file=20mode=20of=20cli.ts=20from=20100644=20to=20100?= =?UTF-8?q?755.=20This=20change=20allows=20the=20file=20to=20be=20executab?= =?UTF-8?q?le.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.ts | 0 src/commands/githook.ts | 100 ++++++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 44 deletions(-) mode change 100644 => 100755 src/cli.ts diff --git a/src/cli.ts b/src/cli.ts old mode 100644 new mode 100755 diff --git a/src/commands/githook.ts b/src/commands/githook.ts index b04169f..a0bb79e 100644 --- a/src/commands/githook.ts +++ b/src/commands/githook.ts @@ -19,59 +19,71 @@ export const hookCommand = command( name: 'hook', parameters: [''] }, - async (argv) => { + (argv) => { const HOOK_PATH = fileURLToPath(new URL('cli.cjs', import.meta.url)); - try { - await assertGitRepo(); + (async () => { + try { + await assertGitRepo(); - const { setUnset: mode } = argv._; + const { setUnset: mode } = argv._; - if (mode === 'set') { - intro(`setting opencommit as '${HOOK_NAME}' hook`); - if (isHookExists) { - const realPath = await fs.realpath(SYMLINK_URL); + if (mode === 'set') { + intro(`setting opencommit as '${HOOK_NAME}' hook`); - if (realPath === HOOK_PATH) - return outro(`opencommit is already set as '${HOOK_NAME}'`); + if (isHookExists) { + let realPath; + try { + realPath = await fs.realpath(SYMLINK_URL); + console.log(123, { realPath }); + } catch (error) { + console.log(321, { realPath }); + outro(error as string); + realPath = null; + } - throw new Error( - `Different ${HOOK_NAME} is already set. Remove it before setting opencommit as '${HOOK_NAME}' hook.` - ); + if (realPath === HOOK_PATH) + return outro(`opencommit is already set as '${HOOK_NAME}'`); + + throw new Error( + `Different ${HOOK_NAME} is already set. Remove it before setting opencommit as '${HOOK_NAME}' hook.` + ); + } + + await fs.mkdir(path.dirname(SYMLINK_URL), { recursive: true }); + await fs.symlink(HOOK_PATH, SYMLINK_URL, 'file'); + await fs.chmod(SYMLINK_URL, 0o755); + + return outro(`${chalk.green('✔')} Hook set`); } - await fs.mkdir(path.dirname(SYMLINK_URL), { recursive: true }); - await fs.symlink(HOOK_PATH, SYMLINK_URL, 'file'); - await fs.chmod(SYMLINK_URL, 0o755); + if (mode === 'unset') { + intro(`unsetting opencommit as '${HOOK_NAME}' hook`); - return outro(`${chalk.green('✔')} Hook set`); + if (!isHookExists) { + return outro( + `opencommit wasn't previously set as '${HOOK_NAME}' hook, nothing to remove` + ); + } + + const realpath = await fs.realpath(SYMLINK_URL); + if (realpath !== HOOK_PATH) { + return outro( + `opencommit wasn't previously set as '${HOOK_NAME}' hook, but different hook was, if you want to remove it — do it manually` + ); + } + + await fs.rm(SYMLINK_URL); + return outro(`${chalk.green('✔')} Hook is removed`); + } + + throw new Error( + `unsupported mode: ${mode}. Supported modes are: 'set' or 'unset'` + ); + } catch (error) { + outro(`${chalk.red('✖')} ${error}`); + process.exit(1); } - - if (mode === 'unset') { - intro(`unsetting opencommit as '${HOOK_NAME}' hook`); - if (!isHookExists) { - return outro( - `opencommit wasn't previously set as '${HOOK_NAME}' hook, nothing to remove` - ); - } - - const realpath = await fs.realpath(SYMLINK_URL); - if (realpath !== HOOK_PATH) { - return outro( - `opencommit wasn't previously set as '${HOOK_NAME}' hook, but different hook was, if you want to remove it — do it manually` - ); - } - - await fs.rm(SYMLINK_URL); - return outro(`${chalk.green('✔')} Hook is removed`); - } - - throw new Error( - `unsupported mode: ${mode}. Supported modes are: 'set' or 'unset'` - ); - } catch (error) { - outro(`${chalk.red('✖')} ${error}`); - process.exit(1); - } + })(); } );