Skip to content

Commit

Permalink
feat(cli): docusaurus deploy should support a --target-dir option (#9767
Browse files Browse the repository at this point in the history
)

Co-authored-by: sebastien <lorber.sebastien@gmail.com>
  • Loading branch information
SandPod and slorber committed Apr 18, 2024
1 parent 491af1f commit a612b4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Expand Up @@ -115,6 +115,10 @@ cli
'--skip-build',
'skip building website before deploy it (default: false)',
)
.option(
'--target-dir <dir>',
'path to the target directory to deploy to (default: `.`)',
)
.action(deploy);

/**
Expand Down
27 changes: 15 additions & 12 deletions packages/docusaurus/src/commands/deploy.ts
Expand Up @@ -19,6 +19,7 @@ export type DeployCLIOptions = Pick<
'config' | 'locale' | 'outDir'
> & {
skipBuild?: boolean;
targetDir?: string;
};

// GIT_PASS env variable should not appear in logs
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions website/docs/cli.mdx
Expand Up @@ -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}
Expand Down

0 comments on commit a612b4e

Please sign in to comment.