perf(v2): skip runtime fileHash cache in prod & get timestamp asynchronously (#1951)

* skip hash calculation in prod when generating file

* perf: convert lastUpdated to be async process

* changelog
This commit is contained in:
Endi 2019-11-08 23:35:39 +07:00 committed by GitHub
parent e04c8f140f
commit 639d8d3eac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 107 additions and 42 deletions

View file

@ -6,7 +6,7 @@
*/
import shell from 'shelljs';
import spawn from 'cross-spawn';
import execa from 'execa';
type FileLastUpdateData = {timestamp?: number; author?: string};
@ -14,9 +14,12 @@ const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+), (.+)$/;
let showedGitRequirementError = false;
export default function getFileLastUpdate(
filePath: string,
): FileLastUpdateData | null {
export default async function getFileLastUpdate(
filePath?: string,
): Promise<FileLastUpdateData | null> {
if (!filePath) {
return null;
}
function getTimestampAndAuthor(str: string): FileLastUpdateData | null {
if (!str) {
return null;
@ -39,12 +42,13 @@ export default function getFileLastUpdate(
return null;
}
const result = spawn
.sync('git', ['log', '-1', '--format=%ct, %an', filePath])
.stdout.toString()
.trim();
return getTimestampAndAuthor(result);
const {stdout} = await execa('git', [
'log',
'-1',
'--format=%ct, %an',
filePath,
]);
return getTimestampAndAuthor(stdout);
} catch (error) {
console.error(error);
}