-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 1.5 KB
/
index.js
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
import process from 'node:process'
import fsPromises from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
function toPath(urlOrPath) {
return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath
}
function isObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
// Function from https://github.com/sindresorhus/find-up-simple/blob/v1.0.0/index.js.
export async function findUp(name, { cwd = process.cwd(), type = 'file', stopAt } = {}) {
let directory = path.resolve(toPath(cwd) ?? '')
const { root } = path.parse(directory)
stopAt = path.resolve(directory, toPath(stopAt ?? root))
while (directory && directory !== stopAt && directory !== root) {
const filePath = path.isAbsolute(name) ? name : path.join(directory, name)
try {
const stats = await fsPromises.stat(filePath) // eslint-disable-line no-await-in-loop
if (
(type === 'file' && stats.isFile()) ||
(type === 'directory' && stats.isDirectory())
) {
return filePath
}
} catch {}
directory = path.dirname(directory)
}
}
// Modified function from https://stackoverflow.com/a/34749873.
export function skipArrayMergeDeep(target, source) {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}
return target
}