mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 19:27:48 +02:00
fix: exclude file movements for last updated time (#1023)
* Fix last updated time misleading, only show when file content change or otherwise when it is first created Fix #1015 * Fix prettier * - Simplify regex - Uses shelljs instead of cross-spawn - Make logic clearer * Add test when repositories is moved * Use shell.exec mock I initially try to mock the whole shelljs. But it returns error shell.exec is not a function when i try to provide the mockResolvedValue I think it is because of the inner code of shelljs who run a forEach to require each of its method which make it a promise. I tried moving the jest.mock inside beforeAll and also adding babel-dynamic-import but it did not solve the problem. In the end, I decided to just mock shelljs.exec since it is the only function used anyway
This commit is contained in:
parent
bc7be697ad
commit
a39677c202
2 changed files with 63 additions and 4 deletions
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const shell = require('shelljs');
|
||||||
const utils = require('../utils');
|
const utils = require('../utils');
|
||||||
|
|
||||||
const blogPostWithTruncateContents = fs.readFileSync(
|
const blogPostWithTruncateContents = fs.readFileSync(
|
||||||
|
@ -96,6 +97,40 @@ describe('utils', () => {
|
||||||
fs.writeFileSync(tempFilePath, 'Lorem ipsum :)');
|
fs.writeFileSync(tempFilePath, 'Lorem ipsum :)');
|
||||||
expect(utils.getGitLastUpdated(tempFilePath)).toBeNull();
|
expect(utils.getGitLastUpdated(tempFilePath)).toBeNull();
|
||||||
fs.unlinkSync(tempFilePath);
|
fs.unlinkSync(tempFilePath);
|
||||||
|
|
||||||
|
// test renaming and moving file
|
||||||
|
|
||||||
|
const tempFilePath2 = path.join(__dirname, '__fixtures__', '.temp2');
|
||||||
|
const tempFilePath3 = path.join(
|
||||||
|
__dirname,
|
||||||
|
'__fixtures__',
|
||||||
|
'test',
|
||||||
|
'.temp3',
|
||||||
|
);
|
||||||
|
|
||||||
|
// create new file
|
||||||
|
shell.exec = jest.fn(() => ({
|
||||||
|
stdout:
|
||||||
|
'1539502055\n' +
|
||||||
|
'\n' +
|
||||||
|
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
|
||||||
|
}));
|
||||||
|
const createTime = utils.getGitLastUpdated(tempFilePath2);
|
||||||
|
expect(typeof createTime).toBe('string');
|
||||||
|
|
||||||
|
// rename / move the file
|
||||||
|
shell.exec = jest.fn(() => ({
|
||||||
|
stdout:
|
||||||
|
'1539502056\n' +
|
||||||
|
'\n' +
|
||||||
|
' rename v1/lib/core/__tests__/__fixtures__/{.temp2 => test/.temp3} (100%)\n' +
|
||||||
|
'1539502055\n' +
|
||||||
|
'\n' +
|
||||||
|
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
|
||||||
|
}));
|
||||||
|
const lastUpdateTime = utils.getGitLastUpdated(tempFilePath3);
|
||||||
|
// should only consider file content change
|
||||||
|
expect(lastUpdateTime).toEqual(createTime);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('idx', () => {
|
test('idx', () => {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
const spawn = require('cross-spawn');
|
const shell = require('shelljs');
|
||||||
|
|
||||||
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
|
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
|
||||||
|
|
||||||
|
@ -38,10 +38,34 @@ function idx(target, keyPaths) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isNormalInteger(str) {
|
||||||
|
return /^\d+$/.test(str);
|
||||||
|
}
|
||||||
|
|
||||||
function getGitLastUpdated(filepath) {
|
function getGitLastUpdated(filepath) {
|
||||||
const timeSpan = spawn
|
// To differentiate between content change and file renaming / moving, use --summary
|
||||||
.sync('git', ['log', '-1', '--format=%ct', filepath])
|
// To follow the file history until before it is moved (when we create new version), use
|
||||||
.stdout.toString('utf-8');
|
// --follow
|
||||||
|
const silentState = shell.config.silent; // save old silent state
|
||||||
|
shell.config.silent = true;
|
||||||
|
const result = shell
|
||||||
|
.exec(`git log --follow --summary --format=%ct ${filepath}`)
|
||||||
|
.stdout.trim();
|
||||||
|
shell.config.silent = silentState;
|
||||||
|
|
||||||
|
// Format the log results to be ['1234567', 'rename ...', '1234566', 'move ...', '1234565', '1234564']
|
||||||
|
const records = result
|
||||||
|
.toString('utf-8')
|
||||||
|
.replace(/\n\s*\n/g, '\n')
|
||||||
|
.split('\n')
|
||||||
|
.filter(String);
|
||||||
|
|
||||||
|
const timeSpan = records.find((item, index, arr) => {
|
||||||
|
const isTimestamp = isNormalInteger(item);
|
||||||
|
const isLastTwoItem = index + 2 >= arr.length;
|
||||||
|
const nextItemIsTimestamp = isNormalInteger(arr[index + 1]);
|
||||||
|
return isTimestamp && (isLastTwoItem || nextItemIsTimestamp);
|
||||||
|
});
|
||||||
if (timeSpan) {
|
if (timeSpan) {
|
||||||
const date = new Date(parseInt(timeSpan, 10) * 1000);
|
const date = new Date(parseInt(timeSpan, 10) * 1000);
|
||||||
return date.toLocaleString();
|
return date.toLocaleString();
|
||||||
|
|
Loading…
Add table
Reference in a new issue