1 | export var Branch;
|
2 | (function (Branch) {
|
3 | Branch["Public"] = "public";
|
4 | Branch["Pretty"] = "p";
|
5 | Branch["Private"] = "private";
|
6 | Branch["PrivateLog"] = "privateLog";
|
7 | Branch["Version"] = "version";
|
8 | })(Branch || (Branch = {}));
|
9 | export var Kind;
|
10 | (function (Kind) {
|
11 | Kind["Directory"] = "directory";
|
12 | Kind["File"] = "file";
|
13 | })(Kind || (Kind = {}));
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function directory(...args) {
|
19 | if (args.some(p => p.includes("/"))) {
|
20 | throw new Error("Forward slashes `/` are not allowed");
|
21 | }
|
22 | return { directory: args };
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 | export function file(...args) {
|
28 | if (args.some(p => p.includes("/"))) {
|
29 | throw new Error("Forward slashes `/` are not allowed");
|
30 | }
|
31 | return { file: args };
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | export function root() {
|
37 | return { directory: [] };
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export function fromPosix(path) {
|
49 | const split = path.replace(/^\/+/, "").split("/");
|
50 | if (path.endsWith("/"))
|
51 | return { directory: split.slice(0, -1) };
|
52 | else if (path === "")
|
53 | return root();
|
54 | return { file: split };
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export function toPosix(path, { absolute } = { absolute: false }) {
|
63 | const prefix = absolute ? "/" : "";
|
64 | const joinedPath = unwrap(path).join("/");
|
65 | if (isDirectory(path))
|
66 | return prefix + joinedPath + (joinedPath.length ? "/" : "");
|
67 | return prefix + joinedPath;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | export function combine(a, b) {
|
74 | return map(p => unwrap(a).concat(p), b);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | export function isBranch(branch, path) {
|
80 | return unwrap(path)[0] === branch;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | export function isDirectory(path) {
|
86 | return !!path.directory;
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 | export function isFile(path) {
|
92 | return !!path.file;
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | export function isRootDirectory(path) {
|
98 | return path.directory.length === 0;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 | export function isSameBranch(a, b) {
|
104 | return unwrap(a)[0] === unwrap(b)[0];
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | export function isSameKind(a, b) {
|
110 | if (isDirectory(a) && isDirectory(b))
|
111 | return true;
|
112 | else if (isFile(a) && isFile(b))
|
113 | return true;
|
114 | else
|
115 | return false;
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 | export function kind(path) {
|
121 | if (isDirectory(path))
|
122 | return Kind.Directory;
|
123 | return Kind.File;
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | export function map(fn, path) {
|
129 | if (isDirectory(path))
|
130 | return { directory: fn(path.directory) };
|
131 | else if (isFile(path))
|
132 | return { file: fn(path.file) };
|
133 | return path;
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 | export function parent(path) {
|
139 | return isDirectory(path) && isRootDirectory(path)
|
140 | ? null
|
141 | : directory(...unwrap(path).slice(0, -1));
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 | export function removeBranch(path) {
|
147 | return map(p => isDirectory(path) || p.length > 1 ? p.slice(1) : p, path);
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 | export function unwrap(path) {
|
153 | if (isDirectory(path)) {
|
154 | return path.directory;
|
155 | }
|
156 | else if (isFile(path)) {
|
157 | return path.file;
|
158 | }
|
159 | return [];
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | export function log(path) {
|
166 | return `[ ${path.join(", ")} ]`;
|
167 | }
|
168 |
|
\ | No newline at end of file |