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

Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
Alexander Sandor 2024-04-18 17:59:31 +02:00 committed by GitHub
parent 491af1fcae
commit a612b4eacf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 12 deletions

View file

@ -115,6 +115,10 @@ cli
'--skip-build', '--skip-build',
'skip building website before deploy it (default: false)', 'skip building website before deploy it (default: false)',
) )
.option(
'--target-dir <dir>',
'path to the target directory to deploy to (default: `.`)',
)
.action(deploy); .action(deploy);
/** /**

View file

@ -19,6 +19,7 @@ export type DeployCLIOptions = Pick<
'config' | 'locale' | 'outDir' 'config' | 'locale' | 'outDir'
> & { > & {
skipBuild?: boolean; skipBuild?: boolean;
targetDir?: string;
}; };
// GIT_PASS env variable should not appear in logs // 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 currentCommit = shellExecLog('git rev-parse HEAD').stdout.trim();
const runDeploy = async (outputDirectory: string) => { const runDeploy = async (outputDirectory: string) => {
const targetDirectory = cliOptions.targetDir ?? '.';
const fromPath = outputDirectory; const fromPath = outputDirectory;
const toPath = await fs.mkdtemp( const toPath = await fs.mkdtemp(
path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`),
); );
shell.cd(toPath); shell.cd(toPath);
// Check out deployment branch when cloning repository, and then remove all // Clones the repo into the temp folder and checks out the target branch.
// the files in the directory. If the 'clone' command fails, assume that // If the branch doesn't exist, it creates a new one based on the
// the deployment branch doesn't exist, and initialize git in an empty // repository default branch.
// directory, check out a clean deployment branch and add remote.
if ( if (
shellExecLog( shellExecLog(
`git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} "${toPath}"`, `git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} "${toPath}"`,
).code === 0 ).code !== 0
) { ) {
shellExecLog('git rm -rf .'); shellExecLog(`git clone --depth 1 ${deploymentRepoURL} "${toPath}"`);
} else {
shellExecLog('git init');
shellExecLog(`git checkout -b ${deploymentBranch}`); 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 { try {
await fs.copy(fromPath, toPath); await fs.copy(fromPath, targetPath);
} catch (err) { } 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; throw err;
} }
shellExecLog('git add --all'); shellExecLog('git add --all');
@ -254,7 +256,8 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
if (!cliOptions.skipBuild) { if (!cliOptions.skipBuild) {
// Build site, then push to deploymentBranch branch of specified repo. // Build site, then push to deploymentBranch branch of specified repo.
try { try {
await build(siteDir, cliOptions, false).then(() => runDeploy(outDir)); await build(siteDir, cliOptions, false);
await runDeploy(outDir);
} catch (err) { } catch (err) {
logger.error('Deployment of the build output failed.'); logger.error('Deployment of the build output failed.');
throw err; throw err;

View file

@ -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. | | `--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. | | `--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. | | `--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` | | `--config` | `undefined` | Path to Docusaurus config file, default to `[siteDir]/docusaurus.config.js` |
### `docusaurus serve [siteDir]` {#docusaurus-serve-sitedir} ### `docusaurus serve [siteDir]` {#docusaurus-serve-sitedir}