Skip to content

Commit

Permalink
fix listr2 types
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Sep 21, 2023
1 parent 7103a27 commit addd1aa
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
17 changes: 13 additions & 4 deletions apps/app/lib/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable node/no-process-env */
import { Command } from 'commander'
import { Listr, type ListrContext, type ListrRenderer, type ListrTaskWrapper } from 'listr2'
import {
Listr,
type ListrContext,
type ListrDefaultRenderer,
type ListrSimpleRenderer,
type ListrTask,
type ListrTaskWrapper,
} from 'listr2'

import { generateTranslationKeys } from 'lib/generators'

const program = new Command()

export type ListrTask = ListrTaskWrapper<unknown, typeof ListrRenderer>
export type PassedTask = ListrTaskWrapper<unknown, ListrDefaultRenderer, ListrSimpleRenderer>

const options = {
bottomBar: 10,
Expand All @@ -15,7 +22,7 @@ const options = {
const translation = [
{
title: 'Translation definitions from DB',
task: (_ctx: ListrContext, task: ListrTask) => generateTranslationKeys(task),
task: (_ctx: ListrContext, task: PassedTask) => generateTranslationKeys(task),
options,
skip: !process.env.DATABASE_URL,
},
Expand All @@ -28,7 +35,7 @@ program

program.parse(process.argv)
const cliOpts = program.opts()
let tasklist = []
let tasklist: ListrJob[] = []

if (cliOpts.translations) tasklist.push(...translation)

Expand All @@ -39,3 +46,5 @@ const tasks = new Listr(tasklist, {
})

tasks.run()

type ListrJob = ListrTask<unknown, ListrDefaultRenderer>
4 changes: 2 additions & 2 deletions apps/app/lib/generators/translationKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import prettier from 'prettier'
import fs from 'fs'

import { prisma } from '@weareinreach/db'
import { type ListrTask } from 'lib/generate'
import { type PassedTask } from 'lib/generate'

const localePath = 'public/locales/en'

Expand All @@ -21,7 +21,7 @@ const isObject = (data: unknown): data is Record<string, string> =>

const countKeys = (obj: Output): number => Object.keys(flatten(obj)).length

export const generateTranslationKeys = async (task: ListrTask) => {
export const generateTranslationKeys = async (task: PassedTask) => {
const prettierOpts = (await prettier.resolveConfig(__filename)) ?? undefined

const data = await prisma.translationNamespace.findMany({
Expand Down
7 changes: 3 additions & 4 deletions apps/app/lib/prebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import path from 'path'

import { getEnv } from '@weareinreach/env'

const renderOptions = {
const rendererOptions = {
bottomBar: 10,
persistentOutput: true,
timer: PRESET_TIMER,
} satisfies ListrJob['options']
const injectOptions = (job: ListrJob): ListrJob => ({ ...job, options: renderOptions })
} satisfies ListrJob['rendererOptions']
const injectOptions = (job: ListrJob): ListrJob => ({ ...job, rendererOptions })

const tasks = new Listr(
[
Expand Down Expand Up @@ -38,7 +38,6 @@ const tasks = new Listr(
timer: PRESET_TIMER,
},
exitOnError: false,
forceColor: true,
}
)

Expand Down
10 changes: 4 additions & 6 deletions packages/db/lib/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Listr,
type ListrDefaultRenderer,
type ListrRenderer,
type ListrSimpleRenderer,
type ListrTask as ListrTaskObj,
type ListrTaskWrapper,
PRESET_TIMER,
Expand All @@ -12,14 +12,14 @@ import { prisma } from '~db/client'

import * as job from './generators'

const renderOptions = {
const rendererOptions = {
bottomBar: 10,
timer: PRESET_TIMER,
}
const defineJob = (title: string, job: (task: ListrTask) => void | Promise<void>): ListrJob => ({
title,
task: async (_ctx, task): Promise<void> => job(task),
options: renderOptions,
rendererOptions,
skip: !process.env.DATABASE_URL,
})

Expand Down Expand Up @@ -50,8 +50,6 @@ const tasks = new Listr<Context>(
],
{
exitOnError: false,
forceColor: true,

fallbackRendererOptions: {
timer: PRESET_TIMER,
},
Expand All @@ -70,5 +68,5 @@ tasks.run()
export type Context = {
error?: boolean
}
export type ListrTask = ListrTaskWrapper<Context, typeof ListrRenderer>
export type ListrTask = ListrTaskWrapper<Context, ListrDefaultRenderer, ListrSimpleRenderer>
type ListrJob = ListrTaskObj<Context, ListrDefaultRenderer>
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const jobs = new Listr(
task.title = jobTitle
task.task.off(ListrTaskEventType.OUTPUT)
},
options: { bottomBar: 10, persistentOutput: true },
rendererOptions: { bottomBar: 10, persistentOutput: true },
},
],
{
Expand Down
3 changes: 1 addition & 2 deletions packages/db/prisma/dataMigrationRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {
Listr,
type ListrDefaultRenderer,
type ListrSimpleRenderer,
type ListrTask as ListrTaskObj,
type ListrTaskWrapper,
PRESET_TIMER,
Expand Down Expand Up @@ -94,7 +93,7 @@ export type Context = {
pendingMigrations?: boolean
jobCount?: number
}
export type PassedTask = ListrTaskWrapper<Context, ListrDefaultRenderer, ListrSimpleRenderer>
export type PassedTask = ListrTaskWrapper<Context, ListrDefaultRenderer, ListrDefaultRenderer>
export type ListrJob = ListrTaskObj<Context, ListrDefaultRenderer>

export type MigrationJob = ListrJob & { def: JobDef }
Expand Down
4 changes: 2 additions & 2 deletions packages/db/util/crowdin/getIdsforDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const tasks = new Listr([
})
await run()
},
options: { bottomBar: 20 },
rendererOptions: { bottomBar: 20 },
},
{
title: 'Running Jobs',
Expand All @@ -92,7 +92,7 @@ const tasks = new Listr([

startQueue()
},
options: { bottomBar: 20 },
rendererOptions: { bottomBar: 20 },
},
])
tasks.run()

0 comments on commit addd1aa

Please sign in to comment.