aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/node-utils/src/path.ts
blob: 1d569833ef41b77b1d398c7fff7750a5e966d621 (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
47
48
49
50
import { basename, extname, isAbsolute, join, resolve } from 'path'
import { fileURLToPath } from 'url'

let rootPath: string

export function currentDir (metaUrl: string) {
  return resolve(fileURLToPath(metaUrl), '..')
}

export function root (metaUrl?: string) {
  if (rootPath) return rootPath

  if (!metaUrl) {
    metaUrl = import.meta.url

    const filename = basename(metaUrl) === 'path.js' || basename(metaUrl) === 'path.ts'
    if (!filename) throw new Error('meta url must be specified as this file has been bundled in another one')
  }

  rootPath = currentDir(metaUrl)

  if (basename(rootPath) === 'src' || basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
  if ([ 'node-utils', 'peertube-cli', 'peertube-runner' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..')
  if ([ 'packages', 'apps' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..')
  if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')

  return rootPath
}

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

  return join(root(), path)
}

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

  return ext.toLowerCase()
}

export 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(), 'packages', 'tests', 'fixtures', path)
}