Skip to content

Commit

Permalink
fix: revert ignoreDuplicates in warn
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed May 15, 2024
1 parent 992a7a1 commit 0e99723
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/config/ts-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {join, relative as pathRelative, sep} from 'node:path'
import * as TSNode from 'ts-node'

import Cache from '../cache'
import {warn} from '../errors/warn'
import {memoizedWarn} from '../errors/warn'
import {Plugin, TSConfig} from '../interfaces'
import {settings} from '../settings'
import {existsSync} from '../util/fs'
Expand Down Expand Up @@ -72,7 +72,7 @@ async function loadTSConfig(root: string): Promise<TSConfig | undefined> {
if (isErrno(error)) return

debug(`Could not parse tsconfig.json. Skipping typescript path lookup for ${root}.`)
warn(`Could not parse tsconfig.json for ${root}. Falling back to compiled source.`)
memoizedWarn(`Could not parse tsconfig.json for ${root}. Falling back to compiled source.`)
}
}

Expand All @@ -88,7 +88,7 @@ async function registerTSNode(root: string, tsconfig: TSConfig): Promise<void> {
tsNode = require(tsNodePath)
} catch {
debug(`Could not find ts-node at ${tsNodePath}. Skipping ts-node registration for ${root}.`)
warn(
memoizedWarn(
`Could not find ts-node at ${tsNodePath}. Please ensure that ts-node is a devDependency. Falling back to compiled source.`,
)
return
Expand Down Expand Up @@ -235,7 +235,7 @@ async function determinePath(root: string, orig: string): Promise<string> {
}

debug(`No source file found. Returning default path ${orig}`)
if (!isProd()) warn(`Could not find source for ${orig} based on tsconfig. Defaulting to compiled source.`)
if (!isProd()) memoizedWarn(`Could not find source for ${orig} based on tsconfig. Defaulting to compiled source.`)

return orig
}
Expand Down Expand Up @@ -275,15 +275,15 @@ export async function tsPath(root: string, orig: string | undefined, plugin?: Pl
`Skipping typescript path lookup for ${root} because it's an ESM module (NODE_ENV: ${process.env.NODE_ENV}, root plugin module type: ${rootPlugin?.moduleType})`,
)
if (plugin?.type === 'link')
warn(
memoizedWarn(
`${plugin?.name} is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.`,
)
return orig
}

if (cannotUseTsNode(root, plugin, isProduction)) {
debug(`Skipping typescript path lookup for ${root} because ts-node is run in node version ${process.version}"`)
warn(
memoizedWarn(
`ts-node executable cannot transpile ESM in Node 20. Existing compiled source will be used instead. See https://github.com/oclif/core/issues/817.`,
)
return orig
Expand Down
23 changes: 8 additions & 15 deletions src/errors/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,12 @@ import {stderr} from '../ux/write'
import {CLIError, addOclifExitCode} from './errors/cli'
import prettyPrint from './errors/pretty-print'

const WARNINGS = new Set<Error | string>()

type Options = {
/**
* If true, will only print the same warning once.
*/
ignoreDuplicates?: boolean
}

/**
* Prints a pretty warning message to stderr.
*
* @param input The error or string to print.
* @param options.ignoreDuplicates If true, will only print the same warning once.
*/
export function warn(input: Error | string, options?: Options): void {
const ignoreDuplicates = options?.ignoreDuplicates ?? true
if (ignoreDuplicates && WARNINGS.has(input)) return
WARNINGS.add(input)

export function warn(input: Error | string): void {
let err: Error & OclifError

if (typeof input === 'string') {
Expand All @@ -39,4 +25,11 @@ export function warn(input: Error | string, options?: Options): void {
if (err?.stack) getLogger().error(err.stack)
}

const WARNINGS = new Set<Error | string>()
export function memoizedWarn(input: Error | string): void {
if (!WARNINGS.has(input)) warn(input)

WARNINGS.add(input)
}

export default warn
9 changes: 3 additions & 6 deletions src/util/read-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {readFile, readdir} from 'node:fs/promises'
import {dirname, join} from 'node:path'

import {warn} from '../errors/warn'
import {memoizedWarn} from '../errors/warn'
import {TSConfig} from '../interfaces'
import {makeDebug} from '../logger'
import {mergeNestedObjects} from './util'
Expand Down Expand Up @@ -45,17 +45,14 @@ export async function readTSConfig(root: string, tsconfigName = 'tsconfig.json')
}

if (!typescript) {
warn(
memoizedWarn(
'Could not find typescript. Please ensure that typescript is a devDependency. Falling back to compiled source.',
)
return
}

const read = async (path: string): Promise<unknown> => {
const localRoot = await upUntil(path, async (p) =>
// eslint-disable-next-line unicorn/no-await-expression-member
(await readdir(p)).includes('package.json'),
)
const localRoot = await upUntil(path, async (p) => (await readdir(p)).includes('package.json'))
if (!localRoot) return

try {
Expand Down

0 comments on commit 0e99723

Please sign in to comment.