feat(v2): allow deploy without building website (#2770)

* feat(v2): allow deploy without building website

* Update cli.md
This commit is contained in:
Alexey Pyltsyn 2020-05-27 17:34:41 +03:00 committed by GitHub
parent 90db53657e
commit 082c6212cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 82 deletions

View file

@ -67,6 +67,7 @@ export interface BuildCLIOptions {
bundleAnalyzer: boolean; bundleAnalyzer: boolean;
outDir: string; outDir: string;
minify: boolean; minify: boolean;
skipBuild: boolean;
} }
export interface LoadContext { export interface LoadContext {

View file

@ -71,8 +71,12 @@ cli
'--out-dir <dir>', '--out-dir <dir>',
'The full path for the new output directory, relative to the current workspace (default: build).', 'The full path for the new output directory, relative to the current workspace (default: build).',
) )
.action((siteDir = '.', {outDir}) => { .option(
wrapCommand(deploy)(path.resolve(siteDir), {outDir}); '--skip-build',
'Skip building website before deploy it (default: false)',
)
.action((siteDir = '.', {outDir, skipBuild}) => {
wrapCommand(deploy)(path.resolve(siteDir), {outDir, skipBuild});
}); });
cli cli

View file

@ -9,6 +9,7 @@ import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import shell from 'shelljs'; import shell from 'shelljs';
import {CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME} from '../constants'; import {CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME} from '../constants';
import {loadContext} from '../server';
import {loadConfig} from '../server/config'; import {loadConfig} from '../server/config';
import {build} from './build'; import {build} from './build';
import {BuildCLIOptions} from '@docusaurus/types'; import {BuildCLIOptions} from '@docusaurus/types';
@ -17,6 +18,9 @@ export async function deploy(
siteDir: string, siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {}, cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> { ): Promise<void> {
const {outDir} = loadContext(siteDir, cliOptions.outDir);
const tempDir = path.join(siteDir, GENERATED_FILES_DIR_NAME);
console.log('Deploy command invoked ...'); console.log('Deploy command invoked ...');
if (!shell.which('git')) { if (!shell.which('git')) {
throw new Error('Git not installed or on the PATH!'); throw new Error('Git not installed or on the PATH!');
@ -94,19 +98,16 @@ export async function deploy(
// out to deployment branch. // out to deployment branch.
const currentCommit = shell.exec('git rev-parse HEAD').stdout.trim(); const currentCommit = shell.exec('git rev-parse HEAD').stdout.trim();
// Clear Docusaurus 2 cache dir for deploy consistency. const runDeploy = (outDir) => {
const tempDir = path.join(siteDir, '.docusaurus'); if (shell.cd(tempDir).code !== 0) {
fs.removeSync(tempDir); throw new Error(
`Temp dir ${GENERATED_FILES_DIR_NAME} does not exists. Run build website first.`,
// Build static html files, then push to deploymentBranch branch of specified repo. );
build(siteDir, cliOptions, false) }
.then((outDir) => {
shell.cd(tempDir);
if ( if (
shell.exec( shell.exec(`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`)
`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`, .code !== 0
).code !== 0
) { ) {
throw new Error('Error: git clone failed'); throw new Error('Error: git clone failed');
} }
@ -128,9 +129,8 @@ export async function deploy(
} }
} else if ( } else if (
shell.exec(`git checkout -b ${deploymentBranch}`).code + shell.exec(`git checkout -b ${deploymentBranch}`).code +
shell.exec( shell.exec(`git branch --set-upstream-to=origin/${deploymentBranch}`)
`git branch --set-upstream-to=origin/${deploymentBranch}`, .code !==
).code !==
0 0
) { ) {
throw new Error(`Error: Git checkout ${deploymentBranch} failed`); throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
@ -180,9 +180,21 @@ export async function deploy(
shell.exit(0); shell.exit(0);
} }
}); });
}) };
if (!cliOptions.skipBuild) {
// Clear Docusaurus 2 cache dir for deploy consistency.
fs.removeSync(tempDir);
// Build static html files, then push to deploymentBranch branch of specified repo.
build(siteDir, cliOptions, false)
.then(runDeploy)
.catch((buildError) => { .catch((buildError) => {
console.error(buildError); console.error(buildError);
process.exit(1); process.exit(1);
}); });
} else {
// Push current build to deploymentBranch branch of specified repo.
runDeploy(outDir);
}
} }

View file

@ -110,3 +110,4 @@ Deploys your site with [GitHub Pages](https://pages.github.com/). Check out the
| Name | Default | Description | | Name | Default | Description |
| --- | --- | --- | | --- | --- | --- |
| `--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 custom deploy script. |