feat(logger): new "url" format, add double quotes around paths (#7019)

* refactor(logger): add double quotes around paths

* Introduce url formatter

* Fix
This commit is contained in:
Alexey Pyltsyn 2022-03-27 09:59:20 +03:00 committed by GitHub
parent 2bcac29cd4
commit a307da0b9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 11 deletions

View file

@ -271,7 +271,7 @@ export default async function init(
return logger.red('Invalid repository URL'); return logger.red('Invalid repository URL');
}, },
message: logger.interpolate`Enter a repository URL from GitHub, Bitbucket, GitLab, or any other public repo. 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'})`, (e.g: url=${'https://github.com/ownerName/repoName.git'})`,
}); });
({gitStrategy} = await prompts({ ({gitStrategy} = await prompts({
type: 'select', type: 'select',
@ -318,7 +318,7 @@ export default async function init(
logger.info('Creating new Docusaurus project...'); logger.info('Creating new Docusaurus project...');
if (isValidGitRepoUrl(template)) { if (isValidGitRepoUrl(template)) {
logger.info`Cloning Git template path=${template}...`; logger.info`Cloning Git template url=${template}...`;
if (!gitStrategies.includes(gitStrategy)) { if (!gitStrategies.includes(gitStrategy)) {
logger.error`Invalid git strategy: name=${gitStrategy}. Value must be one of ${gitStrategies.join( logger.error`Invalid git strategy: name=${gitStrategy}. Value must be one of ${gitStrategies.join(
', ', ', ',
@ -416,7 +416,7 @@ export default async function init(
} }
const useNpm = pkgManager === 'npm'; const useNpm = pkgManager === 'npm';
logger.success`Created path=${cdpath}.`; logger.success`Created name=${cdpath}.`;
logger.info`Inside that directory, you can run several commands: logger.info`Inside that directory, you can run several commands:
code=${`${pkgManager} start`} code=${`${pkgManager} start`}

View file

@ -8,7 +8,8 @@ It exports a single object as default export: `logger`. `logger` has the followi
- Some useful colors. - Some useful colors.
- Formatters. These functions have the same signature as the formatters of `picocolors`. Note that their implementations are not guaranteed. You should only care about their semantics. - Formatters. These functions have the same signature as the formatters of `picocolors`. Note that their implementations are not guaranteed. You should only care about their semantics.
- `path`: formats a file path or URL. - `path`: formats a file path.
- `url`: formats a URL.
- `id`: formats an identifier. - `id`: formats an identifier.
- `code`: formats a code snippet. - `code`: formats a code snippet.
- `subdue`: subdues the text. - `subdue`: subdues the text.
@ -34,6 +35,7 @@ To buy anything, enter code=${'buy x'} where code=${'x'} is the item's name; to
An embedded expression is optionally preceded by a flag in the form `%[a-z]+` (a percentage sign followed by a few lowercase letters). If it's not preceded by any flag, it's printed out as-is. Otherwise, it's formatted with one of the formatters: An embedded expression is optionally preceded by a flag in the form `%[a-z]+` (a percentage sign followed by a few lowercase letters). If it's not preceded by any flag, it's printed out as-is. Otherwise, it's formatted with one of the formatters:
- `path=`: `path` - `path=`: `path`
- `url=`: `url`
- `name=`: `id` - `name=`: `id`
- `code=`: `code` - `code=`: `code`
- `subdue=`: `subdue` - `subdue=`: `subdue`

View file

@ -11,7 +11,12 @@ import logger from '../index';
describe('formatters', () => { describe('formatters', () => {
it('path', () => { it('path', () => {
// cSpell:ignore mhey // cSpell:ignore mhey
expect(logger.path('hey')).toMatchInlineSnapshot(`"hey"`); expect(logger.path('hey')).toMatchInlineSnapshot(`"\\"hey\\""`);
});
it('url', () => {
expect(logger.url('https://docusaurus.io/')).toMatchInlineSnapshot(
`"https://docusaurus.io/"`,
);
}); });
it('id', () => { it('id', () => {
expect(logger.name('hey')).toMatchInlineSnapshot(`"hey"`); expect(logger.name('hey')).toMatchInlineSnapshot(`"hey"`);
@ -40,8 +45,7 @@ describe('interpolate', () => {
expect( expect(
logger.interpolate`The package at path=${'packages/docusaurus'} has number=${10} files. name=${'Babel'} is exported here subdue=${'(as a preset)'} that you can with code=${"require.resolve('@docusaurus/core/lib/babel/preset')"}`, logger.interpolate`The package at path=${'packages/docusaurus'} has number=${10} files. name=${'Babel'} is exported here subdue=${'(as a preset)'} that you can with code=${"require.resolve('@docusaurus/core/lib/babel/preset')"}`,
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
// cSpell:ignore mpackages `"The package at \\"packages/docusaurus\\" has 10 files. Babel is exported here (as a preset) that you can with \`require.resolve('@docusaurus/core/lib/babel/preset')\`"`,
`"The package at packages/docusaurus has 10 files. Babel is exported here (as a preset) that you can with \`require.resolve('@docusaurus/core/lib/babel/preset')\`"`,
); );
}); });
it('interpolates arrays with flags', () => { it('interpolates arrays with flags', () => {

View file

@ -9,7 +9,8 @@ import chalk, {type Chalk} from 'chalk';
type InterpolatableValue = string | number | (string | number)[]; type InterpolatableValue = string | number | (string | number)[];
const path = (msg: unknown): string => chalk.cyan(chalk.underline(msg)); const path = (msg: unknown): string => chalk.cyan(chalk.underline(`"${msg}"`));
const url = (msg: unknown): string => chalk.cyan(chalk.underline(msg));
const name = (msg: unknown): string => chalk.blue(chalk.bold(msg)); const name = (msg: unknown): string => chalk.blue(chalk.bold(msg));
const code = (msg: unknown): string => chalk.cyan(`\`${msg}\``); const code = (msg: unknown): string => chalk.cyan(`\`${msg}\``);
const subdue: Chalk = chalk.gray; const subdue: Chalk = chalk.gray;
@ -30,6 +31,8 @@ function interpolate(
switch (flag[0]) { switch (flag[0]) {
case 'path=': case 'path=':
return path; return path;
case 'url=':
return url;
case 'number=': case 'number=':
return num; return num;
case 'name=': case 'name=':
@ -131,6 +134,7 @@ const logger = {
bold: chalk.bold, bold: chalk.bold,
dim: chalk.dim, dim: chalk.dim,
path, path,
url,
name, name,
code, code,
subdue, subdue,

View file

@ -65,7 +65,7 @@ function normalizeSidebar(
throw new Error( throw new Error(
logger.interpolate`Invalid sidebar items collection code=${JSON.stringify( logger.interpolate`Invalid sidebar items collection code=${JSON.stringify(
sidebar, sidebar,
)} in ${place}: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a code=${'type'} property). See path=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`, )} in ${place}: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a code=${'type'} property). See url=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`,
); );
} }

View file

@ -55,7 +55,7 @@ export default async function render(
if (isNotDefinedErrorRegex.test((err as Error).message)) { if (isNotDefinedErrorRegex.test((err as Error).message)) {
logger.info`It looks like you are using code that should run on the client-side only. logger.info`It looks like you are using code that should run on the client-side only.
To get around it, try using code=${'<BrowserOnly>'} (path=${'https://docusaurus.io/docs/docusaurus-core/#browseronly'}) or code=${'ExecutionEnvironment'} (path=${'https://docusaurus.io/docs/docusaurus-core/#executionenvironment'}). To get around it, try using code=${'<BrowserOnly>'} (url=${'https://docusaurus.io/docs/docusaurus-core/#browseronly'}) or code=${'ExecutionEnvironment'} (url=${'https://docusaurus.io/docs/docusaurus-core/#executionenvironment'}).
It might also require to wrap your client code in code=${'useEffect'} hook and/or import a third-party library dynamically (if any).`; It might also require to wrap your client code in code=${'useEffect'} hook and/or import a third-party library dynamically (if any).`;
} }

View file

@ -230,7 +230,7 @@ export async function loadPlugins({
// TODO remove this deprecated lifecycle soon // TODO remove this deprecated lifecycle soon
// deprecated since alpha-60 // deprecated since alpha-60
// TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918 // TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918
logger.error`Plugin code=${'routesLoaded'} lifecycle is deprecated. If you think we should keep this lifecycle, please report here: path=${'https://github.com/facebook/docusaurus/issues/3918'}`; logger.error`Plugin code=${'routesLoaded'} lifecycle is deprecated. If you think we should keep this lifecycle, please report here: url=${'https://github.com/facebook/docusaurus/issues/3918'}`;
await plugin.routesLoaded(pluginsRouteConfigs); await plugin.routesLoaded(pluginsRouteConfigs);
}), }),