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

feat(cli): docusaurus deploy should support a --target-dir option #9767

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
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 (default: `.`)',
)
.action(deploy);

/**
Expand Down
27 changes: 15 additions & 12 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'config' | 'locale' | 'outDir'
> & {
skipBuild?: boolean;
targetDir?: string;
};

// GIT_PASS env variable should not appear in logs
Expand Down Expand Up @@ -185,32 +186,33 @@
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}`);

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.

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 @@
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
Loading