From 334b4397e7f4367262a743907ad6dbbd9f77767e Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Mon, 8 Nov 2021 15:33:06 +0800 Subject: [PATCH] refactor(content-docs): use shelljs instead of execa (#5904) --- .../package.json | 1 - .../src/__tests__/lastUpdate.test.ts | 2 +- .../src/lastUpdate.ts | 19 ++++++------------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/package.json b/packages/docusaurus-plugin-content-docs/package.json index 61849c6a1ca0..ee791cbb155f 100644 --- a/packages/docusaurus-plugin-content-docs/package.json +++ b/packages/docusaurus-plugin-content-docs/package.json @@ -34,7 +34,6 @@ "chalk": "^4.1.2", "combine-promises": "^1.1.0", "escape-string-regexp": "^4.0.0", - "execa": "^5.0.0", "fs-extra": "^10.0.0", "globby": "^11.0.2", "import-fresh": "^3.2.2", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts index 323ac109ec15..5c8f07e0e512 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts @@ -40,7 +40,7 @@ describe('lastUpdate', () => { expect(await getFileLastUpdate(nonExistingFilePath)).toBeNull(); expect(consoleMock).toHaveBeenCalledTimes(1); expect(consoleMock.mock.calls[0][0].message).toContain( - `Command failed with exit code 128: git log -1 --format=%ct, %an ${nonExistingFileName}`, + `fatal: ambiguous argument '${nonExistingFilePath}': unknown revision or path not in the working tree.`, ); expect(await getFileLastUpdate(null)).toBeNull(); expect(await getFileLastUpdate(undefined)).toBeNull(); diff --git a/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts b/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts index a7d07e9f9406..a5de58da6aed 100644 --- a/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts +++ b/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts @@ -6,12 +6,10 @@ */ import shell from 'shelljs'; -import execa from 'execa'; -import path from 'path'; type FileLastUpdateData = {timestamp?: number; author?: string}; -const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+), (.+)$/; +const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+),(.+)$/; let showedGitRequirementError = false; @@ -44,16 +42,11 @@ export async function getFileLastUpdate( return null; } - const fileBasename = path.basename(filePath); - const fileDirname = path.dirname(filePath); - const {stdout} = await execa( - 'git', - ['log', '-1', '--format=%ct, %an', fileBasename], - { - cwd: fileDirname, - }, - ); - return getTimestampAndAuthor(stdout); + const result = shell.exec(`git log -1 --format=%ct,%an ${filePath}`); + if (result.stderr) { + throw new Error(result.stderr); + } + return getTimestampAndAuthor(result.stdout.trim()); } catch (error) { console.error(error); }