Skip to content

Commit

Permalink
fix(cli): await async prettier functions (#10478)
Browse files Browse the repository at this point in the history
**Problem**
This follows up on the work done in #10179. The prettier API is now
async. Most of our code was already awaiting but we must now add `await`
in several places we previously did not.
  • Loading branch information
Josh-Walker-GM authored Apr 19, 2024
1 parent 60798a6 commit b3d324c
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export const handler = async ({ force, verbose }) => {
const opentelemetryTasks = [
{
title: `Adding OpenTelemetry setup files...`,
task: () => {
task: async () => {
const setupTemplateContent = fs.readFileSync(
path.resolve(__dirname, 'templates', 'opentelemetry.ts.template'),
'utf-8',
)
const setupScriptContent = ts
? setupTemplateContent
: transformTSToJS(opentelemetryScriptPath, setupTemplateContent)
: await transformTSToJS(opentelemetryScriptPath, setupTemplateContent)

return [
writeFile(opentelemetryScriptPath, setupScriptContent, {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/experimental/setupRscHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const handler = async ({ force, verbose }) => {
)
const entryServerContent = isTypeScriptProject()
? entryServerTemplate
: transformTSToJS(entryServerPath, entryServerTemplate)
: await transformTSToJS(entryServerPath, entryServerTemplate)

writeFile(entryServerPath, entryServerContent, {
overwriteExisting: true,
Expand All @@ -132,7 +132,7 @@ export const handler = async ({ force, verbose }) => {
const documentPath = path.join(rwPaths.web.src, `Document${ext}`)
const documentContent = isTypeScriptProject()
? documentTemplate
: transformTSToJS(documentPath, documentTemplate)
: await transformTSToJS(documentPath, documentTemplate)

writeFile(documentPath, documentContent, {
overwriteExisting: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const handler = async ({ force, verbose }) => {
let entryClientPath = rwPaths.web.entryClient
const entryClientContent = ts
? entryClientTemplate
: transformTSToJS(entryClientPath, entryClientTemplate)
: await transformTSToJS(entryClientPath, entryClientTemplate)

let overwriteExisting = force

Expand Down Expand Up @@ -130,7 +130,7 @@ export const handler = async ({ force, verbose }) => {
)
const entryServerContent = ts
? entryServerTemplate
: transformTSToJS(entryServerPath, entryServerTemplate)
: await transformTSToJS(entryServerPath, entryServerTemplate)

writeFile(entryServerPath, entryServerContent, {
overwriteExisting: force,
Expand All @@ -152,7 +152,7 @@ export const handler = async ({ force, verbose }) => {
const documentPath = path.join(rwPaths.web.src, `Document${ext}`)
const documentContent = ts
? documentTemplate
: transformTSToJS(documentPath, documentTemplate)
: await transformTSToJS(documentPath, documentTemplate)

writeFile(documentPath, documentContent, {
overwriteExisting: force,
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/generate/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { verifyModelName } from '../../../lib/schemaHelpers'
import { validateName, yargsDefaults } from '../helpers'
const TEMPLATE_PATH = path.resolve(__dirname, 'templates', 'model.js.template')

export const files = ({ name, typescript = false }) => {
const files = async ({ name, typescript = false }) => {
const outputFilename = `${name}.${typescript ? 'ts' : 'js'}`
const outputPath = path.join(getPaths().api.models, outputFilename)

return {
[outputPath]: generateTemplate(TEMPLATE_PATH, { name }),
[outputPath]: await generateTemplate(TEMPLATE_PATH, { name }),
}
}

Expand Down Expand Up @@ -59,8 +59,8 @@ export const handler = async ({ force, ...args }) => {
[
{
title: 'Generating model file...',
task: () => {
return writeFilesTask(files(args), { overwriteExisting: force })
task: async () => {
return writeFilesTask(await files(args), { overwriteExisting: force })
},
},
{
Expand Down
30 changes: 18 additions & 12 deletions packages/cli/src/commands/generate/realtime/realtimeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function handler({ name, type, force, verbose }) {
{
title: `Adding ${name} example subscription ...`,
enabled: () => functionType === 'subscription',
task: () => {
task: async () => {
// sdl

const exampleSdlTemplateContent = path.resolve(
Expand All @@ -100,7 +100,7 @@ export async function handler({ name, type, force, verbose }) {

const sdlContent = ts
? exampleSdlTemplateContent
: transformTSToJS(sdlFile, exampleSdlTemplateContent)
: await transformTSToJS(sdlFile, exampleSdlTemplateContent)

// service

Expand All @@ -119,7 +119,7 @@ export async function handler({ name, type, force, verbose }) {

const serviceContent = ts
? exampleServiceTemplateContent
: transformTSToJS(serviceFile, exampleServiceTemplateContent)
: await transformTSToJS(serviceFile, exampleServiceTemplateContent)

// subscription

Expand All @@ -139,27 +139,33 @@ export async function handler({ name, type, force, verbose }) {

const setupScriptContent = ts
? exampleSubscriptionTemplateContent
: transformTSToJS(exampleFile, exampleSubscriptionTemplateContent)
: await transformTSToJS(
exampleFile,
exampleSubscriptionTemplateContent,
)

// write all files
return [
writeFile(
sdlFile,
generateTemplate(sdlContent, templateVariables(name)),
await generateTemplate(sdlContent, templateVariables(name)),
{
overwriteExisting: force,
},
),
writeFile(
serviceFile,
generateTemplate(serviceContent, templateVariables(name)),
await generateTemplate(serviceContent, templateVariables(name)),
{
overwriteExisting: force,
},
),
writeFile(
exampleFile,
generateTemplate(setupScriptContent, templateVariables(name)),
await generateTemplate(
setupScriptContent,
templateVariables(name),
),
{
overwriteExisting: force,
},
Expand All @@ -170,7 +176,7 @@ export async function handler({ name, type, force, verbose }) {
{
title: `Adding ${name} example live query ...`,
enabled: () => functionType === 'liveQuery',
task: () => {
task: async () => {
// sdl
const exampleSdlTemplateContent = path.resolve(
__dirname,
Expand All @@ -185,7 +191,7 @@ export async function handler({ name, type, force, verbose }) {
)
const sdlContent = ts
? exampleSdlTemplateContent
: transformTSToJS(sdlFile, exampleSdlTemplateContent)
: await transformTSToJS(sdlFile, exampleSdlTemplateContent)

// service
const exampleServiceTemplateContent = path.resolve(
Expand All @@ -203,20 +209,20 @@ export async function handler({ name, type, force, verbose }) {
)
const serviceContent = ts
? exampleServiceTemplateContent
: transformTSToJS(serviceFile, exampleServiceTemplateContent)
: await transformTSToJS(serviceFile, exampleServiceTemplateContent)

// write all files
return [
writeFile(
sdlFile,
generateTemplate(sdlContent, templateVariables(name)),
await generateTemplate(sdlContent, templateVariables(name)),
{
overwriteExisting: force,
},
),
writeFile(
serviceFile,
generateTemplate(serviceContent, templateVariables(name)),
await generateTemplate(serviceContent, templateVariables(name)),
{
overwriteExisting: force,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ describe('fragments graphQLClientConfig', () => {
await matchInlineTransformSnapshot(
'appGqlConfigTransform',
testProjectAppTsx,
`import { FatalErrorBoundary, RedwoodProvider } from \"@redwoodjs/web\";
`import type { ReactNode } from 'react'
import { FatalErrorBoundary, RedwoodProvider } from \"@redwoodjs/web\";
import { RedwoodApolloProvider } from \"@redwoodjs/web/apollo\";
import FatalErrorPage from \"src/pages/FatalErrorPage\";
Expand All @@ -74,22 +76,25 @@ describe('fragments graphQLClientConfig', () => {
import \"./scaffold.css\";
import \"./index.css\";
interface AppProps {
children?: ReactNode;
}
const graphQLClientConfig = {
cacheConfig: {
possibleTypes: possibleTypes.possibleTypes,
},
};
const App = () => (
const App = ({ children }: AppProps) => (
<FatalErrorBoundary page={FatalErrorPage}>
<RedwoodProvider titleTemplate=\"%PageTitle | %AppTitle\">
<AuthProvider>
<RedwoodApolloProvider
useAuth={useAuth}
graphQLClientConfig={graphQLClientConfig}
>
<Routes />
{children ? children : <Routes />}
</RedwoodApolloProvider>
</AuthProvider>
</RedwoodProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function handler({ force }: Args) {

const prettierOptions = await getPrettierOptions()

const prettifiedApp = format(source, {
const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/setup/mailer/mailerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const handler = async ({ force, skipExamples }) => {
title: `Adding api/src/lib/mailer.${
projectIsTypescript ? 'ts' : 'js'
}...`,
task: () => {
task: async () => {
const templatePath = path.resolve(
__dirname,
'templates',
Expand All @@ -40,7 +40,7 @@ export const handler = async ({ force, skipExamples }) => {
)
const mailerContent = projectIsTypescript
? templateContent
: transformTSToJS(mailerPath, templateContent)
: await transformTSToJS(mailerPath, templateContent)

return writeFile(mailerPath, mailerContent, {
overwriteExisting: force,
Expand All @@ -59,7 +59,7 @@ export const handler = async ({ force, skipExamples }) => {
{
title: `Adding example ReactEmail mail template`,
skip: () => skipExamples,
task: () => {
task: async () => {
const templatePath = path.resolve(
__dirname,
'templates',
Expand All @@ -77,7 +77,7 @@ export const handler = async ({ force, skipExamples }) => {
)
const exampleTemplateContent = projectIsTypescript
? templateContent
: transformTSToJS(exampleTemplatePath, templateContent)
: await transformTSToJS(exampleTemplatePath, templateContent)

return writeFile(exampleTemplatePath, exampleTemplateContent, {
overwriteExisting: force,
Expand Down
Loading

0 comments on commit b3d324c

Please sign in to comment.