mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-31 07:32:53 +00:00
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import shell from 'shelljs';
|
|
|
|
type FileLastUpdateData = {timestamp?: number; author?: string};
|
|
|
|
const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+),(.+)$/;
|
|
|
|
let showedGitRequirementError = false;
|
|
|
|
export async function getFileLastUpdate(
|
|
filePath?: string,
|
|
): Promise<FileLastUpdateData | null> {
|
|
if (!filePath) {
|
|
return null;
|
|
}
|
|
function getTimestampAndAuthor(str: string): FileLastUpdateData | null {
|
|
if (!str) {
|
|
return null;
|
|
}
|
|
|
|
const temp = str.match(GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX);
|
|
return !temp || temp.length < 3
|
|
? null
|
|
: {timestamp: +temp[1], author: temp[2]};
|
|
}
|
|
|
|
// Wrap in try/catch in case the shell commands fail
|
|
// (e.g. project doesn't use Git, etc).
|
|
try {
|
|
if (!shell.which('git')) {
|
|
if (!showedGitRequirementError) {
|
|
showedGitRequirementError = true;
|
|
console.warn('Sorry, the docs plugin last update options require Git.');
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
const result = shell.exec(`git log -1 --format=%ct,%an ${filePath}`);
|
|
if (result.code !== 0) {
|
|
throw new Error(
|
|
`Retrieval of git history failed at ${filePath} with exit code ${result.code}: ${result.stderr}`,
|
|
);
|
|
}
|
|
return getTimestampAndAuthor(result.stdout.trim());
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
|
|
return null;
|
|
}
|