forked from intlify/eslint-plugin-vue-i18n
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath-utils.ts
41 lines (35 loc) · 1.14 KB
/
path-utils.ts
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
/**
* @fileoverview Common helpers for operations on filenames and paths
* Borrow from GitHub `eslint/eslint` repo
* @see https://github.com/eslint/eslint/blob/v5.2.0/lib/util/path-util.js
* @author kazuya kawaguchi (a.k.a. kazupon)
*/
import { normalize, isAbsolute, resolve, relative } from 'path'
/**
* Replace Windows with posix style paths
*/
export function convertPathToPosix(filepath: string): string {
const normalizedFilepath = normalize(filepath)
const posixFilepath = normalizedFilepath.replace(/\\/g, '/')
return posixFilepath
}
/**
* Converts an absolute filepath to a relative path from a given base path
*/
export function getRelativePath(filepath: string, baseDir: string): string {
const absolutePath = isAbsolute(filepath) ? filepath : resolve(filepath)
if (baseDir) {
if (!isAbsolute(baseDir)) {
throw new Error(`baseDir should be an absolute path: ${baseDir}`)
}
return relative(baseDir, absolutePath)
}
return absolutePath.replace(/^\//, '')
}
export function getBasename(filepath: string): string {
return filepath
.replace(/^.*(\\|\/|:)/, '')
.split('.')
.slice(0, -1)
.join('.')
}