From 57e291cb1ae5a6e2cf37779a6e4b7c3c61b31283 Mon Sep 17 00:00:00 2001 From: ChristopherPHolder Date: Tue, 19 Dec 2023 13:58:38 +0100 Subject: [PATCH] fix(esm): fix imports --- .../src/lib/commands/collect/options/config.ts | 4 ++-- .../src/lib/commands/collect/options/types.ts | 5 +++-- .../lib/commands/collect/utils/config/index.ts | 16 +++++++--------- .../collect/utils/persist/persist-flow.ts | 14 +++++++------- .../collect/utils/user-flow/collect-flow.ts | 7 +++---- .../commands/collect/utils/user-flow/types.ts | 13 ++++--------- .../collect/utils/user-flow/user-flow.mock.ts | 4 ++-- .../lib/commands/init/derive-budgets-from-lhr.ts | 15 +++++++++++---- .../init/processes/generate-userflow.test.ts | 2 +- packages/cli/src/lib/core/prettier/index.ts | 7 +++++-- packages/cli/src/lib/hacky-things/lighthouse.ts | 12 ------------ packages/cli/src/lib/index.ts | 1 - 12 files changed, 45 insertions(+), 55 deletions(-) delete mode 100644 packages/cli/src/lib/hacky-things/lighthouse.ts diff --git a/packages/cli/src/lib/commands/collect/options/config.ts b/packages/cli/src/lib/commands/collect/options/config.ts index d9ddde8ff..c1a9ce58a 100644 --- a/packages/cli/src/lib/commands/collect/options/config.ts +++ b/packages/cli/src/lib/commands/collect/options/config.ts @@ -1,7 +1,7 @@ import _yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import { Param } from './config.model.js'; -import { LhConfigJson } from '../../../hacky-things/lighthouse.js'; +import { Config } from 'lighthouse'; const yargs = _yargs(hideBin(process.argv)); @@ -14,6 +14,6 @@ export const param: Param = { }; export function get(): string[] { - const { config } = yargs.argv as any as { config: LhConfigJson }; + const { config } = yargs.argv as any as { config: Config }; return config as string[]; } diff --git a/packages/cli/src/lib/commands/collect/options/types.ts b/packages/cli/src/lib/commands/collect/options/types.ts index c85e27d98..e54657be9 100644 --- a/packages/cli/src/lib/commands/collect/options/types.ts +++ b/packages/cli/src/lib/commands/collect/options/types.ts @@ -1,3 +1,5 @@ +import { Config } from 'lighthouse'; + import { Param as OpenReport } from './openReport.model.js'; import { Param as UfPath } from './ufPath.model.js'; import { Param as Url } from './url.model.js'; @@ -6,7 +8,6 @@ import { Param as AwaitServeStdout } from './awaitServeStdout.model.js'; import { Param as OutPath } from './outPath.model.js'; import { Param as Format } from './format.model.js'; import { AssertArgvOptions } from '../../assert/options/types.js'; -import { LhConfigJson } from '../../../hacky-things/lighthouse.js'; export type PersistYargsOptions = OpenReport & OutPath & Format; export type CollectYargsOptions = UfPath & OutPath & Url & ServeCommand & AwaitServeStdout; @@ -15,7 +16,7 @@ export type CollectRcOptions = { url: string, ufPath: string, configPath?: string; - config?: LhConfigJson, + config?: Config, // @TODO get better typing for if serveCommand is given await is required serveCommand?: string, awaitServeStdout?: string; diff --git a/packages/cli/src/lib/commands/collect/utils/config/index.ts b/packages/cli/src/lib/commands/collect/utils/config/index.ts index 7cc51664c..515197bdf 100644 --- a/packages/cli/src/lib/commands/collect/utils/config/index.ts +++ b/packages/cli/src/lib/commands/collect/utils/config/index.ts @@ -1,18 +1,16 @@ +import { Budget, Config } from 'lighthouse'; + import { readFile, writeFile } from '../../../../core/file/index.js'; import { logVerbose } from '../../../../core/loggin/index.js'; import { DEFAULT_COLLECT_CONFIG_PATH } from '../../options/configPath.constant.js'; -import { LhConfigJson } from '../../../../hacky-things/lighthouse.js'; import { CollectCommandArgv } from '../../options/types.js'; import { readBudgets } from '../../../assert/utils/budgets/index.js'; -// @ts-ignore -import Budget from 'lighthouse/types/lhr/budget'; - -export function readConfig(configPath: string = DEFAULT_COLLECT_CONFIG_PATH): LhConfigJson { +export function readConfig(configPath: string = DEFAULT_COLLECT_CONFIG_PATH): Config { return JSON.parse(readFile(configPath, { fail: true })); } -export function writeConfig(config: LhConfigJson, configPath: string = DEFAULT_COLLECT_CONFIG_PATH): void { +export function writeConfig(config: Config, configPath: string = DEFAULT_COLLECT_CONFIG_PATH): void { logVerbose(`Update config under ${configPath}`); if (JSON.stringify(readConfig()) !== JSON.stringify(config)) { @@ -23,8 +21,8 @@ export function writeConfig(config: LhConfigJson, configPath: string = DEFAULT_C } } -export function getLhConfigFromArgv(rc: Partial>): LhConfigJson { - let cfg: LhConfigJson = {}; +export function getLhConfigFromArgv(rc: Partial>): Config { + let cfg: Config = {}; if (!!rc?.configPath && !!rc?.config) { throw new Error('configPath and config can\'t be used together'); } @@ -62,7 +60,7 @@ export function getLhConfigFromArgv(rc: Partial { if (!format.length) { @@ -56,7 +55,8 @@ export async function persistFlow( } } - const fileName = toReportName(url, flow.name, reducedReport); + // @TODO Define correct fallback for missing name! + const fileName = toReportName(url, flow._options?.name ?? 'flow-results', reducedReport); return results.map((result) => { const filePath = join(outPath, `${fileName}.${result.format}`); writeFile(filePath, result.out); diff --git a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts index 25c5a1c99..677a269f6 100644 --- a/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts +++ b/packages/cli/src/lib/commands/collect/utils/user-flow/collect-flow.ts @@ -1,10 +1,9 @@ import { UserFlowProvider } from './types.js'; import { logVerbose } from '../../../../core/loggin/index.js'; -import * as puppeteer from 'puppeteer'; +import puppeteer from 'puppeteer'; import { Browser, LaunchOptions, Page } from 'puppeteer'; import { normalize } from 'path'; -// @ts-ignore -import { startFlow, UserFlow } from 'lighthouse/lighthouse-core/fraggle-rock/api'; +import { startFlow, UserFlow } from 'lighthouse'; import { get as dryRun } from '../../../../commands/collect/options/dryRun.js'; import { UserFlowMock } from './user-flow.mock.js'; import { detectCliMode } from '../../../../global/cli-mode/cli-mode.js'; @@ -37,7 +36,7 @@ export async function collectFlow( logVerbose(`User-flow path: ${normalize(path)}`); let start = Date.now(); - const flow: UserFlow = !dryRun() ? await startFlow(page, flowOptions) : new UserFlowMock(page, flowOptions); + const flow: UserFlow = !dryRun() ? await startFlow(page, flowOptions) : new UserFlowMock(page, flowOptions) as any as UserFlow; // run custom interactions await interactions({ flow, page, browser, collectOptions: cliOption }); diff --git a/packages/cli/src/lib/commands/collect/utils/user-flow/types.ts b/packages/cli/src/lib/commands/collect/utils/user-flow/types.ts index 2f954fc6a..557781ed2 100644 --- a/packages/cli/src/lib/commands/collect/utils/user-flow/types.ts +++ b/packages/cli/src/lib/commands/collect/utils/user-flow/types.ts @@ -7,24 +7,19 @@ import { Product } from 'puppeteer'; -// @ts-ignore -import * as Config from 'lighthouse/types/config'; -import { UserFlow } from '../../../../hacky-things/lighthouse.js'; -// @ts-ignore -import { SharedFlagsSettings } from 'lighthouse/types/lhr/settings'; - +import {SharedFlagsSettings, UserFlow } from 'lighthouse'; export type UserFlowContext = { browser: Browser; page: Page; - flow: any; + flow: UserFlow; collectOptions: { url: string }; }; export type StepOptions = { stepName: string; } & { - /*page: Page,*/ config?: Config.default.Json /*configContext?: LH.Config.FRContext*/; + /*page: Page,*/ config?: UserFlow.Options /*configContext?: LH.Config.FRContext*/; }; export type UserFlowInteractionsFn = ( @@ -35,7 +30,7 @@ export type UserFlowOptions = { name: string; } & { // throttling - /*page: Page,*/ config?: Config.default.Json /*configContext?: LH.Config.FRContext*/; + /*page: Page,*/ config?: UserFlow.Options['config'] /*configContext?: LH.Config.FRContext*/; }; // @TODO diff --git a/packages/cli/src/lib/commands/collect/utils/user-flow/user-flow.mock.ts b/packages/cli/src/lib/commands/collect/utils/user-flow/user-flow.mock.ts index 46ce8f420..231157a7c 100644 --- a/packages/cli/src/lib/commands/collect/utils/user-flow/user-flow.mock.ts +++ b/packages/cli/src/lib/commands/collect/utils/user-flow/user-flow.mock.ts @@ -1,8 +1,8 @@ import { Page } from 'puppeteer'; import { logVerbose } from '../../../../core/loggin/index.js'; -// @ts-ignore -import FlowResult from 'lighthouse/types/lhr/flow'; + import { StepOptions, UserFlowOptions } from './types.js'; +import { FlowResult } from 'lighthouse'; const dummyFlowResult: (cfg: UserFlowOptions) => FlowResult = (cfg: UserFlowOptions): FlowResult => { const config = cfg?.config || {}; diff --git a/packages/cli/src/lib/commands/init/derive-budgets-from-lhr.ts b/packages/cli/src/lib/commands/init/derive-budgets-from-lhr.ts index 848bc3251..54ebc5586 100644 --- a/packages/cli/src/lib/commands/init/derive-budgets-from-lhr.ts +++ b/packages/cli/src/lib/commands/init/derive-budgets-from-lhr.ts @@ -1,14 +1,15 @@ -import { UserFlow } from '../../hacky-things/lighthouse.js'; -// @ts-ignore -import Budget from 'lighthouse/types/lhr/budget'; +import type { Budget, FlowResult} from 'lighthouse'; + import { RequestCountResourceTypeBudgets, TransferSizeResourceTypeBudgets } from './constants.js'; import { logVerbose } from '../../core/loggin/index.js'; -export function deriveBudgetsFromLhr(flow: any): Budget[] { +export function deriveBudgetsFromLhr(flow: FlowResult): Budget[] { const budgetObject: Budget = {}; if (flow?.steps[0].lhr?.audits) { if (flow.steps[0].lhr.audits['resource-summary']) { const resourceSummary = flow.steps[0].lhr.audits['resource-summary']; + // TODO fix typing Error + // @ts-ignore budgetObject.resourceSizes = (resourceSummary.details.items as any) .filter(({ resourceType }: any) => TransferSizeResourceTypeBudgets.includes(resourceType)) .map(({ resourceType, transferSize }: any) => ({ @@ -16,6 +17,8 @@ export function deriveBudgetsFromLhr(flow: any): Budget[] { budget: transferSize }) ); + // TODO fix typing Error + // @ts-ignore budgetObject.resourceCounts = (resourceSummary.details.items as any) .filter(({ resourceType }: any) => RequestCountResourceTypeBudgets.includes(resourceType)) .map(({ resourceType, requestCount }: any) => ({ @@ -31,6 +34,8 @@ export function deriveBudgetsFromLhr(flow: any): Budget[] { if (flow.steps[0].lhr.audits['cumulative-layout-shift']) { budgetObject.timings.push({ 'metric': 'cumulative-layout-shift', + // TODO fix typing Error + // @ts-ignore 'budget': flow.steps[0].lhr.audits['cumulative-layout-shift'].details.items[0].totalCumulativeLayoutShift }); } else { @@ -40,6 +45,8 @@ export function deriveBudgetsFromLhr(flow: any): Budget[] { if (flow.steps[0].lhr.audits['largest-contentful-paint']) { budgetObject.timings.push({ 'metric': 'largest-contentful-paint', + // TODO fix typing Error + // @ts-ignore 'budget': parseInt(flow.steps[0].lhr.audits['largest-contentful-paint'].numericValue) }); } else { diff --git a/packages/cli/src/lib/commands/init/processes/generate-userflow.test.ts b/packages/cli/src/lib/commands/init/processes/generate-userflow.test.ts index 6ed3337b6..7b8911c4a 100644 --- a/packages/cli/src/lib/commands/init/processes/generate-userflow.test.ts +++ b/packages/cli/src/lib/commands/init/processes/generate-userflow.test.ts @@ -1,5 +1,5 @@ import { INITIATED_PRJ_CFG, INITIATED_RC_JSON, EMPTY_PRJ_CFG } from 'test-data'; -import { handleFlowGeneration } from './generate-userflow'; +import { handleFlowGeneration } from './generate-userflow.js'; import { existsSync } from 'fs'; import { join } from 'path'; import { withUserFlowProject } from '@push-based/user-flow-cli-testing'; diff --git a/packages/cli/src/lib/core/prettier/index.ts b/packages/cli/src/lib/core/prettier/index.ts index 76c75750b..5c1ecd192 100644 --- a/packages/cli/src/lib/core/prettier/index.ts +++ b/packages/cli/src/lib/core/prettier/index.ts @@ -1,7 +1,10 @@ -import { format as prettier, Options as PrettierOptions, resolveConfig } from 'prettier'; +import pkg, { Options } from 'prettier'; + import { SupportedExtname, SupportedParser } from './types.js'; import { supportedExtname } from './constants.js'; +const { format: prettier, resolveConfig } = pkg; + export function getParserFromExtname(extname: SupportedExtname | string): SupportedParser { extname = extname[0] === '.' ? extname.slice(1, extname.length) : extname; @@ -25,7 +28,7 @@ export function getParserFromExtname(extname: SupportedExtname | string): Suppor */ export function formatCode( code: string, - parser: PrettierOptions['parser'] = 'typescript' + parser: Options['parser'] = 'typescript' ) { const prettierConfig = resolveConfig.sync(__dirname); return prettier(code, { diff --git a/packages/cli/src/lib/hacky-things/lighthouse.ts b/packages/cli/src/lib/hacky-things/lighthouse.ts deleted file mode 100644 index 5cd6c177b..000000000 --- a/packages/cli/src/lib/hacky-things/lighthouse.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* -* NOTICE: -* -* As long as we did bot solve the import problem we will isolate this hack here to not pollute our codebase with `@ts-ignore`. -* -* */ - - -import { default as LhConfig } from 'lighthouse/types/config.d.js'; -// @ts-ignore -export type LhConfigJson = LhConfig.Json; -export { default as UserFlow } from 'lighthouse/types/user-flow.d.js'; diff --git a/packages/cli/src/lib/index.ts b/packages/cli/src/lib/index.ts index 7eccdb1b8..8f47291e9 100644 --- a/packages/cli/src/lib/index.ts +++ b/packages/cli/src/lib/index.ts @@ -18,7 +18,6 @@ export { MeasureModes } from './commands/collect/utils/replay/types.js'; export { RcJson } from './types.js'; export { CI_PROPERTY, CLI_MODE_PROPERTY } from './global/cli-mode/cli-mode.js'; export { CLI_MODES } from './global/cli-mode/types.js'; -export { LhConfigJson } from './hacky-things/lighthouse.js'; export { getEnvPreset, SANDBOX_PRESET, CI_PRESET, DEFAULT_PRESET } from './pre-set.js'; export { InitCommandArgv } from './commands/init/options/types.js'; export { GlobalOptionsArgv } from './global/options/types.js';