refactor(content-docs): use shelljs instead of execa (#5904)

This commit is contained in:
Joshua Chen 2021-11-08 15:33:06 +08:00 committed by GitHub
parent 13d07eff13
commit 334b4397e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 15 deletions

View file

@ -34,7 +34,6 @@
"chalk": "^4.1.2", "chalk": "^4.1.2",
"combine-promises": "^1.1.0", "combine-promises": "^1.1.0",
"escape-string-regexp": "^4.0.0", "escape-string-regexp": "^4.0.0",
"execa": "^5.0.0",
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"globby": "^11.0.2", "globby": "^11.0.2",
"import-fresh": "^3.2.2", "import-fresh": "^3.2.2",

View file

@ -40,7 +40,7 @@ describe('lastUpdate', () => {
expect(await getFileLastUpdate(nonExistingFilePath)).toBeNull(); expect(await getFileLastUpdate(nonExistingFilePath)).toBeNull();
expect(consoleMock).toHaveBeenCalledTimes(1); expect(consoleMock).toHaveBeenCalledTimes(1);
expect(consoleMock.mock.calls[0][0].message).toContain( 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(null)).toBeNull();
expect(await getFileLastUpdate(undefined)).toBeNull(); expect(await getFileLastUpdate(undefined)).toBeNull();

View file

@ -6,12 +6,10 @@
*/ */
import shell from 'shelljs'; import shell from 'shelljs';
import execa from 'execa';
import path from 'path';
type FileLastUpdateData = {timestamp?: number; author?: string}; type FileLastUpdateData = {timestamp?: number; author?: string};
const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+), (.+)$/; const GIT_COMMIT_TIMESTAMP_AUTHOR_REGEX = /^(\d+),(.+)$/;
let showedGitRequirementError = false; let showedGitRequirementError = false;
@ -44,16 +42,11 @@ export async function getFileLastUpdate(
return null; return null;
} }
const fileBasename = path.basename(filePath); const result = shell.exec(`git log -1 --format=%ct,%an ${filePath}`);
const fileDirname = path.dirname(filePath); if (result.stderr) {
const {stdout} = await execa( throw new Error(result.stderr);
'git', }
['log', '-1', '--format=%ct, %an', fileBasename], return getTimestampAndAuthor(result.stdout.trim());
{
cwd: fileDirname,
},
);
return getTimestampAndAuthor(stdout);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }