|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { mkdir, readFile, symlink } from 'node:fs/promises' |
| 4 | +import { join } from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | + |
| 7 | +/** |
| 8 | + * Reads the nearest package.json and returns the package name. |
| 9 | + * @param rootDir Directory to start searching from (defaults to process.cwd()) |
| 10 | + */ |
| 11 | +async function getPackageName(rootDir: string = process.cwd()): Promise<string | undefined> { |
| 12 | + try { |
| 13 | + const pkgPath = join(rootDir, 'package.json') |
| 14 | + const pkgJson = await readFile(pkgPath, 'utf8') |
| 15 | + const pkg = JSON.parse(pkgJson) |
| 16 | + return pkg.name |
| 17 | + } |
| 18 | + catch (err) { |
| 19 | + console.error(`[ERROR] Failed to get package name: ${err}`) |
| 20 | + // Optionally, walk up directories if not found, or just return undefined |
| 21 | + return undefined |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function getBinNames(rootDir: string = process.cwd()): Promise<string[] | undefined> { |
| 26 | + try { |
| 27 | + const pkgPath = join(rootDir, 'package.json') |
| 28 | + const pkgJson = await readFile(pkgPath, 'utf8') |
| 29 | + const pkg = JSON.parse(pkgJson) |
| 30 | + if (!pkg.bin) |
| 31 | + return undefined |
| 32 | + if (typeof pkg.bin === 'string') { |
| 33 | + // If bin is a string, the bin name is the package name |
| 34 | + return [pkg.name] |
| 35 | + } |
| 36 | + if (typeof pkg.bin === 'object') { |
| 37 | + // If bin is an object, the keys are the bin names |
| 38 | + return Object.keys(pkg.bin) |
| 39 | + } |
| 40 | + return undefined |
| 41 | + } |
| 42 | + catch (err) { |
| 43 | + console.error(`[ERROR] Failed to get bin names: ${err}`) |
| 44 | + return undefined |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/* |
| 49 | +* Transforms the <project>/node_modules/bun-git-hooks to <project> |
| 50 | +*/ |
| 51 | +async function getProjectRootDirectoryFromNodeModules(projectPath: string): Promise<string | undefined> { |
| 52 | + function _arraysAreEqual(a1: any[], a2: any[]) { |
| 53 | + return JSON.stringify(a1) === JSON.stringify(a2) |
| 54 | + } |
| 55 | + |
| 56 | + const packageName = await getPackageName() |
| 57 | + if (packageName === undefined) { |
| 58 | + console.error('[ERROR] Failed to get package name') |
| 59 | + return undefined |
| 60 | + } |
| 61 | + |
| 62 | + const binNames = await getBinNames() |
| 63 | + if (binNames === undefined) { |
| 64 | + console.error('[ERROR] Failed to get bin names') |
| 65 | + return undefined |
| 66 | + } |
| 67 | + |
| 68 | + const projDir = projectPath.split(/[\\/]/) // <- would split both on '/' and '\' |
| 69 | + |
| 70 | + const indexOfStoreDir = projDir.indexOf('.store') |
| 71 | + if (indexOfStoreDir > -1) { |
| 72 | + return projDir.slice(0, indexOfStoreDir - 1).join('/') |
| 73 | + } |
| 74 | + |
| 75 | + // Handle .bin case for any bin name |
| 76 | + if ( |
| 77 | + projDir.length > 3 |
| 78 | + && projDir[projDir.length - 3] === 'node_modules' |
| 79 | + && projDir[projDir.length - 2] === '.bin' |
| 80 | + && binNames.includes(projDir[projDir.length - 1]) |
| 81 | + ) { |
| 82 | + return projDir.slice(0, -3).join('/') |
| 83 | + } |
| 84 | + |
| 85 | + // Existing node_modules check |
| 86 | + if (projDir.length > 2 |
| 87 | + && _arraysAreEqual(projDir.slice(-2), ['node_modules', packageName])) { |
| 88 | + return projDir.slice(0, -2).join('/') |
| 89 | + } |
| 90 | + |
| 91 | + return undefined |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Creates the pre-commit from command in config by default |
| 96 | + */ |
| 97 | +async function postinstall() { |
| 98 | + let projectDirectory |
| 99 | + |
| 100 | + /* When script is run after install, the process.cwd() would be like <project_folder>/node_modules/simple-git-hooks |
| 101 | + Here we try to get the original project directory by going upwards by 2 levels |
| 102 | + If we were not able to get new directory we assume, we are already in the project root */ |
| 103 | + const parsedProjectDirectory = await getProjectRootDirectoryFromNodeModules(process.cwd()) |
| 104 | + if (parsedProjectDirectory !== undefined) { |
| 105 | + projectDirectory = parsedProjectDirectory |
| 106 | + } |
| 107 | + else { |
| 108 | + projectDirectory = process.cwd() |
| 109 | + } |
| 110 | + |
| 111 | + // Link the binary |
| 112 | + const binDir = join(projectDirectory, 'node_modules', '.bin') |
| 113 | + await mkdir(binDir, { recursive: true }) |
| 114 | + |
| 115 | + const sourcePath = join(process.cwd(), 'dist', 'bin', 'cli.js') |
| 116 | + |
| 117 | + const binNames = await getBinNames() |
| 118 | + if (binNames === undefined) { |
| 119 | + console.error('[ERROR] Failed to get bin names') |
| 120 | + return undefined |
| 121 | + } |
| 122 | + |
| 123 | + for (const binName of binNames) { |
| 124 | + const targetPath = join(binDir, binName) |
| 125 | + try { |
| 126 | + await symlink(sourcePath, targetPath, 'file') |
| 127 | + } |
| 128 | + catch (err) { |
| 129 | + if ((err as NodeJS.ErrnoException).code !== 'EEXIST') { |
| 130 | + console.error(`[ERROR] Failed to link binary: ${err}`) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +postinstall() |
0 commit comments