|
1 | 1 | import findUp from 'find-up';
|
2 | 2 | import * as path from 'path';
|
3 | 3 |
|
| 4 | +interface ParsedPath { |
| 5 | + /** |
| 6 | + * The root of the path such as '/' or 'c:\' |
| 7 | + */ |
| 8 | + root: string; |
| 9 | + /** |
| 10 | + * The full directory path such as '/home/user/dir' or 'c:\path\dir' |
| 11 | + */ |
| 12 | + dir: string; |
| 13 | + /** |
| 14 | + * The file name including extension (if any) such as 'index.html' |
| 15 | + */ |
| 16 | + base: string; |
| 17 | + /** |
| 18 | + * The file extension (if any) such as '.html' |
| 19 | + */ |
| 20 | + ext: string; |
| 21 | + /** |
| 22 | + * The file name without extension (if any) such as 'index' |
| 23 | + */ |
| 24 | + name: string; |
| 25 | +} |
| 26 | + |
| 27 | +export interface PathInterface { |
| 28 | + dirname(path: string): string; |
| 29 | + isAbsolute(p: string): boolean; |
| 30 | + join(...paths: string[]): string; |
| 31 | + normalize(p: string): string; |
| 32 | + parse(path: string): ParsedPath; |
| 33 | + relative(from: string, to: string): string; |
| 34 | + resolve(...paths: string[]): string; |
| 35 | + sep: string; |
| 36 | +} |
| 37 | + |
| 38 | +interface PathHelper { |
| 39 | + /** |
| 40 | + * Parse a directory and return its root |
| 41 | + * @param directory - directory to parse. |
| 42 | + * @returns root directory |
| 43 | + */ |
| 44 | + directoryRoot(directory: string): string; |
| 45 | + |
| 46 | + /** |
| 47 | + * Find the git repository root directory. |
| 48 | + * @param directory - directory to search up from. |
| 49 | + * @returns resolves to `.git` root or undefined |
| 50 | + */ |
| 51 | + findRepoRoot(directory: string): Promise<string | undefined>; |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks to see if the child directory is nested under the parent directory. |
| 55 | + * @param parent - parent directory |
| 56 | + * @param child - possible child directory |
| 57 | + * @returns true iff child is a child of parent. |
| 58 | + */ |
| 59 | + isParentOf(parent: string, child: string): boolean; |
| 60 | + |
| 61 | + /** |
| 62 | + * Check to see if a parent directory contains a child directory. |
| 63 | + * @param parent - parent directory |
| 64 | + * @param child - child directory |
| 65 | + * @returns true iff child is the same as the parent or nested in the parent. |
| 66 | + */ |
| 67 | + contains(parent: string, child: string): boolean; |
| 68 | + |
| 69 | + /** |
| 70 | + * Make a path relative to another if the other is a parent. |
| 71 | + * @param path - the path to make relative |
| 72 | + * @param rootPath - a root of path |
| 73 | + * @returns the normalized relative path or undefined if rootPath is not a parent. |
| 74 | + */ |
| 75 | + makeRelativeTo(path: string, rootPath: string): string | undefined; |
| 76 | + |
| 77 | + /** |
| 78 | + * Normalize a path to have only forward slashes. |
| 79 | + * @param path - path to normalize |
| 80 | + * @returns a normalized string. |
| 81 | + */ |
| 82 | + normalizePath(path: string): string; |
| 83 | +} |
| 84 | + |
| 85 | +export function factoryPathHelper(path: PathInterface): PathHelper { |
| 86 | + function directoryRoot(directory: string): string { |
| 87 | + const p = path.parse(directory); |
| 88 | + return p.root; |
| 89 | + } |
| 90 | + |
| 91 | + async function findRepoRoot(directory: string): Promise<string | undefined> { |
| 92 | + const found = await findUp('.git', { cwd: directory, type: 'directory' }); |
| 93 | + if (!found) return undefined; |
| 94 | + return path.dirname(found); |
| 95 | + } |
| 96 | + |
| 97 | + function isParentOf(parent: string, child: string): boolean { |
| 98 | + const rel = path.relative(parent, child); |
| 99 | + return !!rel && !path.isAbsolute(rel) && rel[0] !== '.'; |
| 100 | + } |
| 101 | + |
| 102 | + function contains(parent: string, child: string): boolean { |
| 103 | + const rel = path.relative(parent, child); |
| 104 | + return !rel || (!path.isAbsolute(rel) && rel[0] !== '.'); |
| 105 | + } |
| 106 | + |
| 107 | + function makeRelativeTo(child: string, parent: string): string | undefined { |
| 108 | + const rel = path.relative(parent, child); |
| 109 | + if (path.isAbsolute(rel) || rel[0] === '.') return undefined; |
| 110 | + return normalizePath(rel); |
| 111 | + } |
| 112 | + |
| 113 | + function normalizePath(path: string): string { |
| 114 | + return path.replace(/\\/g, '/'); |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + directoryRoot, |
| 119 | + findRepoRoot, |
| 120 | + isParentOf, |
| 121 | + contains, |
| 122 | + normalizePath, |
| 123 | + makeRelativeTo, |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +const defaultHelper = factoryPathHelper(path); |
| 128 | + |
4 | 129 | /**
|
5 | 130 | * Parse a directory and return its root
|
6 | 131 | * @param directory - directory to parse.
|
7 | 132 | * @returns root directory
|
8 | 133 | */
|
9 |
| -export function directoryRoot(directory: string): string { |
10 |
| - const p = path.parse(directory); |
11 |
| - return p.root; |
12 |
| -} |
| 134 | +export const directoryRoot = defaultHelper.directoryRoot; |
13 | 135 |
|
14 | 136 | /**
|
15 | 137 | * Find the git repository root directory.
|
16 | 138 | * @param directory - directory to search up from.
|
17 | 139 | * @returns resolves to `.git` root or undefined
|
18 | 140 | */
|
19 |
| -export async function findRepoRoot(directory: string): Promise<string | undefined> { |
20 |
| - const found = await findUp('.git', { cwd: directory, type: 'directory' }); |
21 |
| - if (!found) return undefined; |
22 |
| - return path.dirname(found); |
23 |
| -} |
| 141 | +export const findRepoRoot = defaultHelper.findRepoRoot; |
24 | 142 |
|
25 | 143 | /**
|
26 | 144 | * Checks to see if the child directory is nested under the parent directory.
|
27 | 145 | * @param parent - parent directory
|
28 | 146 | * @param child - possible child directory
|
29 | 147 | * @returns true iff child is a child of parent.
|
30 | 148 | */
|
31 |
| -export function isParentOf(parent: string, child: string): boolean { |
32 |
| - const rel = path.relative(parent, child); |
33 |
| - return !!rel && !path.isAbsolute(rel) && rel[0] !== '.'; |
34 |
| -} |
| 149 | +export const isParentOf = defaultHelper.isParentOf; |
35 | 150 |
|
36 | 151 | /**
|
37 | 152 | * Check to see if a parent directory contains a child directory.
|
38 | 153 | * @param parent - parent directory
|
39 | 154 | * @param child - child directory
|
40 | 155 | * @returns true iff child is the same as the parent or nested in the parent.
|
41 | 156 | */
|
42 |
| -export function contains(parent: string, child: string): boolean { |
43 |
| - const rel = path.relative(parent, child); |
44 |
| - return !rel || (!path.isAbsolute(rel) && rel[0] !== '.'); |
| 157 | +export const contains = defaultHelper.contains; |
| 158 | + |
| 159 | +/** |
| 160 | + * Make a path relative to another if the other is a parent. |
| 161 | + * @param path - the path to make relative |
| 162 | + * @param rootPath - a root of path |
| 163 | + * @returns the normalized relative path or undefined if rootPath is not a parent. |
| 164 | + */ |
| 165 | +export const makeRelativeTo = defaultHelper.makeRelativeTo; |
| 166 | + |
| 167 | +/** |
| 168 | + * Normalize a path to have only forward slashes. |
| 169 | + * @param path - path to normalize |
| 170 | + * @returns a normalized string. |
| 171 | + */ |
| 172 | +export const normalizePath = defaultHelper.normalizePath; |
| 173 | + |
| 174 | +export const DefaultPathHelper: PathHelper = { |
| 175 | + directoryRoot, |
| 176 | + findRepoRoot, |
| 177 | + isParentOf, |
| 178 | + contains, |
| 179 | + makeRelativeTo, |
| 180 | + normalizePath, |
| 181 | +}; |
| 182 | + |
| 183 | +export function isDefined<T>(v: T | undefined | null): v is T { |
| 184 | + return v !== undefined && v !== null; |
45 | 185 | }
|
0 commit comments