mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 14:22:27 +02:00
perf: optimize getFileCommitDate, make it async (#9890)
This commit is contained in:
parent
0279c329ad
commit
f159bb2472
5 changed files with 54 additions and 34 deletions
|
@ -24,7 +24,7 @@ export class FileNotTrackedError extends Error {}
|
|||
* @throws Also throws when `git log` exited with non-zero, or when it outputs
|
||||
* unexpected text.
|
||||
*/
|
||||
export function getFileCommitDate(
|
||||
export async function getFileCommitDate(
|
||||
/** Absolute path to the file. */
|
||||
file: string,
|
||||
args: {
|
||||
|
@ -36,12 +36,12 @@ export function getFileCommitDate(
|
|||
/** Use `includeAuthor: true` to get the author information as well. */
|
||||
includeAuthor?: false;
|
||||
},
|
||||
): {
|
||||
): Promise<{
|
||||
/** Relevant commit date. */
|
||||
date: Date;
|
||||
/** Timestamp in **seconds**, as returned from git. */
|
||||
timestamp: number;
|
||||
};
|
||||
}>;
|
||||
/**
|
||||
* Fetches the git history of a file and returns a relevant commit date.
|
||||
* It gets the commit date instead of author date so that amended commits
|
||||
|
@ -52,7 +52,7 @@ export function getFileCommitDate(
|
|||
* @throws Also throws when `git log` exited with non-zero, or when it outputs
|
||||
* unexpected text.
|
||||
*/
|
||||
export function getFileCommitDate(
|
||||
export async function getFileCommitDate(
|
||||
/** Absolute path to the file. */
|
||||
file: string,
|
||||
args: {
|
||||
|
@ -63,15 +63,16 @@ export function getFileCommitDate(
|
|||
age?: 'oldest' | 'newest';
|
||||
includeAuthor: true;
|
||||
},
|
||||
): {
|
||||
): Promise<{
|
||||
/** Relevant commit date. */
|
||||
date: Date;
|
||||
/** Timestamp in **seconds**, as returned from git. */
|
||||
timestamp: number;
|
||||
/** The author's name, as returned from git. */
|
||||
author: string;
|
||||
};
|
||||
export function getFileCommitDate(
|
||||
}>;
|
||||
|
||||
export async function getFileCommitDate(
|
||||
file: string,
|
||||
{
|
||||
age = 'oldest',
|
||||
|
@ -80,11 +81,11 @@ export function getFileCommitDate(
|
|||
age?: 'oldest' | 'newest';
|
||||
includeAuthor?: boolean;
|
||||
},
|
||||
): {
|
||||
): Promise<{
|
||||
date: Date;
|
||||
timestamp: number;
|
||||
author?: string;
|
||||
} {
|
||||
}> {
|
||||
if (!shell.which('git')) {
|
||||
throw new GitNotFoundError(
|
||||
`Failed to retrieve git history for "${file}" because git is not installed.`,
|
||||
|
@ -105,11 +106,24 @@ export function getFileCommitDate(
|
|||
.filter(Boolean)
|
||||
.join(' ');
|
||||
|
||||
const result = shell.exec(`git log ${args} -- "${path.basename(file)}"`, {
|
||||
// Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
||||
cwd: path.dirname(file),
|
||||
silent: true,
|
||||
const result = await new Promise<{
|
||||
code: number;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}>((resolve) => {
|
||||
shell.exec(
|
||||
`git log ${args} -- "${path.basename(file)}"`,
|
||||
{
|
||||
// Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
||||
cwd: path.dirname(file),
|
||||
silent: true,
|
||||
},
|
||||
(code, stdout, stderr) => {
|
||||
resolve({code, stdout, stderr});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
if (result.code !== 0) {
|
||||
throw new Error(
|
||||
`Failed to retrieve the git history for file "${file}" with exit code ${result.code}: ${result.stderr}`,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue