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,9 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
import * as fs from 'fs-extra';
import fs from 'fs-extra';
import importFresh from 'import-fresh';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import glob from 'glob';
import Color from 'color';
@ -79,7 +79,7 @@ export async function migrateDocusaurusProject(
): Promise<void> {
function createMigrationContext(): MigrationContext {
const v1Config = importFresh(`${siteDir}/siteConfig`) as VersionOneConfig;
console.log('Starting migration from v1 to v2...');
logger.info('Starting migration from v1 to v2...');
const partialMigrationContext = {
siteDir,
newDir,
@ -109,85 +109,68 @@ export async function migrateDocusaurusProject(
react: '^17.0.1',
'react-dom': '^17.0.1',
};
let errorCount = 0;
try {
createClientRedirects(siteConfig, deps, config);
console.log(
chalk.green('Successfully created client redirect for non clean URL'),
);
} catch (errorInClientRedirect) {
console.log(
chalk.red(`Error while creating redirects: ${errorInClientRedirect}`),
);
logger.success('Created client redirect for non clean URL');
} catch (e) {
logger.error(`Failed to creating redirects: ${e}`);
errorCount += 1;
}
if (shouldMigratePages) {
try {
createPages(newDir, siteDir);
console.log(
chalk.green(
'Successfully created pages (check migration page for more details)',
),
);
} catch (errorInMigratingPages) {
console.log(
chalk.red(
`Error occurred while creating pages: ${errorInMigratingPages}`,
),
logger.success(
'Created new doc pages (check migration page for more details)',
);
} catch (e) {
logger.error(`Failed to create new doc pages: ${e}`);
errorCount += 1;
}
} else {
try {
createDefaultLandingPage(newDir);
console.log(
chalk.green(
'Successfully created landing page (check migration page for more details)',
),
);
} catch (errorInLandingPage) {
console.log(
chalk.red(
`Error occurred while creating landing page: ${errorInLandingPage}`,
),
logger.success(
'Created landing page (check migration page for more details)',
);
} catch (e) {
logger.error(`Failed to create landing page: ${e}`);
errorCount += 1;
}
}
try {
migrateStaticFiles(siteDir, newDir);
console.log(chalk.green('Successfully migrated static folder'));
} catch (errorInStatic) {
console.log(
chalk.red(`Error occurred while copying static folder: ${errorInStatic}`),
);
logger.success('Migrated static folder');
} catch (e) {
logger.error(`Failed to copy static folder: ${e}`);
errorCount += 1;
}
try {
migrateBlogFiles(siteDir, newDir, classicPreset, shouldMigrateMdFiles);
} catch (errorInMigratingBlogs) {
console.log(
chalk.red(
`Error occurred while migrating blogs: ${errorInMigratingBlogs}`,
),
);
} catch (e) {
logger.error(`Failed to migrate blogs: ${e}`);
errorCount += 1;
}
try {
handleVersioning(siteDir, siteConfig, newDir, config, shouldMigrateMdFiles);
} catch (errorInVersion) {
console.log(
chalk.red(
`Error occurred while migrating versioned docs: ${errorInVersion}`,
),
);
} catch (e) {
logger.error(`Failed to migrate versioned docs: ${e}`);
errorCount += 1;
}
try {
migrateLatestDocs(siteDir, newDir, shouldMigrateMdFiles, classicPreset);
} catch (errorInDoc) {
chalk.red(`Error occurred while migrating docs: ${errorInDoc}`);
} catch (e) {
logger.error(`Failed to migrate docs: ${e}`);
errorCount += 1;
}
try {
migrateLatestSidebar(siteDir, newDir, classicPreset, siteConfig);
} catch (error) {
console.log(chalk.red(`Error occurred while migrating sidebar: ${error}`));
} catch (e) {
logger.error(`Failed to migrate sidebar: ${e}`);
errorCount += 1;
}
try {
@ -195,26 +178,26 @@ export async function migrateDocusaurusProject(
path.join(newDir, 'docusaurus.config.js'),
`module.exports=${JSON.stringify(config, null, 2)}`,
);
console.log(
chalk.green(
`Successfully created a new config file with new navbar and footer config`,
),
);
} catch (error) {
console.log(
chalk.red(`Error occurred while creating config file: ${error}`),
logger.success(
`Created a new config file with new navbar and footer config`,
);
} catch (e) {
logger.error(`Failed to create config file: ${e}`);
errorCount += 1;
}
try {
migratePackageFile(siteDir, deps, newDir);
} catch (error) {
console.log(
chalk.red(
`Error occurred while creating package.json file for project: ${error}`,
),
} catch (e) {
logger.error(
`Error occurred while creating package.json file for project: ${e}`,
);
errorCount += 1;
}
if (errorCount) {
logger.warn`Migration from v1 to v2 failed with number=${errorCount} errors: please check the log above`;
} else {
logger.success('Completed migration from v1 to v2');
}
console.log('Completed migration from v1 to v2');
}
export function createConfigFile({
@ -270,11 +253,9 @@ export function createConfigFile({
customConfigFields[key] = value;
}
});
console.log(
`${chalk.yellow(
'Following Fields from siteConfig.js will be added to docusaurus.config.js in `customFields`',
)}\n${chalk.yellow(Object.keys(customConfigFields).join('\n'))}`,
);
logger.info`Following Fields from path=${'siteConfig.js'} will be added to path=${'docusaurus.config.js'} in code=${'customFields'}: ${Object.keys(
customConfigFields,
)}`;
let v2DocsPath: string | undefined;
if (siteConfig.customDocsPath) {
@ -409,12 +390,12 @@ function createPages(newDir: string, siteDir: string): void {
const content = String(fs.readFileSync(filePath));
fs.writeFileSync(filePath, migratePage(content));
});
} catch (error) {
console.log(chalk.red(`Unable to migrate Pages : ${error}`));
} catch (e) {
logger.error(`Unable to migrate Pages: ${e}`);
createDefaultLandingPage(newDir);
}
} else {
console.log('Ignoring Pages');
logger.info('Ignoring Pages');
}
}
@ -452,13 +433,9 @@ function migrateBlogFiles(
fs.writeFileSync(file, sanitizedFileContent(content, migrateMDFiles));
});
classicPreset.blog.path = 'blog';
console.log(
chalk.green(
`Successfully migrated blogs to version 2 with change in frontmatter`,
),
);
logger.success('Migrated blogs to version 2 with change in front matter');
} else {
console.log(chalk.yellow(`Blog not found. Skipping migration for blog`));
logger.warn('Blog not found. Skipping migration for blog');
}
}
@ -489,18 +466,10 @@ function handleVersioning(
versionRegex,
migrateMDFiles,
);
console.log(
chalk.green(
`Successfully migrated version docs and sidebar. The following doc versions have been created: \n${loadedVersions.join(
'\n',
)}`,
),
);
logger.success`Migrated version docs and sidebar. The following doc versions have been created:name=${loadedVersions}`;
} else {
console.log(
chalk.yellow(
'Versioned docs not found. Skipping migration for versioned docs',
),
logger.warn(
'Versioned docs not found. Skipping migration for versioned docs',
);
}
}
@ -689,9 +658,7 @@ function migrateLatestSidebar(
'sidebars.json',
);
} catch {
console.log(
chalk.yellow(`Sidebar not found. Skipping migration for sidebar`),
);
logger.warn('Sidebar not found. Skipping migration for sidebar');
}
if (siteConfig.colors) {
const primaryColor = Color(siteConfig.colors.primaryColor);
@ -732,11 +699,9 @@ function migrateLatestDocs(
const content = String(fs.readFileSync(file));
fs.writeFileSync(file, sanitizedFileContent(content, migrateMDFiles));
});
console.log(chalk.green(`Successfully migrated docs to version 2`));
logger.success('Migrated docs to version 2');
} else {
console.log(
chalk.yellow(`Docs folder not found. Skipping migration for docs`),
);
logger.warn('Docs folder not found. Skipping migration for docs');
}
}
@ -774,7 +739,7 @@ function migratePackageFile(
path.join(newDir, 'package.json'),
JSON.stringify(packageFile, null, 2),
);
console.log(chalk.green(`Successfully migrated package.json file`));
logger.success('Migrated package.json file');
}
export async function migrateMDToMDX(
@ -790,5 +755,5 @@ export async function migrateMDToMDX(
sanitizedFileContent(String(fs.readFileSync(file)), true),
);
});
console.log(`Successfully migrated ${siteDir} to ${newDir}`);
logger.success`Successfully migrated path=${siteDir} to path=${newDir}`;
}