|
| 1 | +import { parseCommandLineArgv } from 'windows-argv-parser' |
| 2 | +import stringArgv from 'string-argv' |
| 3 | +import { promisify } from 'util' |
| 4 | +import { exec } from 'child_process' |
| 5 | +import { access, stat } from 'fs/promises' |
| 6 | +import * as fs from 'fs' |
| 7 | + |
| 8 | +const execAsync = promisify(exec) |
| 9 | + |
| 10 | +/** The string that will be replaced by the target path in the custom integration arguments */ |
| 11 | +export const TargetPathArgument = '%TARGET_PATH%' |
| 12 | + |
| 13 | +/** The interface representing a custom integration (external editor or shell) */ |
| 14 | +export interface ICustomIntegration { |
| 15 | + /** The path to the custom integration */ |
| 16 | + readonly path: string |
| 17 | + /** The arguments to pass to the custom integration */ |
| 18 | + readonly arguments: ReadonlyArray<string> |
| 19 | + /** The bundle ID of the custom integration (macOS only) */ |
| 20 | + readonly bundleID?: string |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Parse the arguments string of a custom integration into an array of strings. |
| 25 | + * |
| 26 | + * @param args The arguments string to parse |
| 27 | + */ |
| 28 | +export function parseCustomIntegrationArguments( |
| 29 | + args: string |
| 30 | +): ReadonlyArray<string> { |
| 31 | + return __WIN32__ ? parseCommandLineArgv(args) : stringArgv(args) |
| 32 | +} |
| 33 | + |
| 34 | +// Function to retrieve, on macOS, the bundleId of an app given its path |
| 35 | +async function getAppBundleID(path: string) { |
| 36 | + try { |
| 37 | + // Ensure the path ends with `.app` for applications |
| 38 | + if (!path.endsWith('.app')) { |
| 39 | + throw new Error( |
| 40 | + 'The provided path does not point to a macOS application.' |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + // Use mdls to query the kMDItemCFBundleIdentifier attribute |
| 45 | + const { stdout } = await execAsync( |
| 46 | + `mdls -name kMDItemCFBundleIdentifier -raw "${path}"` |
| 47 | + ) |
| 48 | + const bundleId = stdout.trim() |
| 49 | + |
| 50 | + // Check for valid output |
| 51 | + if (!bundleId || bundleId === '(null)') { |
| 52 | + return undefined |
| 53 | + } |
| 54 | + |
| 55 | + return bundleId |
| 56 | + } catch (error) { |
| 57 | + console.error('Failed to retrieve bundle ID:', error) |
| 58 | + return undefined |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Replace the target path placeholder in the custom integration arguments with |
| 64 | + * the actual target path. |
| 65 | + * |
| 66 | + * @param args The custom integration arguments |
| 67 | + * @param repoPath The target path to replace the placeholder with |
| 68 | + */ |
| 69 | +export function expandTargetPathArgument( |
| 70 | + args: ReadonlyArray<string>, |
| 71 | + repoPath: string |
| 72 | +): ReadonlyArray<string> { |
| 73 | + return args.map(arg => arg.replaceAll(TargetPathArgument, repoPath)) |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Check if the custom integration arguments contain the target path placeholder. |
| 78 | + * |
| 79 | + * @param args The custom integration arguments |
| 80 | + */ |
| 81 | +export function checkTargetPathArgument(args: ReadonlyArray<string>): boolean { |
| 82 | + return args.some(arg => arg.includes(TargetPathArgument)) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Validate the path of a custom integration. |
| 87 | + * |
| 88 | + * @param path The path to the custom integration |
| 89 | + * |
| 90 | + * @returns An object with a boolean indicating if the path is valid and, if |
| 91 | + * the path is a macOS app, the bundle ID of the app. |
| 92 | + */ |
| 93 | +export async function validateCustomIntegrationPath( |
| 94 | + path: string |
| 95 | +): Promise<{ isValid: boolean; bundleID?: string }> { |
| 96 | + if (path.length === 0) { |
| 97 | + return { isValid: false } |
| 98 | + } |
| 99 | + |
| 100 | + let bundleID = undefined |
| 101 | + |
| 102 | + try { |
| 103 | + const pathStat = await stat(path) |
| 104 | + const canBeExecuted = await access(path, fs.constants.X_OK) |
| 105 | + .then(() => true) |
| 106 | + .catch(() => false) |
| 107 | + |
| 108 | + const isExecutableFile = pathStat.isFile() && canBeExecuted |
| 109 | + |
| 110 | + // On macOS, not only executable files are valid, but also apps (which are |
| 111 | + // directories with a `.app` extension and from which we can retrieve |
| 112 | + // the app bundle ID) |
| 113 | + if (__DARWIN__ && !isExecutableFile && pathStat.isDirectory()) { |
| 114 | + bundleID = await getAppBundleID(path) |
| 115 | + } |
| 116 | + |
| 117 | + return { isValid: isExecutableFile || !!bundleID, bundleID } |
| 118 | + } catch (e) { |
| 119 | + return { isValid: false } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Check if a custom integration is valid (meaning both the path and the |
| 125 | + * arguments are valid). |
| 126 | + * |
| 127 | + * @param customIntegration The custom integration to validate |
| 128 | + */ |
| 129 | +export async function isValidCustomIntegration( |
| 130 | + customIntegration: ICustomIntegration |
| 131 | +): Promise<boolean> { |
| 132 | + const pathResult = await validateCustomIntegrationPath(customIntegration.path) |
| 133 | + const targetPathPresent = checkTargetPathArgument(customIntegration.arguments) |
| 134 | + return pathResult.isValid && targetPathPresent |
| 135 | +} |
0 commit comments