mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
feat(content-blog): infer blog post date from git history (#6593)
This commit is contained in:
parent
665d164351
commit
6996ed2f2f
7 changed files with 127 additions and 57 deletions
|
@ -5,14 +5,11 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import shell from 'shelljs';
|
||||
import logger from '@docusaurus/logger';
|
||||
import path from 'path';
|
||||
import {getFileCommitDate, GitNotFoundError} from '@docusaurus/utils';
|
||||
|
||||
type FileLastUpdateData = {timestamp?: number; author?: string};
|
||||
|
||||
const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(?<timestamp>\d+),(?<author>.+)$/;
|
||||
|
||||
let showedGitRequirementError = false;
|
||||
|
||||
export async function getFileLastUpdate(
|
||||
|
@ -21,53 +18,22 @@ export async function getFileLastUpdate(
|
|||
if (!filePath) {
|
||||
return null;
|
||||
}
|
||||
function getTimestampAndAuthor(str: string): FileLastUpdateData | null {
|
||||
if (!str) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const temp = str.match(GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX)?.groups;
|
||||
return temp
|
||||
? {timestamp: Number(temp.timestamp), author: temp.author}
|
||||
: null;
|
||||
}
|
||||
|
||||
// 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;
|
||||
logger.warn('Sorry, the docs plugin last update options require Git.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!shell.test('-f', filePath)) {
|
||||
throw new Error(
|
||||
`Retrieval of git history failed at "${filePath}" because the file does not exist.`,
|
||||
);
|
||||
}
|
||||
|
||||
const fileBasename = path.basename(filePath);
|
||||
const fileDirname = path.dirname(filePath);
|
||||
const result = shell.exec(
|
||||
`git log --max-count=1 --format=%ct,%an -- "${fileBasename}"`,
|
||||
{
|
||||
cwd: fileDirname, // this is needed: https://github.com/facebook/docusaurus/pull/5048
|
||||
silent: true,
|
||||
},
|
||||
);
|
||||
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());
|
||||
const result = getFileCommitDate(filePath, {
|
||||
age: 'newest',
|
||||
includeAuthor: true,
|
||||
});
|
||||
return {timestamp: result.timestamp, author: result.author};
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
if (e instanceof GitNotFoundError && !showedGitRequirementError) {
|
||||
logger.warn('Sorry, the docs plugin last update options require Git.');
|
||||
showedGitRequirementError = true;
|
||||
} else {
|
||||
logger.error(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue