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-03-20 20:42:36 -04:00
|
|
|
import {
|
|
|
|
|
getFileCommitDate,
|
|
|
|
|
FileNotTrackedError,
|
|
|
|
|
GitNotFoundError,
|
|
|
|
|
} from '@docusaurus/utils';
|
2019-10-11 00:45:39 -04:00
|
|
|
|
2019-10-11 01:14:09 -04:00
|
|
|
let showedGitRequirementError = false;
|
2022-03-20 20:42:36 -04:00
|
|
|
let showedFileNotTrackedError = false;
|
2019-10-11 01:14:09 -04:00
|
|
|
|
2020-08-17 11:50:22 -04:00
|
|
|
export async function getFileLastUpdate(
|
2022-06-07 09:42:17 -04:00
|
|
|
filePath: string,
|
2022-03-21 06:40:20 -04:00
|
|
|
): Promise<{timestamp: number; author: string} | null> {
|
2019-11-08 11:35:39 -05:00
|
|
|
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) {
|
2022-03-23 11:35:26 -04:00
|
|
|
if (err instanceof GitNotFoundError) {
|
|
|
|
|
if (!showedGitRequirementError) {
|
|
|
|
|
logger.warn('Sorry, the docs plugin last update options require Git.');
|
|
|
|
|
showedGitRequirementError = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (err instanceof FileNotTrackedError) {
|
|
|
|
|
if (!showedFileNotTrackedError) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
'Cannot infer the update date for some files, as they are not tracked by git.',
|
|
|
|
|
);
|
|
|
|
|
showedFileNotTrackedError = true;
|
|
|
|
|
}
|
2022-02-09 11:18:32 -05:00
|
|
|
} else {
|
2022-03-20 20:42:36 -04:00
|
|
|
logger.warn(err);
|
2022-02-09 11:18:32 -05:00
|
|
|
}
|
|
|
|
|
return null;
|
2019-10-11 00:45:39 -04:00
|
|
|
}
|
|
|
|
|
}
|