Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace unmaintained shelljs dependency by execa #10358

Merged
merged 19 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"@docusaurus/logger": "3.7.0",
"@docusaurus/utils": "3.7.0",
"commander": "^5.1.0",
"execa": "5.1.1",
"fs-extra": "^11.1.1",
"lodash": "^4.17.21",
"prompts": "^2.4.2",
"semver": "^7.5.4",
"shelljs": "^0.8.5",
"supports-color": "^9.4.0",
"tslib": "^2.6.0"
},
Expand Down
44 changes: 24 additions & 20 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {fileURLToPath} from 'url';
import path from 'path';
import _ from 'lodash';
import {logger} from '@docusaurus/logger';
import shell from 'shelljs';
import execa from 'execa';
import prompts, {type Choice} from 'prompts';
import supportsColor from 'supports-color';
import {escapeShellArg, askPreferredLanguage} from '@docusaurus/utils';
Expand Down Expand Up @@ -70,9 +70,9 @@ function findPackageManagerFromUserAgent(): PackageManager | undefined {
}

async function askForPackageManagerChoice(): Promise<PackageManager> {
const hasYarn = shell.exec('yarn --version', {silent: true}).code === 0;
const hasPnpm = shell.exec('pnpm --version', {silent: true}).code === 0;
const hasBun = shell.exec('bun --version', {silent: true}).code === 0;
const hasYarn = (await execa.command('yarn --version')).exitCode === 0;
const hasPnpm = (await execa.command('pnpm --version')).exitCode === 0;
const hasBun = (await execa.command('bun --version')).exitCode === 0;

if (!hasYarn && !hasPnpm && !hasBun) {
return 'npm';
Expand Down Expand Up @@ -533,7 +533,7 @@ export default async function init(
const gitCloneCommand = `${gitCommand} ${escapeShellArg(
source.url,
)} ${escapeShellArg(dest)}`;
if (shell.exec(gitCloneCommand).code !== 0) {
if (execa.command(gitCloneCommand).exitCode !== 0) {
logger.error`Cloning Git template failed!`;
process.exit(1);
}
Expand Down Expand Up @@ -583,24 +583,28 @@ export default async function init(
const cdpath = path.relative('.', dest);
const pkgManager = await getPackageManager(dest, cliOptions);
if (!cliOptions.skipInstall) {
shell.cd(dest);
process.chdir(dest);
logger.info`Installing dependencies with name=${pkgManager}...`;
// ...

if (
shell.exec(
pkgManager === 'yarn'
? 'yarn'
: pkgManager === 'bun'
? 'bun install'
: `${pkgManager} install --color always`,
{
env: {
...process.env,
// Force coloring the output, since the command is invoked by
// shelljs, which is not an interactive shell
...(supportsColor.stdout ? {FORCE_COLOR: '1'} : {}),
(
await execa.command(
pkgManager === 'yarn'
? 'yarn'
: pkgManager === 'bun'
? 'bun install'
: `${pkgManager} install --color always`,
{
env: {
...process.env,
// Force coloring the output, since the command is invoked by
// shelljs, which is not an interactive shell
...(supportsColor.stdout ? {FORCE_COLOR: '1'} : {}),
},
},
},
).code !== 0
)
).exitCode !== 0
) {
logger.error('Dependency installation failed.');
logger.info`The site directory has already been created, and you can retry by typing:
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-content-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
"@types/js-yaml": "^4.0.5",
"@types/picomatch": "^2.3.0",
"commander": "^5.1.0",
"picomatch": "^2.3.1",
"shelljs": "^0.8.5"
"picomatch": "^2.3.1"
},
"peerDependencies": {
"react": "^18.0.0 || ^19.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@docusaurus/types": "3.7.0",
"@docusaurus/utils-common": "3.7.0",
"escape-string-regexp": "^4.0.0",
"execa": "5.1.1",
"file-loader": "^6.2.0",
"fs-extra": "^11.1.1",
"github-slugger": "^1.5.0",
Expand All @@ -33,7 +34,6 @@
"micromatch": "^4.0.5",
"prompts": "^2.4.2",
"resolve-pathname": "^3.0.0",
"shelljs": "^0.8.5",
"tslib": "^2.6.0",
"url-loader": "^4.1.1",
"utility-types": "^3.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(() => {});
Expand Down
34 changes: 13 additions & 21 deletions packages/docusaurus-utils/src/gitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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}`,
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus/bin/beforeCli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import fs from 'fs-extra';
import path from 'path';
import {createRequire} from 'module';
import shell from 'shelljs';
import execa from 'execa';
import {logger} from '@docusaurus/logger';
import semver from 'semver';
import updateNotifier from 'update-notifier';
Expand Down Expand Up @@ -111,8 +111,8 @@ export default async function beforeCli() {
return undefined;
}

const yarnVersionResult = shell.exec('yarn --version', {silent: true});
if (yarnVersionResult?.code === 0) {
const yarnVersionResult = await execa.command('yarn --version');
if (yarnVersionResult.exitCode === 0) {
const majorVersion = parseInt(
yarnVersionResult.stdout?.trim().split('.')[0] ?? '',
10,
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"escape-html": "^1.0.3",
"eta": "^2.2.0",
"eval": "^0.1.8",
"execa": "5.1.1",
"fs-extra": "^11.1.1",
"html-tags": "^3.3.1",
"html-webpack-plugin": "^5.6.0",
Expand All @@ -68,7 +69,6 @@
"react-router-dom": "^5.3.4",
"semver": "^7.5.4",
"serve-handler": "^6.1.6",
"shelljs": "^0.8.5",
"tinypool": "^1.0.2",
"tslib": "^2.6.0",
"update-notifier": "^6.0.2",
Expand Down
Loading