refactor(plugin-debug): migrate package to TypeScript (#5465)

* Complete migration

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Fix JSON root name

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Joshua Chen 2021-09-02 21:58:28 +08:00 committed by GitHub
parent 402a5e1f88
commit 6b7f3e8553
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 130 additions and 39 deletions

View file

@ -24,7 +24,7 @@ export default function pluginDebug({
name: 'docusaurus-plugin-debug',
getThemePath() {
return path.resolve(__dirname, '../src/theme');
return path.resolve(__dirname, '../lib/theme');
},
async contentLoaded({actions: {createData, addRoute}, allContent}) {

View file

@ -7,17 +7,17 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugJsonView from '../DebugJsonView';
import DebugLayout from '@theme/DebugLayout';
import DebugJsonView from '@theme/DebugJsonView';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
function DebugMetadata() {
function DebugMetadata(): JSX.Element {
const {siteConfig} = useDocusaurusContext();
return (
<DebugLayout>
<h2>Site config</h2>
<DebugJsonView src={siteConfig} collapseDepth="3" />
<DebugJsonView src={siteConfig} collapseDepth={3} />
</DebugLayout>
);
}

View file

@ -7,17 +7,30 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugJsonView from '../DebugJsonView';
import DebugLayout from '@theme/DebugLayout';
import DebugJsonView from '@theme/DebugJsonView';
import type {Props} from '@theme/DebugContent';
const PluginInstanceContent = ({pluginId, pluginInstanceContent}) => (
const PluginInstanceContent = ({
pluginId,
pluginInstanceContent,
}: {
pluginId: string;
pluginInstanceContent: unknown;
}) => (
<section style={{marginBottom: 30}}>
<code>{pluginId}</code>
<DebugJsonView src={pluginInstanceContent} collapseDepth="2" />
<DebugJsonView src={pluginInstanceContent} collapseDepth={2} />
</section>
);
const PluginContent = ({pluginName, pluginContent}) => {
const PluginContent = ({
pluginName,
pluginContent,
}: {
pluginName: string;
pluginContent: Record<string, unknown>;
}) => {
return (
<section style={{marginBottom: 60}}>
<h3>{pluginName}</h3>
@ -41,7 +54,7 @@ const PluginContent = ({pluginName, pluginContent}) => {
);
};
function DebugContent({allContent}) {
function DebugContent({allContent}: Props): JSX.Element {
return (
<DebugLayout>
<h2>Plugin content</h2>

View file

@ -7,16 +7,16 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugJsonView from '../DebugJsonView';
import DebugLayout from '@theme/DebugLayout';
import DebugJsonView from '@theme/DebugJsonView';
import useGlobalData from '@docusaurus/useGlobalData';
function DebugMetadata() {
function DebugMetadata(): JSX.Element {
const globalData = useGlobalData();
return (
<DebugLayout>
<h2>Global data</h2>
<DebugJsonView src={globalData} collapseDepth="3" />
<DebugJsonView src={globalData} collapseDepth={3} />
</DebugLayout>
);
}

View file

@ -6,19 +6,20 @@
*/
import React from 'react';
import BrowserOnly from '@docusaurus/BrowserOnly';
import type {Props} from '@theme/DebugJsonView';
import type {ReactJsonViewProps} from 'react-json-view';
// avoids "react-json-view" to display "root"
const RootName = false;
const RootName = null;
// Seems ReactJson does not work with SSR
// https://github.com/mac-s-g/react-json-view/issues/121
const BrowserOnlyReactJson = (props) => {
const BrowserOnlyReactJson = (props: ReactJsonViewProps) => {
return (
<BrowserOnly>
{() => {
// eslint-disable-next-line global-require
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const ReactJson = require('react-json-view').default;
return <ReactJson {...props} />;
}}
@ -26,10 +27,12 @@ const BrowserOnlyReactJson = (props) => {
);
};
function DebugJsonView({src, collapseDepth}) {
function DebugJsonView({src, collapseDepth}: Props): JSX.Element {
return (
<BrowserOnlyReactJson
src={src}
// Prop type defined by react-json-view
// eslint-disable-next-line @typescript-eslint/ban-types
src={src as object}
style={{
marginTop: '10px',
padding: '10px',
@ -45,7 +48,7 @@ function DebugJsonView({src, collapseDepth}) {
return field.name !== RootName && Object.keys(field.src).length > 50;
}}
collapsed={collapseDepth}
groupArraysAfterLength="5"
groupArraysAfterLength={5}
enableClipboard={false}
displayDataTypes={false}
/>

View file

@ -5,12 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import React, {ReactNode} from 'react';
import Head from '@docusaurus/Head';
import Link from '@docusaurus/Link';
import styles from './styles.module.css';
const DebugNavLink = ({to, children}) => (
const DebugNavLink = ({to, children}: {to: string; children: ReactNode}) => (
<Link
className={styles.navlink}
isNavLink
@ -23,7 +23,7 @@ const DebugNavLink = ({to, children}) => (
</Link>
);
function DebugLayout({children}) {
function DebugLayout({children}: {children: ReactNode}): JSX.Element {
return (
<>
<Head>

View file

@ -7,11 +7,11 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugLayout from '@theme/DebugLayout';
import registry from '@generated/registry';
import styles from './styles.module.css';
function DebugRegistry() {
function DebugRegistry(): JSX.Element {
return (
<DebugLayout>
<h2>Registry</h2>

View file

@ -7,12 +7,12 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugJsonView from '../DebugJsonView';
import DebugLayout from '@theme/DebugLayout';
import DebugJsonView from '@theme/DebugJsonView';
import routes from '@generated/routes';
import styles from './styles.module.css';
function DebugRoutes() {
function DebugRoutes(): JSX.Element {
return (
<DebugLayout>
<h2>Routes</h2>

View file

@ -7,11 +7,11 @@
import React from 'react';
import DebugLayout from '../DebugLayout';
import DebugLayout from '@theme/DebugLayout';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import styles from './styles.module.css';
function DebugMetadata() {
function DebugMetadata(): JSX.Element {
const {siteMetadata} = useDocusaurusContext();
return (
<DebugLayout>
@ -28,11 +28,12 @@ function DebugMetadata() {
{Object.entries(siteMetadata.pluginVersions).map(
([name, versionInformation]) => (
<li key={name} className={styles.listItem}>
{versionInformation.version && (
<div className={styles.version}>
<code>{versionInformation.version}</code>
</div>
)}
{versionInformation.type === 'package' &&
versionInformation.version && (
<div className={styles.version}>
<code>{versionInformation.version}</code>
</div>
)}
<div className={styles.name}>{name}</div>
<div>Type: {versionInformation.type}</div>
</li>

View file

@ -0,0 +1,51 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="@docusaurus/module-type-aliases" />
declare module '@theme/DebugConfig' {
export default function DebugMetadata(): JSX.Element;
}
declare module '@theme/DebugContent' {
import type {AllContent} from '@docusaurus/types';
export type Props = {
allContent: AllContent;
};
export default function DebugContent(props: Props): JSX.Element;
}
declare module '@theme/DebugGlobalData' {
export default function DebugGlobalData(): JSX.Element;
}
declare module '@theme/DebugJsonView' {
export type Props = {
src: unknown;
collapseDepth?: number;
};
export default function DebugJsonView(props: Props): JSX.Element;
}
declare module '@theme/DebugLayout' {
export default function DebugLayout(props: {
children: ReactNode;
}): JSX.Element;
}
declare module '@theme/DebugRegistry' {
export default function DebugRegistry(): JSX.Element;
}
declare module '@theme/DebugRoutes' {
export default function DebugRoutes(): JSX.Element;
}
declare module '@theme/DebugSiteMetadata' {
export default function DebugSiteMetadata(): JSX.Element;
}