-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ogImageHandler.ts
125 lines (114 loc) · 4.24 KB
/
ogImageHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import path from 'node:path'
import fs from 'fs-extra'
import { Listr } from 'listr2'
import { format } from 'prettier'
import { addWebPackages, getPrettierOptions } from '@redwoodjs/cli-helpers'
import { getConfig, getPaths } from '@redwoodjs/project-config'
import { runTransform } from '../../../../lib/runTransform'
export async function handler({ force }: { force: boolean }) {
const rwPaths = getPaths()
const rootPkgJson = fs.readJSONSync(path.join(rwPaths.base, 'package.json'))
const currentProjectVersion = rootPkgJson.devDependencies['@redwoodjs/core']
const notes: string[] = ['']
const tasks = new Listr(
[
{
title: 'Check prerequisites',
skip: force,
task: () => {
if (!getConfig().experimental?.streamingSsr?.enabled) {
throw new Error(
'The Streaming SSR experimental feature must be enabled before you can setup middleware',
)
}
},
},
addWebPackages([`@redwoodjs/ogimage-gen@${currentProjectVersion}`]),
{
title: 'Add OG Image middleware ...',
task: async () => {
const serverEntryPath = rwPaths.web.entryServer
if (serverEntryPath === null) {
throw new Error(
'Could not find the server entry file. Is your project using the default structure?',
)
}
const transformResult = await runTransform({
transformPath: path.join(__dirname, 'codemodMiddleware.js'),
targetPaths: [serverEntryPath],
})
if (transformResult.error) {
throw new Error(transformResult.error)
}
},
},
{
title: 'Add OG Image vite plugin ...',
task: async () => {
const viteConfigPath = rwPaths.web.viteConfig
if (viteConfigPath === null) {
throw new Error('Could not find the Vite config file')
}
const transformResult = await runTransform({
transformPath: path.join(__dirname, 'codemodVitePlugin.js'),
targetPaths: [viteConfigPath],
})
if (transformResult.error) {
throw new Error(transformResult.error)
}
},
},
{
title: 'Prettifying changed files',
task: async (_ctx, task) => {
const prettifyPaths = [
rwPaths.web.entryServer,
rwPaths.web.viteConfig,
]
for (const prettifyPath of prettifyPaths) {
if (prettifyPath === null) {
throw new Error('Could not find the file to be prettified')
}
try {
const source = fs.readFileSync(prettifyPath, 'utf-8')
const prettierOptions = await getPrettierOptions()
const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})
fs.writeFileSync(prettifyPath, prettifiedApp, 'utf-8')
} catch (error) {
task.output =
"Couldn't prettify the changes. Please reformat the files manually if needed."
}
}
},
},
{
title: 'One more thing...',
task: () => {
// Note: We avoid logging in the task because it can mess up the formatting of the text and we are often looking to maintain some basic indentation and such.
notes.push(
"og:image generation is almost ready to go! You'll need to add playwright as a dependency to the api side and then install the headless browser packages:",
)
notes.push('')
notes.push(' yarn workspace api add playwright')
notes.push(' cd api')
notes.push(' yarn playwright install')
notes.push('')
notes.push(
'Depending on how your host is configured you may need to install additional dependencies first. If so, the `playwright install` step will error out and give you the command to run to install those deps.',
)
},
},
],
{ rendererOptions: { collapseSubtasks: false } },
)
try {
await tasks.run()
console.log(notes.join('\n'))
} catch (e: any) {
console.error(e.message)
process.exit(e?.exitCode || 1)
}
}