mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-27 05:28:43 +02:00
polish(v2): improve docusaurus deploy logs (#3880)
* improve deploy logs * improve deploy logs
This commit is contained in:
parent
c4aeb1982c
commit
ec297cead0
1 changed files with 47 additions and 12 deletions
|
@ -8,12 +8,36 @@
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import shell from 'shelljs';
|
import shell from 'shelljs';
|
||||||
|
import chalk from 'chalk';
|
||||||
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 {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';
|
||||||
|
|
||||||
|
// GIT_PASS env variable should not appear in logs
|
||||||
|
function obfuscateGitPass(str) {
|
||||||
|
const gitPass = process.env.GIT_PASS;
|
||||||
|
return gitPass ? str.replace(gitPass, 'GIT_PASS') : str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log executed commands so that user can figure out mistakes on his own
|
||||||
|
// for example: https://github.com/facebook/docusaurus/issues/3875
|
||||||
|
function shellExecLog(cmd) {
|
||||||
|
try {
|
||||||
|
const result = shell.exec(cmd);
|
||||||
|
console.log(
|
||||||
|
`${chalk.cyan('CMD:')} ${obfuscateGitPass(cmd)} ${chalk.cyan(
|
||||||
|
`(code=${result.code})`,
|
||||||
|
)}`,
|
||||||
|
);
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`${chalk.red('CMD:')} ${obfuscateGitPass(cmd)}`);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default async function deploy(
|
export default async function deploy(
|
||||||
siteDir: string,
|
siteDir: string,
|
||||||
cliOptions: Partial<BuildCLIOptions> = {},
|
cliOptions: Partial<BuildCLIOptions> = {},
|
||||||
|
@ -48,6 +72,8 @@ export default async function deploy(
|
||||||
`Missing project organization name. Did you forget to define 'organizationName' in ${CONFIG_FILE_NAME}? You may also export it via the ORGANIZATION_NAME environment variable.`,
|
`Missing project organization name. Did you forget to define 'organizationName' in ${CONFIG_FILE_NAME}? You may also export it via the ORGANIZATION_NAME environment variable.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
console.log(`${chalk.cyan('organizationName:')} ${organizationName}`);
|
||||||
|
|
||||||
const projectName =
|
const projectName =
|
||||||
process.env.PROJECT_NAME ||
|
process.env.PROJECT_NAME ||
|
||||||
process.env.CIRCLE_PROJECT_REPONAME ||
|
process.env.CIRCLE_PROJECT_REPONAME ||
|
||||||
|
@ -57,6 +83,7 @@ export default async function deploy(
|
||||||
`Missing project name. Did you forget to define 'projectName' in ${CONFIG_FILE_NAME}? You may also export it via the PROJECT_NAME environment variable.`,
|
`Missing project name. Did you forget to define 'projectName' in ${CONFIG_FILE_NAME}? You may also export it via the PROJECT_NAME environment variable.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
console.log(`${chalk.cyan('projectName:')} ${projectName}`);
|
||||||
|
|
||||||
// We never deploy on pull request.
|
// We never deploy on pull request.
|
||||||
const isPullRequest =
|
const isPullRequest =
|
||||||
|
@ -70,6 +97,8 @@ export default async function deploy(
|
||||||
const deploymentBranch =
|
const deploymentBranch =
|
||||||
process.env.DEPLOYMENT_BRANCH ||
|
process.env.DEPLOYMENT_BRANCH ||
|
||||||
(projectName.indexOf('.github.io') !== -1 ? 'master' : 'gh-pages');
|
(projectName.indexOf('.github.io') !== -1 ? 'master' : 'gh-pages');
|
||||||
|
console.log(`${chalk.cyan('deploymentBranch:')} ${deploymentBranch}`);
|
||||||
|
|
||||||
const githubHost =
|
const githubHost =
|
||||||
process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com';
|
process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com';
|
||||||
|
|
||||||
|
@ -88,6 +117,10 @@ export default async function deploy(
|
||||||
? sshRemoteBranch
|
? sshRemoteBranch
|
||||||
: nonSshRemoteBranch;
|
: nonSshRemoteBranch;
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`,
|
||||||
|
);
|
||||||
|
|
||||||
// Check if this is a cross-repo publish.
|
// Check if this is a cross-repo publish.
|
||||||
const currentRepoUrl = shell
|
const currentRepoUrl = shell
|
||||||
.exec('git config --get remote.origin.url')
|
.exec('git config --get remote.origin.url')
|
||||||
|
@ -106,7 +139,7 @@ export default async function deploy(
|
||||||
|
|
||||||
// Save the commit hash that triggers publish-gh-pages before checking
|
// Save the commit hash that triggers publish-gh-pages before checking
|
||||||
// out to deployment branch.
|
// out to deployment branch.
|
||||||
const currentCommit = shell.exec('git rev-parse HEAD').stdout.trim();
|
const currentCommit = shellExecLog('git rev-parse HEAD').stdout.trim();
|
||||||
|
|
||||||
const runDeploy = (outputDirectory) => {
|
const runDeploy = (outputDirectory) => {
|
||||||
if (shell.cd(tempDir).code !== 0) {
|
if (shell.cd(tempDir).code !== 0) {
|
||||||
|
@ -116,8 +149,9 @@ export default async function deploy(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
shell.exec(`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`)
|
shellExecLog(
|
||||||
.code !== 0
|
`git clone ${remoteBranch} ${projectName}-${deploymentBranch}`,
|
||||||
|
).code !== 0
|
||||||
) {
|
) {
|
||||||
throw new Error('Error: git clone failed');
|
throw new Error('Error: git clone failed');
|
||||||
}
|
}
|
||||||
|
@ -131,23 +165,24 @@ export default async function deploy(
|
||||||
.exec('git rev-parse --abbrev-ref HEAD')
|
.exec('git rev-parse --abbrev-ref HEAD')
|
||||||
.stdout.trim();
|
.stdout.trim();
|
||||||
if (defaultBranch !== deploymentBranch) {
|
if (defaultBranch !== deploymentBranch) {
|
||||||
if (shell.exec(`git checkout origin/${deploymentBranch}`).code !== 0) {
|
if (shellExecLog(`git checkout origin/${deploymentBranch}`).code !== 0) {
|
||||||
if (
|
if (
|
||||||
shell.exec(`git checkout --orphan ${deploymentBranch}`).code !== 0
|
shellExecLog(`git checkout --orphan ${deploymentBranch}`).code !== 0
|
||||||
) {
|
) {
|
||||||
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
|
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
shell.exec(`git checkout -b ${deploymentBranch}`).code +
|
shellExecLog(`git checkout -b ${deploymentBranch}`).code +
|
||||||
shell.exec(`git branch --set-upstream-to=origin/${deploymentBranch}`)
|
shellExecLog(
|
||||||
.code !==
|
`git branch --set-upstream-to=origin/${deploymentBranch}`,
|
||||||
|
).code !==
|
||||||
0
|
0
|
||||||
) {
|
) {
|
||||||
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
|
throw new Error(`Error: Git checkout ${deploymentBranch} failed`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shell.exec('git rm -rf .');
|
shellExecLog('git rm -rf .');
|
||||||
|
|
||||||
shell.cd('../..');
|
shell.cd('../..');
|
||||||
|
|
||||||
|
@ -165,14 +200,14 @@ export default async function deploy(
|
||||||
}
|
}
|
||||||
|
|
||||||
shell.cd(toPath);
|
shell.cd(toPath);
|
||||||
shell.exec('git add --all');
|
shellExecLog('git add --all');
|
||||||
|
|
||||||
const commitMessage =
|
const commitMessage =
|
||||||
process.env.CUSTOM_COMMIT_MESSAGE ||
|
process.env.CUSTOM_COMMIT_MESSAGE ||
|
||||||
`Deploy website - based on ${currentCommit}`;
|
`Deploy website - based on ${currentCommit}`;
|
||||||
const commitResults = shell.exec(`git commit -m "${commitMessage}"`);
|
const commitResults = shellExecLog(`git commit -m "${commitMessage}"`);
|
||||||
if (
|
if (
|
||||||
shell.exec(`git push --force origin ${deploymentBranch}`).code !== 0
|
shellExecLog(`git push --force origin ${deploymentBranch}`).code !== 0
|
||||||
) {
|
) {
|
||||||
throw new Error('Error: Git push failed');
|
throw new Error('Error: Git push failed');
|
||||||
} else if (commitResults.code === 0) {
|
} else if (commitResults.code === 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue