mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-21 12:08:03 +02:00
refactor: replace unmaintained shelljs dependency by execa (#10358)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
a6ef3897e0
commit
7f4a37949e
10 changed files with 139 additions and 85 deletions
|
@ -9,12 +9,13 @@ import {jest} from '@jest/globals';
|
|||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {createTempRepo} from '@testing-utils/git';
|
||||
import shell from 'shelljs';
|
||||
import execa from 'execa';
|
||||
|
||||
import {
|
||||
getGitLastUpdate,
|
||||
LAST_UPDATE_FALLBACK,
|
||||
readLastUpdateData,
|
||||
} from '@docusaurus/utils';
|
||||
} from '../lastUpdateUtils';
|
||||
|
||||
describe('getGitLastUpdate', () => {
|
||||
const {repoDir} = createTempRepo();
|
||||
|
@ -69,7 +70,10 @@ describe('getGitLastUpdate', () => {
|
|||
});
|
||||
|
||||
it('git does not exist', async () => {
|
||||
const mock = jest.spyOn(shell, 'which').mockImplementationOnce(() => null);
|
||||
const mock = jest.spyOn(execa, 'sync').mockImplementationOnce(() => {
|
||||
throw new Error('Git does not exist');
|
||||
});
|
||||
|
||||
const consoleMock = jest
|
||||
.spyOn(console, 'warn')
|
||||
.mockImplementation(() => {});
|
||||
|
|
|
@ -8,9 +8,15 @@
|
|||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import _ from 'lodash';
|
||||
import shell from 'shelljs'; // TODO replace with async-first version
|
||||
import execa from 'execa';
|
||||
|
||||
const realHasGitFn = () => !!shell.which('git');
|
||||
const realHasGitFn = () => {
|
||||
try {
|
||||
return execa.sync('git', ['--version']).exitCode === 0;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// The hasGit call is synchronous IO so we memoize it
|
||||
// The user won't install Git in the middle of a build anyway...
|
||||
|
@ -123,27 +129,13 @@ export async function getFileCommitDate(
|
|||
file,
|
||||
)}"`;
|
||||
|
||||
const result = await new Promise<{
|
||||
code: number;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}>((resolve) => {
|
||||
shell.exec(
|
||||
command,
|
||||
{
|
||||
// Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
||||
cwd: path.dirname(file),
|
||||
silent: true,
|
||||
},
|
||||
(code, stdout, stderr) => {
|
||||
resolve({code, stdout, stderr});
|
||||
},
|
||||
);
|
||||
const result = await execa(command, {
|
||||
cwd: path.dirname(file),
|
||||
shell: true,
|
||||
});
|
||||
|
||||
if (result.code !== 0) {
|
||||
if (result.exitCode !== 0) {
|
||||
throw new Error(
|
||||
`Failed to retrieve the git history for file "${file}" with exit code ${result.code}: ${result.stderr}`,
|
||||
`Failed to retrieve the git history for file "${file}" with exit code ${result.exitCode}: ${result.stderr}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue