From 85603fae7326c02b19a120e544295c174f790c20 Mon Sep 17 00:00:00 2001 From: Alexander Sandor Date: Sun, 21 Jan 2024 10:15:11 +0100 Subject: [PATCH 1/5] fix: Add default target dir constant. --- packages/docusaurus-utils/src/constants.ts | 5 +++++ packages/docusaurus-utils/src/index.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index 5039d6989b00..aa2fd516ead9 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -27,6 +27,11 @@ export const DOCUSAURUS_VERSION = */ export const DEFAULT_BUILD_DIR_NAME = 'build'; +/** + * Can be overridden with cli option `--target-dir`. + */ +export const DEFAULT_TARGET_DIR = '.'; + /** * Can be overridden with cli option `--config`. Code should generally use * `context.siteConfigPath` instead (which is always absolute). diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 6db01244d006..d3db40f70db9 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -10,6 +10,7 @@ export { NODE_MINOR_VERSION, DOCUSAURUS_VERSION, DEFAULT_BUILD_DIR_NAME, + DEFAULT_TARGET_DIR, DEFAULT_CONFIG_FILE_NAME, BABEL_CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME, From e935cf402733cd848022eb144ea005280ba6ee7d Mon Sep 17 00:00:00 2001 From: Alexander Sandor Date: Sun, 21 Jan 2024 10:57:06 +0100 Subject: [PATCH 2/5] fix: add targetDir as deploy CLI option. --- packages/docusaurus-types/src/context.d.ts | 1 + packages/docusaurus/bin/docusaurus.mjs | 4 ++++ packages/docusaurus/src/server/index.ts | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/packages/docusaurus-types/src/context.d.ts b/packages/docusaurus-types/src/context.d.ts index e05f8a9a3256..08ed2bc36029 100644 --- a/packages/docusaurus-types/src/context.d.ts +++ b/packages/docusaurus-types/src/context.d.ts @@ -35,6 +35,7 @@ export type LoadContext = { siteConfig: DocusaurusConfig; siteConfigPath: string; outDir: string; + targetDir: string; /** * Directory where all source translations for the current locale can be found * in. Constructed with `i18n.path` + `i18n.currentLocale.path` (e.g. diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index b65915bcd6bd..da7676d7dbae 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -112,6 +112,10 @@ cli '--skip-build', 'skip building website before deploy it (default: false)', ) + .option( + '--target-dir ', + 'path to the target directory to deploy (default: `.`)', + ) .action(deploy); /** diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index ab50c1883d31..1d3f38b89cd9 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -12,6 +12,7 @@ import { escapePath, localizePath, DEFAULT_BUILD_DIR_NAME, + DEFAULT_TARGET_DIR, DEFAULT_CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME, } from '@docusaurus/utils'; @@ -37,6 +38,8 @@ export type LoadContextOptions = { config?: string; /** Default is `i18n.defaultLocale` */ locale?: string; + /** Custom target directory. Can be customized with `--target-dir` option */ + targetDir?: string; /** * `true` means the paths will have the locale prepended; `false` means they * won't (useful for `yarn build -l zh-Hans` where the output should be @@ -60,6 +63,7 @@ export async function loadContext( outDir: baseOutDir = DEFAULT_BUILD_DIR_NAME, locale, config: customConfigFilePath, + targetDir: baseTargetDir = DEFAULT_TARGET_DIR, } = options; const generatedFilesDir = path.resolve(siteDir, GENERATED_FILES_DIR_NAME); @@ -83,6 +87,8 @@ export async function loadContext( pathType: 'fs', }); + const targetDir = baseTargetDir; + const siteConfig: DocusaurusConfig = {...initialSiteConfig, baseUrl}; const localizationDir = path.resolve( @@ -107,6 +113,7 @@ export async function loadContext( siteConfig, siteConfigPath, outDir, + targetDir, baseUrl, i18n, codeTranslations, @@ -127,6 +134,7 @@ export async function load(options: LoadContextOptions): Promise { siteConfig, siteConfigPath, outDir, + targetDir, baseUrl, i18n, localizationDir, @@ -251,6 +259,7 @@ ${Object.entries(registry) siteMetadata, siteDir, outDir, + targetDir, baseUrl, i18n, localizationDir, From b787b1daa0f0b9d506762356f5a1d38361215e39 Mon Sep 17 00:00:00 2001 From: Alexander Sandor Date: Sun, 21 Jan 2024 10:58:16 +0100 Subject: [PATCH 3/5] feat: Allow defining the target folder for deploy command. --- packages/docusaurus/src/commands/deploy.ts | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 818cdb13e7ef..d7086e25e25b 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -16,7 +16,7 @@ import {build} from './build'; export type DeployCLIOptions = Pick< LoadContextOptions, - 'config' | 'locale' | 'outDir' + 'config' | 'locale' | 'outDir' | 'targetDir' > & { skipBuild?: boolean; }; @@ -46,10 +46,11 @@ export async function deploy( ): Promise { const siteDir = await fs.realpath(siteDirParam); - const {outDir, siteConfig, siteConfigPath} = await loadContext({ + const {outDir, siteConfig, siteConfigPath, targetDir} = await loadContext({ siteDir, config: cliOptions.config, outDir: cliOptions.outDir, + targetDir: cliOptions.targetDir, }); if (typeof siteConfig.trailingSlash === 'undefined') { @@ -184,33 +185,36 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); // out to deployment branch. const currentCommit = shellExecLog('git rev-parse HEAD').stdout.trim(); - const runDeploy = async (outputDirectory: string) => { + const runDeploy = async ( + outputDirectory: string, + targetDirectory: string, + ) => { 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,13 +258,15 @@ 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); + await build(siteDir, cliOptions, false).then((buildDir) => + runDeploy(buildDir, targetDir), + ); } catch (err) { logger.error('Deployment of the build output failed.'); throw err; } } else { // Push current build to deploymentBranch branch of specified repo. - await runDeploy(outDir); + await runDeploy(outDir, targetDir); } } From ac5e95a3f12c4d7ed4e4965028c88563bd2f07c6 Mon Sep 17 00:00:00 2001 From: sebastien Date: Thu, 18 Apr 2024 17:18:04 +0200 Subject: [PATCH 4/5] targetDir does not need to be part of the load context, it's just a CLI option --- packages/docusaurus-types/src/context.d.ts | 1 - packages/docusaurus-utils/src/constants.ts | 5 ----- packages/docusaurus-utils/src/index.ts | 1 - packages/docusaurus/src/commands/deploy.ts | 6 +++--- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/docusaurus-types/src/context.d.ts b/packages/docusaurus-types/src/context.d.ts index 1b88a8844fb5..68c4f78d6ecd 100644 --- a/packages/docusaurus-types/src/context.d.ts +++ b/packages/docusaurus-types/src/context.d.ts @@ -36,7 +36,6 @@ export type LoadContext = { siteConfig: DocusaurusConfig; siteConfigPath: string; outDir: string; - targetDir: string; /** * Directory where all source translations for the current locale can be found * in. Constructed with `i18n.path` + `i18n.currentLocale.path` (e.g. diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index aa2fd516ead9..5039d6989b00 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -27,11 +27,6 @@ export const DOCUSAURUS_VERSION = */ export const DEFAULT_BUILD_DIR_NAME = 'build'; -/** - * Can be overridden with cli option `--target-dir`. - */ -export const DEFAULT_TARGET_DIR = '.'; - /** * Can be overridden with cli option `--config`. Code should generally use * `context.siteConfigPath` instead (which is always absolute). diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 413bcc6af903..405da5258dd4 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -10,7 +10,6 @@ export { NODE_MINOR_VERSION, DOCUSAURUS_VERSION, DEFAULT_BUILD_DIR_NAME, - DEFAULT_TARGET_DIR, DEFAULT_CONFIG_FILE_NAME, BABEL_CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME, diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 91a03f8399d0..2e23a8e3efb1 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -47,7 +47,7 @@ export async function deploy( ): Promise { const siteDir = await fs.realpath(siteDirParam); - const {outDir, siteConfig, siteConfigPath, targetDir} = await loadContext({ + const {outDir, siteConfig, siteConfigPath} = await loadContext({ siteDir, config: cliOptions.config, outDir: cliOptions.outDir, @@ -186,7 +186,7 @@ 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 targetDirectory = cliOptions.targetDir ?? '.'; const fromPath = outputDirectory; const toPath = await fs.mkdtemp( path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), @@ -264,6 +264,6 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); } } else { // Push current build to deploymentBranch branch of specified repo. - await runDeploy(outDir, targetDir); + await runDeploy(outDir); } } From bd669466b6f1ec8fc024957d7ff9e3de4f6ba9a3 Mon Sep 17 00:00:00 2001 From: sebastien Date: Thu, 18 Apr 2024 17:33:16 +0200 Subject: [PATCH 5/5] docs --- packages/docusaurus/bin/docusaurus.mjs | 2 +- website/docs/cli.mdx | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 7fa94561c091..7429f4f4156e 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -117,7 +117,7 @@ cli ) .option( '--target-dir ', - 'path to the target directory to deploy (default: `.`)', + 'path to the target directory to deploy to (default: `.`)', ) .action(deploy); 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}