mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
refactor(v2): simplify & faster last updated logic for docs (#1832)
* refactor(v2): simplify last updated logic for faster dev * Update packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts * Update packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts
This commit is contained in:
parent
3b143c2671
commit
48db8b8e09
6 changed files with 56 additions and 74 deletions
|
@ -7,7 +7,6 @@
|
|||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import shell from 'shelljs';
|
||||
|
||||
import lastUpdate from '../lastUpdate';
|
||||
|
||||
|
@ -45,43 +44,4 @@ describe('lastUpdate', () => {
|
|||
expect(lastUpdate(tempFilePath)).toBeNull();
|
||||
fs.unlinkSync(tempFilePath);
|
||||
});
|
||||
|
||||
test('test renaming and moving file', () => {
|
||||
const mock = jest.spyOn(shell, 'exec');
|
||||
mock
|
||||
.mockImplementationOnce(() => ({
|
||||
stdout:
|
||||
'1539502055, Yangshun Tay\n' +
|
||||
'\n' +
|
||||
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
|
||||
}))
|
||||
.mockImplementationOnce(() => ({
|
||||
stdout:
|
||||
'1539502056, Joel Marcey\n' +
|
||||
'\n' +
|
||||
' rename v1/lib/core/__tests__/__fixtures__/{.temp2 => test/.temp3} (100%)\n' +
|
||||
'1539502055, Yangshun Tay\n' +
|
||||
'\n' +
|
||||
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
|
||||
}));
|
||||
const tempFilePath2 = path.join(__dirname, '__fixtures__', '.temp2');
|
||||
const tempFilePath3 = path.join(
|
||||
__dirname,
|
||||
'__fixtures__',
|
||||
'test',
|
||||
'.temp3',
|
||||
);
|
||||
|
||||
// Create new file.
|
||||
const createData = lastUpdate(tempFilePath2);
|
||||
expect(createData.timestamp).not.toBeNull();
|
||||
|
||||
// Rename/move the file.
|
||||
const updateData = lastUpdate(tempFilePath3);
|
||||
expect(updateData.timestamp).not.toBeNull();
|
||||
// Should only consider file content change.
|
||||
expect(updateData.timestamp).toEqual(createData.timestamp);
|
||||
|
||||
mock.mockRestore();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import shell from 'shelljs';
|
||||
import spawn from 'cross-spawn';
|
||||
|
||||
type FileLastUpdateData = {timestamp?: number; author?: string};
|
||||
|
||||
|
@ -16,10 +17,6 @@ let showedGitRequirementError = false;
|
|||
export default function getFileLastUpdate(
|
||||
filePath: string,
|
||||
): FileLastUpdateData | null {
|
||||
function isTimestampAndAuthor(str: string): boolean {
|
||||
return GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX.test(str);
|
||||
}
|
||||
|
||||
function getTimestampAndAuthor(str: string): FileLastUpdateData | null {
|
||||
if (!str) {
|
||||
return null;
|
||||
|
@ -42,36 +39,12 @@ export default function getFileLastUpdate(
|
|||
return null;
|
||||
}
|
||||
|
||||
// To differentiate between content change and file renaming/moving, use --summary
|
||||
// To follow the file history until before it is moved (when we create new version), use
|
||||
// --follow.
|
||||
const silentState = shell.config.silent; // Save old silent state.
|
||||
shell.config.silent = true;
|
||||
const result = shell
|
||||
.exec(`git log --follow --summary --format="%ct, %an" ${filePath}`)
|
||||
.stdout.trim();
|
||||
shell.config.silent = silentState;
|
||||
const result = spawn
|
||||
.sync('git', ['log', '-1', '--format=%ct, %an', filePath])
|
||||
.stdout.toString()
|
||||
.trim();
|
||||
|
||||
// Format the log results to be
|
||||
// ['1234567890, Yangshun Tay', 'rename ...', '1234567880,
|
||||
// 'Joel Marcey', 'move ...', '1234567870', '1234567860']
|
||||
const records = result
|
||||
.replace(/\n\s*\n/g, '\n')
|
||||
.split('\n')
|
||||
.filter(String);
|
||||
const lastContentModifierCommit = records.find((item, index, arr) => {
|
||||
const currentItemIsTimestampAndAuthor = isTimestampAndAuthor(item);
|
||||
const isLastTwoItem = index + 2 >= arr.length;
|
||||
const nextItemIsTimestampAndAuthor = isTimestampAndAuthor(arr[index + 1]);
|
||||
return (
|
||||
currentItemIsTimestampAndAuthor &&
|
||||
(isLastTwoItem || nextItemIsTimestampAndAuthor)
|
||||
);
|
||||
});
|
||||
|
||||
return lastContentModifierCommit
|
||||
? getTimestampAndAuthor(lastContentModifierCommit)
|
||||
: null;
|
||||
return getTimestampAndAuthor(result);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
|
|
@ -104,7 +104,14 @@ export default async function processMetadata({
|
|||
}
|
||||
|
||||
if (showLastUpdateAuthor || showLastUpdateTime) {
|
||||
const fileLastUpdateData = lastUpdate(filePath);
|
||||
// Use fake data in dev for faster development
|
||||
const fileLastUpdateData =
|
||||
process.env.NODE_ENV === 'production'
|
||||
? lastUpdate(filePath)
|
||||
: {
|
||||
author: 'Author',
|
||||
timestamp: '1539502055',
|
||||
};
|
||||
|
||||
if (fileLastUpdateData) {
|
||||
const {author, timestamp} = fileLastUpdateData;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue