- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathcreate-paths.ts
59 lines (54 loc) · 1.81 KB
/
create-paths.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
exportconstposix={sep: '/'};
// TODO: poor man's `path.join(path, '..')` in the browser.
exportfunctionparentPosix(path: string): string{
constsegments=path.split(posix.sep)||[];
segments.pop();
letmodified=segments.join(posix.sep);
if(path.charAt(path.length-1)===posix.sep){
modified+=posix.sep;
}
returnmodified;
}
exportfunctionbasename(path: string): string{
constsegments=path.split(posix.sep)||[];
returnsegments.pop()!;
}
exportfunctionposixSegments(posixPath: string): string[]{
returnposixPath.split(posix.sep).filter((segment)=>!!segment);
}
/**
* Splits the `raw` path into two segments, a root that contains user information and the relevant POSIX path. \
* For examples:
* ```
* `29ad0829759028dde9b877343fa3b0e1:testrest/sketches_v2/xxx_folder/xxx_sub_folder/sketch_in_folder/sketch_in_folder.ino`
* ```
* will be:
* ```
* ['29ad0829759028dde9b877343fa3b0e1:testrest/sketches_v2', '/xxx_folder/xxx_sub_folder/sketch_in_folder/sketch_in_folder.ino']
* ```
*/
exportfunctionsplitSketchPath(
raw: string,
sep='/sketches_v2/'
): [string,string]{
if(!sep){
thrownewError('Invalid separator. Cannot be zero length.');
}
constindex=raw.indexOf(sep);
if(index===-1){
thrownewError(`Invalid path pattern. Raw path was '${raw}'.`);
}
constcreateRoot=raw.substring(0,index+sep.length-1);// TODO: validate the `createRoot` format.
constposixPath=raw.substr(index+sep.length-1);
if(!posixPath){
thrownewError(`Could not extract POSIX path from '${raw}'.`);
}
return[createRoot,posixPath];
}
exportfunctiontoPosixPath(raw: string): string{
if(raw===posix.sep){
returnposix.sep;// Handles the root resource case.
}
const[,posixPath]=splitSketchPath(raw);
returnposixPath;
}