docusaurus/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts
Endi a8826b98b3 fix(v2): docs plugin stability improvement (100% test coverage) (#1912)
* update jest config

* add more tests on docs plugin

* fix(v2): docs plugin should not add routes if there are no docs

* fix

* rm -rf coverage

* nits

* update
2019-10-29 23:59:27 +08:00

54 lines
1.3 KiB
TypeScript

/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import shell from 'shelljs';
import spawn from 'cross-spawn';
type FileLastUpdateData = {timestamp?: number; author?: string};
const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+), (.+)$/;
let showedGitRequirementError = false;
export default function getFileLastUpdate(
filePath: string,
): FileLastUpdateData | null {
function getTimestampAndAuthor(str: string): FileLastUpdateData | null {
if (!str) {
return null;
}
const temp = str.match(GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX);
return !temp || temp.length < 3
? null
: {timestamp: +temp[1], author: temp[2]};
}
// 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;
console.warn('Sorry, the docs plugin last update options require Git.');
}
return null;
}
const result = spawn
.sync('git', ['log', '-1', '--format=%ct, %an', filePath])
.stdout.toString()
.trim();
return getTimestampAndAuthor(result);
} catch (error) {
console.error(error);
}
return null;
}