aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common/path.ts
blob: ec507538b21ca1900ea9f7485a51826f31916b37 (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
35
36
37
38
39
40
41
42
43
44
45
46
import { basename, extname, isAbsolute, join, resolve } from 'path'

let rootPath: string

function root () {
  if (rootPath) return rootPath

  rootPath = __dirname

  if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..')
  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()
}

function buildAbsoluteFixturePath (path: string, customCIPath = false) {
  if (isAbsolute(path)) return path

  if (customCIPath && process.env.GITHUB_WORKSPACE) {
    return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
  }

  return join(root(), 'server', 'tests', 'fixtures', path)
}

export {
  root,
  buildPath,
  buildAbsoluteFixturePath,
  getLowercaseExtension
}