refactor: unify log format with new logger utility (#5994)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Joshua Chen 2021-12-21 00:24:59 +08:00 committed by GitHub
parent faef753730
commit 770418f8d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 717 additions and 650 deletions

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import fs from 'fs-extra';
import {execSync} from 'child_process';
import prompts, {Choice} from 'prompts';
@ -131,12 +131,14 @@ export default async function init(
}
if (!name) {
throw new Error(chalk.red('A website name is required.'));
logger.error('A website name is required.');
process.exit(1);
}
const dest = path.resolve(rootDir, name);
if (fs.existsSync(dest)) {
throw new Error(`Directory already exists at "${dest}"!`);
logger.error`Directory already exists at path=${dest}!`;
process.exit(1);
}
let template = reqTemplate;
@ -171,10 +173,10 @@ export default async function init(
if (url && isValidGitRepoUrl(url)) {
return true;
}
return chalk.red(`Invalid repository URL`);
return logger.red('Invalid repository URL');
},
message:
'Enter a repository URL from GitHub, Bitbucket, GitLab, or any other public repo.\n(e.g: https://github.com/ownerName/repoName.git)',
message: logger.interpolate`Enter a repository URL from GitHub, Bitbucket, GitLab, or any other public repo.
(e.g: path=${'https://github.com/ownerName/repoName.git'})`,
});
template = repoPrompt.gitRepoUrl;
} else if (template === 'Local template') {
@ -187,11 +189,11 @@ export default async function init(
if (fs.existsSync(fullDir)) {
return true;
}
return chalk.red(
`The path ${chalk.magenta(fullDir)} does not exist.`,
return logger.red(
logger.interpolate`path=${fullDir} does not exist.`,
);
}
return chalk.red('Please enter a valid path.');
return logger.red('Please enter a valid path.');
},
message:
'Enter a local folder path, relative to the current working directory.',
@ -200,37 +202,34 @@ export default async function init(
}
if (!template) {
throw new Error('Template should not be empty');
logger.error('Template should not be empty');
process.exit(1);
}
console.log(`
${chalk.cyan('Creating new Docusaurus project...')}
`);
logger.info('Creating new Docusaurus project...');
if (isValidGitRepoUrl(template)) {
console.log(`Cloning Git template ${chalk.cyan(template)}...`);
logger.info`Cloning Git template path=${template}...`;
if (
shell.exec(`git clone --recursive ${template} ${dest}`, {silent: true})
.code !== 0
) {
throw new Error(chalk.red(`Cloning Git template ${template} failed!`));
logger.error`Cloning Git template name=${template} failed!`;
process.exit(1);
}
} else if (templates.includes(template)) {
// Docusaurus templates.
if (useTS) {
if (!hasTS(template)) {
throw new Error(
`Template ${template} doesn't provide the Typescript variant.`,
);
logger.error`Template name=${template} doesn't provide the Typescript variant.`;
process.exit(1);
}
template = `${template}${TypeScriptTemplateSuffix}`;
}
try {
await copyTemplate(templatesDir, template, dest);
} catch (err) {
console.log(
`Copying Docusaurus template ${chalk.cyan(template)} failed!`,
);
logger.error`Copying Docusaurus template name=${template} failed!`;
throw err;
}
} else if (fs.existsSync(path.resolve(process.cwd(), template))) {
@ -238,11 +237,12 @@ ${chalk.cyan('Creating new Docusaurus project...')}
try {
await fs.copy(templateDir, dest);
} catch (err) {
console.log(`Copying local template ${templateDir} failed!`);
logger.error`Copying local template path=${templateDir} failed!`;
throw err;
}
} else {
throw new Error('Invalid template.');
logger.error('Invalid template.');
process.exit(1);
}
// Update package.json info.
@ -253,7 +253,7 @@ ${chalk.cyan('Creating new Docusaurus project...')}
private: true,
});
} catch (err) {
console.log(chalk.red('Failed to update package.json.'));
logger.error('Failed to update package.json.');
throw err;
}
@ -275,7 +275,7 @@ ${chalk.cyan('Creating new Docusaurus project...')}
? name
: path.relative(process.cwd(), name);
if (!cliOptions.skipInstall) {
console.log(`Installing dependencies with ${chalk.cyan(pkgManager)}...`);
logger.info`Installing dependencies with name=${pkgManager}...`;
if (
shell.exec(
`cd "${name}" && ${useYarn ? 'yarn' : 'npm install --color always'}`,
@ -288,36 +288,35 @@ ${chalk.cyan('Creating new Docusaurus project...')}
},
).code !== 0
) {
console.error(chalk.red('Dependency installation failed.'));
console.log(`The site directory has already been created, and you can retry by typing:
logger.error('Dependency installation failed.');
logger.info`The site directory has already been created, and you can retry by typing:
${chalk.cyan('cd')} ${cdpath}
${chalk.cyan(`${pkgManager} install`)}`);
code=${`cd ${cdpath}`}
code=${`${pkgManager} install`}`;
process.exit(0);
}
}
console.log(`
Successfully created "${chalk.cyan(cdpath)}".
Inside that directory, you can run several commands:
logger.success`Created path=${cdpath}.`;
logger.info`Inside that directory, you can run several commands:
${chalk.cyan(`${pkgManager} start`)}
code=${`${pkgManager} start`}
Starts the development server.
${chalk.cyan(`${pkgManager} ${useYarn ? '' : 'run '}build`)}
code=${`${pkgManager} ${useYarn ? '' : 'run '}build`}
Bundles your website into static files for production.
${chalk.cyan(`${pkgManager} ${useYarn ? '' : 'run '}serve`)}
code=${`${pkgManager} ${useYarn ? '' : 'run '}serve`}
Serves the built website locally.
${chalk.cyan(`${pkgManager} deploy`)}
code=${`${pkgManager} deploy`}
Publishes the website to GitHub pages.
We recommend that you begin by typing:
${chalk.cyan('cd')} ${cdpath}
${chalk.cyan(`${pkgManager} start`)}
code=${`cd ${cdpath}`}
code=${`${pkgManager} start`}
Happy building awesome websites!
`);
`;
}