File size: 2,872 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import findUp from 'next/dist/compiled/find-up'
import { readFile } from 'fs/promises'
import JSON5 from 'next/dist/compiled/json5'
import { pathToFileURL } from 'url'

type RecursivePartial<T> = {
  [P in keyof T]?: RecursivePartial<T[P]>
}

export function findConfigPath(
  dir: string,
  key: string
): Promise<string | undefined> {
  // If we didn't find the configuration in `package.json`, we should look for
  // known filenames.
  return findUp(
    [
      `.${key}rc.json`,
      `${key}.config.json`,
      `.${key}rc.js`,
      `${key}.config.js`,
      `${key}.config.mjs`,
      `${key}.config.cjs`,
    ],
    {
      cwd: dir,
    }
  )
}

// We'll allow configuration to be typed, but we force everything provided to
// become optional. We do not perform any schema validation. We should maybe
// force all the types to be `unknown` as well.
export async function findConfig<T>(
  directory: string,
  key: string,
  _returnFile?: boolean
): Promise<RecursivePartial<T> | null> {
  // `package.json` configuration always wins. Let's check that first.
  const packageJsonPath = await findUp('package.json', { cwd: directory })
  let isESM = false

  if (packageJsonPath) {
    try {
      const packageJsonStr = await readFile(packageJsonPath, 'utf8')
      const packageJson = JSON.parse(packageJsonStr) as {
        [key: string]: string
      }

      if (typeof packageJson !== 'object') {
        throw new Error() // Stop processing and continue
      }

      if (packageJson.type === 'module') {
        isESM = true
      }

      if (packageJson[key] != null && typeof packageJson[key] === 'object') {
        return packageJson[key]
      }
    } catch {
      // Ignore error and continue
    }
  }

  const filePath = await findConfigPath(directory, key)

  const esmImport = (path: string) => {
    // Skip mapping to absolute url with pathToFileURL on windows if it's jest
    // https://github.com/nodejs/node/issues/31710#issuecomment-587345749
    if (process.platform === 'win32' && !process.env.JEST_WORKER_ID) {
      // on windows import("C:\\path\\to\\file") is not valid, so we need to
      // use file:// URLs
      return import(pathToFileURL(path).toString())
    } else {
      return import(path)
    }
  }

  if (filePath) {
    if (filePath.endsWith('.js')) {
      if (isESM) {
        return (await esmImport(filePath)).default
      } else {
        return require(filePath)
      }
    } else if (filePath.endsWith('.mjs')) {
      return (await esmImport(filePath)).default
    } else if (filePath.endsWith('.cjs')) {
      return require(filePath)
    }

    // We load JSON contents with JSON5 to allow users to comment in their
    // configuration file. This pattern was popularized by TypeScript.
    const fileContents = await readFile(filePath, 'utf8')
    return JSON5.parse(fileContents)
  }

  return null
}