Skip to content

Commit

Permalink
feat(v2): allow deploy without building website (#2770)
Browse files Browse the repository at this point in the history
* feat(v2): allow deploy without building website

* Update cli.md
  • Loading branch information
lex111 authored May 27, 2020
1 parent 90db536 commit 082c621
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 81 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface BuildCLIOptions {
bundleAnalyzer: boolean;
outDir: string;
minify: boolean;
skipBuild: boolean;
}

export interface LoadContext {
Expand Down
8 changes: 6 additions & 2 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ cli
'--out-dir <dir>',
'The full path for the new output directory, relative to the current workspace (default: build).',
)
.action((siteDir = '.', {outDir}) => {
wrapCommand(deploy)(path.resolve(siteDir), {outDir});
.option(
'--skip-build',
'Skip building website before deploy it (default: false)',
)
.action((siteDir = '.', {outDir, skipBuild}) => {
wrapCommand(deploy)(path.resolve(siteDir), {outDir, skipBuild});
});

cli
Expand Down
170 changes: 91 additions & 79 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs-extra';
import path from 'path';
import shell from 'shelljs';
import {CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME} from '../constants';
import {loadContext} from '../server';
import {loadConfig} from '../server/config';
import {build} from './build';
import {BuildCLIOptions} from '@docusaurus/types';
Expand All @@ -17,6 +18,9 @@ export async function deploy(
siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> {
const {outDir} = loadContext(siteDir, cliOptions.outDir);
const tempDir = path.join(siteDir, GENERATED_FILES_DIR_NAME);

console.log('Deploy command invoked ...');
if (!shell.which('git')) {
throw new Error('Git not installed or on the PATH!');
Expand Down Expand Up @@ -94,95 +98,103 @@ export async function deploy(
// out to deployment branch.
const currentCommit = shell.exec('git rev-parse HEAD').stdout.trim();

// Clear Docusaurus 2 cache dir for deploy consistency.
const tempDir = path.join(siteDir, '.docusaurus');
fs.removeSync(tempDir);

// Build static html files, then push to deploymentBranch branch of specified repo.
build(siteDir, cliOptions, false)
.then((outDir) => {
shell.cd(tempDir);

if (
shell.exec(
`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`,
).code !== 0
) {
throw new Error('Error: git clone failed');
}

shell.cd(`${projectName}-${deploymentBranch}`);

// If the default branch is the one we're deploying to, then we'll fail
// to create it. This is the case of a cross-repo publish, where we clone
// a github.io repo with a default master branch.
const defaultBranch = shell
.exec('git rev-parse --abbrev-ref HEAD')
.stdout.trim();
if (defaultBranch !== deploymentBranch) {
if (shell.exec(`git checkout origin/${deploymentBranch}`).code !== 0) {
if (
shell.exec(`git checkout --orphan ${deploymentBranch}`).code !== 0
) {
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
}
} else if (
shell.exec(`git checkout -b ${deploymentBranch}`).code +
shell.exec(
`git branch --set-upstream-to=origin/${deploymentBranch}`,
).code !==
0
const runDeploy = (outDir) => {
if (shell.cd(tempDir).code !== 0) {
throw new Error(
`Temp dir ${GENERATED_FILES_DIR_NAME} does not exists. Run build website first.`,
);
}

if (
shell.exec(`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`)
.code !== 0
) {
throw new Error('Error: git clone failed');
}

shell.cd(`${projectName}-${deploymentBranch}`);

// If the default branch is the one we're deploying to, then we'll fail
// to create it. This is the case of a cross-repo publish, where we clone
// a github.io repo with a default master branch.
const defaultBranch = shell
.exec('git rev-parse --abbrev-ref HEAD')
.stdout.trim();
if (defaultBranch !== deploymentBranch) {
if (shell.exec(`git checkout origin/${deploymentBranch}`).code !== 0) {
if (
shell.exec(`git checkout --orphan ${deploymentBranch}`).code !== 0
) {
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
}
} else if (
shell.exec(`git checkout -b ${deploymentBranch}`).code +
shell.exec(`git branch --set-upstream-to=origin/${deploymentBranch}`)
.code !==
0
) {
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
}
}

shell.exec('git rm -rf .');
shell.exec('git rm -rf .');

shell.cd('../..');
shell.cd('../..');

const fromPath = outDir;
const toPath = path.join(
GENERATED_FILES_DIR_NAME,
`${projectName}-${deploymentBranch}`,
);
const fromPath = outDir;
const toPath = path.join(
GENERATED_FILES_DIR_NAME,
`${projectName}-${deploymentBranch}`,
);

fs.copy(fromPath, toPath, (error) => {
if (error) {
throw new Error(
`Error: Copying build assets failed with error '${error}'`,
);
}
fs.copy(fromPath, toPath, (error) => {
if (error) {
throw new Error(
`Error: Copying build assets failed with error '${error}'`,
);
}

shell.cd(toPath);
shell.exec('git add --all');
shell.cd(toPath);
shell.exec('git add --all');

const commitMessage =
process.env.CUSTOM_COMMIT_MESSAGE ||
`Deploy website - based on ${currentCommit}`;
const commitResults = shell.exec(`git commit -m "${commitMessage}"`);
if (
shell.exec(`git push --force origin ${deploymentBranch}`).code !== 0
) {
throw new Error('Error: Git push failed');
} else if (commitResults.code === 0) {
// The commit might return a non-zero value when site is up to date.
let websiteURL = '';
if (githubHost === 'github.com') {
websiteURL = projectName.includes('.github.io')
? `https://${organizationName}.github.io/`
: `https://${organizationName}.github.io/${projectName}/`;
} else {
// GitHub enterprise hosting.
websiteURL = `https://${githubHost}/pages/${organizationName}/${projectName}/`;
}
shell.echo(`Website is live at ${websiteURL}`);
shell.exit(0);
const commitMessage =
process.env.CUSTOM_COMMIT_MESSAGE ||
`Deploy website - based on ${currentCommit}`;
const commitResults = shell.exec(`git commit -m "${commitMessage}"`);
if (
shell.exec(`git push --force origin ${deploymentBranch}`).code !== 0
) {
throw new Error('Error: Git push failed');
} else if (commitResults.code === 0) {
// The commit might return a non-zero value when site is up to date.
let websiteURL = '';
if (githubHost === 'github.com') {
websiteURL = projectName.includes('.github.io')
? `https://${organizationName}.github.io/`
: `https://${organizationName}.github.io/${projectName}/`;
} else {
// GitHub enterprise hosting.
websiteURL = `https://${githubHost}/pages/${organizationName}/${projectName}/`;
}
});
})
.catch((buildError) => {
console.error(buildError);
process.exit(1);
shell.echo(`Website is live at ${websiteURL}`);
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) => {
console.error(buildError);
process.exit(1);
});
} else {
// Push current build to deploymentBranch branch of specified repo.
runDeploy(outDir);
}
}
1 change: 1 addition & 0 deletions website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ Deploys your site with [GitHub Pages](https://pages.github.com/). Check out the
| Name | Default | Description |
| --- | --- | --- |
| `--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. |

0 comments on commit 082c621

Please sign in to comment.