-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Setup command and codemod for OG image middleware (#10485)
This PR introduces a setup command for the OG image generation middleware. --- It did involve moving around some of the codemod utilities we have. Either because we needed to include them where we previously didn't or because they no longer made sense in the directory they were in.
- Loading branch information
1 parent
6302bd3
commit 7c98a41
Showing
26 changed files
with
713 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import terminalLink from 'terminal-link' | ||
import type { Argv } from 'yargs' | ||
|
||
import * as ogImageCommand from './ogImage/ogImage' | ||
|
||
export const command = 'middleware <type>' | ||
export const description = 'Set up a middleware' | ||
export function builder(yargs: Argv) { | ||
return yargs | ||
.command(ogImageCommand) | ||
.epilogue( | ||
`Also see the ${terminalLink( | ||
'Redwood CLI Reference', | ||
'https://redwoodjs.com/docs/cli-commands', | ||
)}`, | ||
) | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/cli/src/commands/setup/middleware/ogImage/__codemod_tests__/middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, it } from 'vitest' | ||
|
||
describe('Middleware codemod', () => { | ||
it('Handles the default TSX case', async () => { | ||
await matchTransformSnapshot('codemodMiddleware', 'defaultTsx') | ||
}) | ||
|
||
it('Handles when OgImageMiddleware is already imported', async () => { | ||
await matchTransformSnapshot('codemodMiddleware', 'alreadyContainsImport') | ||
}) | ||
|
||
it('Handles when registerMiddleware function is already defined', async () => { | ||
await matchTransformSnapshot( | ||
'codemodMiddleware', | ||
'registerFunctionAlreadyDefined', | ||
) | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
packages/cli/src/commands/setup/middleware/ogImage/__codemod_tests__/vitePlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { describe, it } from 'vitest' | ||
|
||
describe('Vite plugin codemod', () => { | ||
it('Handles the default vite config case', async () => { | ||
await matchTransformSnapshot('codemodVitePlugin', 'defaultViteConfig') | ||
}) | ||
}) |
18 changes: 18 additions & 0 deletions
18
...li/src/commands/setup/middleware/ogImage/__testfixtures__/alreadyContainsImport.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import OgImageMiddleware from "@redwoodjs/ogimage-gen/middleware"; | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
...i/src/commands/setup/middleware/ogImage/__testfixtures__/alreadyContainsImport.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import OgImageMiddleware from "@redwoodjs/ogimage-gen/middleware"; | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} | ||
|
||
export const registerMiddleware = async () => { | ||
const ogMw = new OgImageMiddleware({ | ||
App, | ||
Document, | ||
}); | ||
|
||
return [ogMw]; | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultTsx.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultTsx.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import OgImageMiddleware from "@redwoodjs/ogimage-gen/middleware"; | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} | ||
|
||
export const registerMiddleware = async () => { | ||
const ogMw = new OgImageMiddleware({ | ||
App, | ||
Document, | ||
}); | ||
|
||
return [ogMw]; | ||
} |
19 changes: 19 additions & 0 deletions
19
...es/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultViteConfig.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import dns from 'dns' | ||
|
||
import type { UserConfig } from 'vite' | ||
import { defineConfig } from 'vite' | ||
|
||
import redwood from '@redwoodjs/vite' | ||
|
||
// So that Vite will load on localhost instead of `127.0.0.1`. | ||
// See: https://vitejs.dev/config/server-options.html#server-host. | ||
dns.setDefaultResultOrder('verbatim') | ||
|
||
const viteConfig: UserConfig = { | ||
plugins: [redwood()], | ||
optimizeDeps: { | ||
force: true, | ||
}, | ||
} | ||
|
||
export default defineConfig(viteConfig) |
20 changes: 20 additions & 0 deletions
20
...s/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultViteConfig.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import vitePluginOgImageGen from '@redwoodjs/ogimage-gen/plugin' | ||
import dns from 'dns' | ||
|
||
import type { UserConfig } from 'vite' | ||
import { defineConfig } from 'vite' | ||
|
||
import redwood from '@redwoodjs/vite' | ||
|
||
// So that Vite will load on localhost instead of `127.0.0.1`. | ||
// See: https://vitejs.dev/config/server-options.html#server-host. | ||
dns.setDefaultResultOrder('verbatim') | ||
|
||
const viteConfig: UserConfig = { | ||
plugins: [redwood(), vitePluginOgImageGen()], | ||
optimizeDeps: { | ||
force: true, | ||
}, | ||
} | ||
|
||
export default defineConfig(viteConfig) |
27 changes: 27 additions & 0 deletions
27
...mmands/setup/middleware/ogImage/__testfixtures__/registerFunctionAlreadyDefined.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} | ||
|
||
export const registerMiddleware = async () => { | ||
const mojomboMiddleware = () => { | ||
while(true){ | ||
console.log("RedwoodJS is awesome!") | ||
} | ||
} | ||
|
||
return [mojomboMiddleware] | ||
} |
33 changes: 33 additions & 0 deletions
33
...mands/setup/middleware/ogImage/__testfixtures__/registerFunctionAlreadyDefined.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import OgImageMiddleware from "@redwoodjs/ogimage-gen/middleware"; | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
css: string[] | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ css, meta }) => { | ||
return ( | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
) | ||
} | ||
|
||
export const registerMiddleware = async () => { | ||
const mojomboMiddleware = () => { | ||
while(true){ | ||
console.log("RedwoodJS is awesome!") | ||
} | ||
} | ||
|
||
const ogMw = new OgImageMiddleware({ | ||
App, | ||
Document, | ||
}); | ||
|
||
return [mojomboMiddleware, ogMw]; | ||
} |
Oops, something went wrong.