2021-10-26 12:59:05 +00:00
|
|
|
import { createHash } from 'crypto'
|
2022-01-18 16:59:14 +00:00
|
|
|
import { promises as fsp, readdirSync, statSync } from 'fs'
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hash (input: string, length = 8) {
|
|
|
|
return createHash('sha256')
|
|
|
|
.update(input)
|
|
|
|
.digest('hex')
|
2022-01-17 10:49:10 +00:00
|
|
|
.slice(0, length)
|
2021-10-26 12:59:05 +00:00
|
|
|
}
|
2022-01-18 16:59:14 +00:00
|
|
|
|
|
|
|
export function readDirRecursively (dir: string) {
|
|
|
|
return readdirSync(dir).reduce((files, file) => {
|
|
|
|
const name = join(dir, file)
|
|
|
|
const isDirectory = statSync(name).isDirectory()
|
|
|
|
return isDirectory ? [...files, ...readDirRecursively(name)] : [...files, name]
|
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function isDirectory (path: string) {
|
|
|
|
try {
|
|
|
|
return (await fsp.stat(path)).isDirectory()
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|