-
Notifications
You must be signed in to change notification settings - Fork 2
/
blend_util
98 lines (90 loc) · 2.46 KB
/
blend_util
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env nu
# How to wrap an external command
# https://github.com/nushell/nushell/issues/7758#issuecomment-2097184688
# How to wrap an internal command with flags ???
# Possible levels of logging:
# - debug
# - info
# - warning
# - error
# - critical
# - success
# https://www.nushell.sh/book/stdout_stderr_exit_codes.html#echo-print-and-log-commands
export def log [
level: string,
--important (-i),
message: string] {
# check usable ansi styles
# ansi -l
let color = match $level {
"debug" => "white_dimmed"
"info" => {
match $important {
true => "default"
_ => "default_dimmed"
}}
"warn" => {
match $important {
true => "light_yellow"
_ => "yellow"
}}
"error" => {
match $important {
true => "light_red"
_ => "red"
}}
"success" => {
match $important {
true => "light_green"
_ => "green"
}}
_ => "reset"
}
print $"(ansi $color)($message)(ansi reset)"
}
export def print_heading [
variant: string,
title: string
] {
match $variant {
"note" => {
print $"(ansi bg_green)(ansi --escape { fg: '#ffffff' })(ansi attr_bold) ($title) (ansi reset)(ansi green)(char nf_left_segment)(ansi reset)"
}
"info" => {
print $"(ansi bg_light_blue)(ansi --escape { fg: '#ffffff' })(ansi attr_bold) ⬝($title) (ansi reset)(ansi light_blue)(char nf_left_segment)(ansi reset)"
}
_ => {
print $"($title)"
}
}
}
export const git_files = [
.gitignore
.gitattributes
.gitmodules
]
export def get_package_base_dir []: nothing -> string {
$env.FILE_PWD | path join "packages"
}
export def is_git_ignored [path: string]: nothing -> bool {
# FIXME: this method is not very efficient but works for now
do { git check-ignore $path} | complete | get exit_code | $in == 0
}
export def expand_package_dir [package_dir: string] {
def expand_inner [dir] {
let dir_contents = (ls $dir | where name not-in $git_files and not (is_git_ignored $it.name))
if ($dir_contents | length) == 1 {
let first_item = ($dir_contents | first)
if ($first_item.type == "dir") {
# MEMO: nushell `ls` is not the same as system's `ls`
# @see https://www.nushell.sh/commands/docs/ls.html
expand_inner $first_item.name
} else {
$first_item.name
}
} else {
$dir
}
}
expand_inner (get_package_base_dir | path relative-to $env.FILE_PWD | path join $package_dir)
}