aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/path.ts
blob: b1a45d69bae2e820fd1bf0a027bb7c108a6caa4b (plain) (blame)
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
import { basename, extname, isAbsolute, join, resolve } from 'path'

let rootPath: string

function root () {
  if (rootPath) return rootPath

  rootPath = __dirname

  if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..')
  if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..')
  if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..')
  if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')

  return rootPath
}

function buildPath (path: string) {
  if (isAbsolute(path)) return path

  return join(root(), path)
}

function getLowercaseExtension (filename: string) {
  const ext = extname(filename) || ''

  return ext.toLowerCase()
}

export {
  root,
  buildPath,
  getLowercaseExtension
}