2019-10-11 00:45:39 -04:00
|
|
|
/**
|
2020-02-25 10:12:28 -05:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2019-10-11 00:45:39 -04:00
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-20 11:24:59 -05:00
|
|
|
import logger from '@docusaurus/logger';
|
2022-02-09 11:18:32 -05:00
|
|
|
import {getFileCommitDate, GitNotFoundError} from '@docusaurus/utils';
|
2019-10-11 00:45:39 -04:00
|
|
|
|
|
|
|
|
type FileLastUpdateData = {timestamp?: number; author?: string};
|
|
|
|
|
|
2019-10-11 01:14:09 -04:00
|
|
|
let showedGitRequirementError = false;
|
|
|
|
|
|
2020-08-17 11:50:22 -04:00
|
|
|
export async function getFileLastUpdate(
|
2019-11-08 11:35:39 -05:00
|
|
|
filePath?: string,
|
|
|
|
|
): Promise<FileLastUpdateData | null> {
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-10-11 00:45:39 -04:00
|
|
|
|
2020-02-18 08:29:23 -05:00
|
|
|
// Wrap in try/catch in case the shell commands fail
|
|
|
|
|
// (e.g. project doesn't use Git, etc).
|
2019-10-11 00:45:39 -04:00
|
|
|
try {
|
2022-02-09 11:18:32 -05:00
|
|
|
const result = getFileCommitDate(filePath, {
|
|
|
|
|
age: 'newest',
|
|
|
|
|
includeAuthor: true,
|
|
|
|
|
});
|
|
|
|
|
return {timestamp: result.timestamp, author: result.author};
|
2022-02-25 02:07:13 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof GitNotFoundError && !showedGitRequirementError) {
|
2022-02-09 11:18:32 -05:00
|
|
|
logger.warn('Sorry, the docs plugin last update options require Git.');
|
|
|
|
|
showedGitRequirementError = true;
|
|
|
|
|
} else {
|
2022-02-25 02:07:13 -05:00
|
|
|
logger.error(err);
|
2022-02-09 11:18:32 -05:00
|
|
|
}
|
|
|
|
|
return null;
|
2019-10-11 00:45:39 -04:00
|
|
|
}
|
|
|
|
|
}
|