-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update metadata commands to be more resilient
1. Shorter timeouts, but errors will just lead to undefined 2. Don't pipe to `head` or `wc` as they may not exist
- Loading branch information
Showing
6 changed files
with
177 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { createInterface } from 'node:readline'; | ||
|
||
import { execaCommand } from 'execa'; | ||
|
||
import gitNoCommits from '../ui/messages/errors/gitNoCommits'; | ||
import gitNotInitialized from '../ui/messages/errors/gitNotInitialized'; | ||
import gitNotInstalled from '../ui/messages/errors/gitNotInstalled'; | ||
|
||
/** | ||
* Execute a Git command in the local terminal. | ||
* | ||
* @param command The command to execute. | ||
* @param options Execa options | ||
* | ||
* @returns The result of the command from the terminal. | ||
*/ | ||
export async function execGitCommand( | ||
command: string, | ||
options?: Parameters<typeof execaCommand>[1] | ||
) { | ||
try { | ||
const { all } = await execaCommand(command, { | ||
env: { LANG: 'C', LC_ALL: 'C' }, // make sure we're speaking English | ||
timeout: 20_000, // 20 seconds | ||
all: true, // interleave stdout and stderr | ||
shell: true, // we'll deal with escaping ourselves (for now) | ||
...options, | ||
}); | ||
|
||
if (all === undefined) { | ||
throw new Error(`Unexpected missing git command output for command: '${command}`); | ||
} | ||
|
||
return all.toString(); | ||
} catch (error) { | ||
const { message } = error; | ||
|
||
if (message.includes('not a git repository')) { | ||
throw new Error(gitNotInitialized({ command })); | ||
} | ||
|
||
if (message.includes('git not found')) { | ||
throw new Error(gitNotInstalled({ command })); | ||
} | ||
|
||
if (message.includes('does not have any commits yet')) { | ||
throw new Error(gitNoCommits({ command })); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Execute a Git command in the local terminal and just get the first line. | ||
* | ||
* @param command The command to execute. | ||
* @param options Execa options | ||
* | ||
* @returns The first line of the command from the terminal. | ||
*/ | ||
export async function execGitCommandOneLine( | ||
command: string, | ||
options?: Parameters<typeof execaCommand>[1] | ||
) { | ||
const process = execaCommand(command, { | ||
env: { LANG: 'C', LC_ALL: 'C' }, // make sure we're speaking English | ||
timeout: 20_000, // 20 seconds | ||
all: true, // interleave stdout and stderr | ||
shell: true, // we'll deal with escaping ourselves (for now) | ||
...options, | ||
}); | ||
|
||
return Promise.race([ | ||
// This promise will resolve only if there is an error or it times out | ||
(async () => { | ||
await process; | ||
throw new Error(`Unexpected missing git command output for command: '${command}`); | ||
})(), | ||
// We expect this promise to resolve first | ||
new Promise<string>((resolve, reject) => { | ||
if (!process.stdout) { | ||
return reject(new Error('Unexpected missing stdout')); | ||
} | ||
|
||
const rl = createInterface(process.stdout); | ||
rl.once('line', (line) => { | ||
rl.close(); | ||
process.kill(); | ||
|
||
resolve(line); | ||
}); | ||
}), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.