2022-04-15 15:19:05 +00:00
|
|
|
import { promises as fsp, readdirSync, statSync } from 'node:fs'
|
2022-07-07 16:26:04 +00:00
|
|
|
import { hash } from 'ohash'
|
2022-01-18 16:59:14 +00:00
|
|
|
import { join } from 'pathe'
|
2021-10-26 12:59:05 +00:00
|
|
|
|
|
|
|
export function uniq<T> (arr: T[]): T[] {
|
|
|
|
return Array.from(new Set(arr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from vue-bundle-renderer utils
|
|
|
|
const IS_JS_RE = /\.[cm]?js(\?[^.]+)?$/
|
|
|
|
const IS_MODULE_RE = /\.mjs(\?[^.]+)?$/
|
|
|
|
const HAS_EXT_RE = /[^./]+\.[^./]+$/
|
2021-11-08 10:34:39 +00:00
|
|
|
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/
|
2021-10-26 12:59:05 +00:00
|
|
|
|
|
|
|
export function isJS (file: string) {
|
|
|
|
return IS_JS_RE.test(file) || !HAS_EXT_RE.test(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isModule (file: string) {
|
|
|
|
return IS_MODULE_RE.test(file) || !HAS_EXT_RE.test(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCSS (file: string) {
|
|
|
|
return IS_CSS_RE.test(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hashId (id: string) {
|
|
|
|
return '$id_' + hash(id)
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:40:06 +00:00
|
|
|
export function readDirRecursively (dir: string): string[] {
|
2022-01-18 16:59:14 +00:00
|
|
|
return readdirSync(dir).reduce((files, file) => {
|
|
|
|
const name = join(dir, file)
|
|
|
|
const isDirectory = statSync(name).isDirectory()
|
|
|
|
return isDirectory ? [...files, ...readDirRecursively(name)] : [...files, name]
|
2022-08-15 13:40:06 +00:00
|
|
|
}, [] as string[])
|
2022-01-18 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function isDirectory (path: string) {
|
|
|
|
try {
|
|
|
|
return (await fsp.stat(path)).isDirectory()
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|