-
Notifications
You must be signed in to change notification settings - Fork 203
/
path_extensions.nu
59 lines (51 loc) · 1.32 KB
/
path_extensions.nu
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
# Returns a list of full paths starting from root, to the path given
#
#
# walk '/home/user/projects';
# > [
# > '/home/',
# > '/home/user/',
# > '/home/user/projects/',
# > ]
#
export def "path walk" [
path: any
] {
let list = ($path | path expand | path split);
$list | enumerate | each { |$part| (
$list | first ($part.index + 1) | path join;
)}
}
# Returns true if 'subfolder' is found along the path of 'folder', with a given type
export def "path check-sub" [
folder: any,
subfolder: string,
--type: list
] {
(ls -a $folder
| where (
($type == $nothing or $it.type in $type)
and ($it.name | path basename) == $subfolder
)
| length
) > 0;
}
# Walks the path along 'folder', and returns the path of the first subfolder found, or nothing if the subfolder was not found.
#
# path find-sub '/home/user/projects/code' '.venv';
# > /home/user/projects/.venv
export def "path find-sub" [
folder: any,
subfolder: string,
--type: list
] {
let paths = (path walk $folder);
let paths = ( $paths
| where (
path check-sub $it $subfolder --type $type
)
);
if ($paths != $nothing) and ($paths | length) > 0 {
[ ($paths | first), $subfolder ] | path join
} else {[]}
}