From a612b4eacf92215750fd12f7d721c2d59504c8e4 Mon Sep 17 00:00:00 2001 From: Alexander Sandor <137198655+SandPod@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:59:31 +0200 Subject: [PATCH] feat(cli): docusaurus deploy should support a --target-dir option (#9767) Co-authored-by: sebastien --- packages/docusaurus/bin/docusaurus.mjs | 4 ++++ packages/docusaurus/src/commands/deploy.ts | 27 ++++++++++++---------- website/docs/cli.mdx | 1 + 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index cd53d42d5952..7429f4f4156e 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -115,6 +115,10 @@ cli '--skip-build', 'skip building website before deploy it (default: false)', ) + .option( + '--target-dir ', + 'path to the target directory to deploy to (default: `.`)', + ) .action(deploy); /** diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index e16f4881552b..2e23a8e3efb1 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -19,6 +19,7 @@ export type DeployCLIOptions = Pick< 'config' | 'locale' | 'outDir' > & { skipBuild?: boolean; + targetDir?: string; }; // GIT_PASS env variable should not appear in logs @@ -185,32 +186,33 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); const currentCommit = shellExecLog('git rev-parse HEAD').stdout.trim(); const runDeploy = async (outputDirectory: string) => { + const targetDirectory = cliOptions.targetDir ?? '.'; const fromPath = outputDirectory; const toPath = await fs.mkdtemp( path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), ); shell.cd(toPath); - // Check out deployment branch when cloning repository, and then remove all - // the files in the directory. If the 'clone' command fails, assume that - // the deployment branch doesn't exist, and initialize git in an empty - // directory, check out a clean deployment branch and add remote. + // Clones the repo into the temp folder and checks out the target branch. + // If the branch doesn't exist, it creates a new one based on the + // repository default branch. if ( shellExecLog( `git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} "${toPath}"`, - ).code === 0 + ).code !== 0 ) { - shellExecLog('git rm -rf .'); - } else { - shellExecLog('git init'); + shellExecLog(`git clone --depth 1 ${deploymentRepoURL} "${toPath}"`); shellExecLog(`git checkout -b ${deploymentBranch}`); - shellExecLog(`git remote add origin ${deploymentRepoURL}`); } + // Clear out any existing contents in the target directory + shellExecLog(`git rm -rf ${targetDirectory}`); + + const targetPath = path.join(toPath, targetDirectory); try { - await fs.copy(fromPath, toPath); + await fs.copy(fromPath, targetPath); } catch (err) { - logger.error`Copying build assets from path=${fromPath} to path=${toPath} failed.`; + logger.error`Copying build assets from path=${fromPath} to path=${targetPath} failed.`; throw err; } shellExecLog('git add --all'); @@ -254,7 +256,8 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); if (!cliOptions.skipBuild) { // Build site, then push to deploymentBranch branch of specified repo. try { - await build(siteDir, cliOptions, false).then(() => runDeploy(outDir)); + await build(siteDir, cliOptions, false); + await runDeploy(outDir); } catch (err) { logger.error('Deployment of the build output failed.'); throw err; diff --git a/website/docs/cli.mdx b/website/docs/cli.mdx index 369779788afa..5be24e5191b5 100644 --- a/website/docs/cli.mdx +++ b/website/docs/cli.mdx @@ -144,6 +144,7 @@ Deploys your site with [GitHub Pages](https://pages.github.com/). Check out the | `--locale` | | Deploy the site in the specified locale. If not specified, all known locales are deployed. | | `--out-dir` | `build` | The full path for the new output directory, relative to the current workspace. | | `--skip-build` | `false` | Deploy website without building it. This may be useful when using a custom deploy script. | +| `--target-dir` | `.` | Path to the target directory to deploy to. | | `--config` | `undefined` | Path to Docusaurus config file, default to `[siteDir]/docusaurus.config.js` | ### `docusaurus serve [siteDir]` {#docusaurus-serve-sitedir}