Skip to content

Commit d346171

Browse files
committed
feat(sorting): fix cloning to allow any source
1 parent e3c76dd commit d346171

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

scripts/sort_themes.nu

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ export def github [
1818
}
1919
}
2020

21-
let item = http get $"https://api.github.com/repos/($repo.owner)/($repo.name)" --headers $headers
22-
21+
let item = try {
22+
http get $"https://api.github.com/repos/($repo.owner)/($repo.name)" --headers $headers
23+
} catch {
24+
return null
25+
}
26+
2327
{
2428
pushed_at: $item.pushed_at
2529
stargazers_count: ($item.stargazers_count | into int)
@@ -41,7 +45,11 @@ export def gitlab [
4145
}
4246
}
4347

44-
let item = http get $"https://gitlab.com/api/v4/projects/($repo.owner)%2F($repo.name)" --headers $headers
48+
let item = try {
49+
http get $"https://gitlab.com/api/v4/projects/($repo.owner)%2F($repo.name)" --headers $headers
50+
} catch {
51+
return null
52+
}
4553

4654
{
4755
pushed_at: $item.last_activity_at
@@ -64,7 +72,11 @@ export def codeberg [
6472
}
6573
}
6674

67-
let item = http get $"https://codeberg.org/api/v1/repos/($repo.owner)/($repo.name)" --headers $headers
75+
let item = try {
76+
http get $"https://codeberg.org/api/v1/repos/($repo.owner)/($repo.name)" --headers $headers
77+
} catch {
78+
return null
79+
}
6880

6981
{
7082
pushed_at: $item.updated_at
@@ -75,6 +87,7 @@ export def codeberg [
7587

7688
# In case of not having API, clone and get information yourself.
7789
export def clone [
90+
link: string # Git link of the repository.
7891
--temp: string = '/tmp/firefoxcss-store/' # Temporary folder to save themes.
7992
]: record<owner: string, name: string> -> record<pushed_at: string, stargazers_count: int, avatar: string> {
8093

@@ -83,36 +96,25 @@ export def clone [
8396
let repo = $in
8497
let folder = $temp | path join $repo.name
8598

86-
let success = if ($folder | path exists) {
99+
if ($folder | path exists) {
87100
cd $folder
88101

89102
^git pull
90-
91-
true
92103
} else {
93104

94-
let link = '[email protected]:' + $repo.owner + '/' + $repo.name + '.git'
95-
96105
let clone_status = ^git clone $link $folder
97106
| complete
98107
| get exit_code
99108

100109
# Could not clone the repository for unknown reasons.
101110
if $clone_status != 0 {
102-
print --stderr $"Could not clone '($link)'."
103-
false
111+
return null
104112
}
105-
106-
cd $folder
107113

108-
true
114+
cd $folder
109115
}
110116

111-
let pushed_at = if $success {
112-
^git show --quiet --date='format-local:%Y-%m-%dT%H:%M:%SZ' --format="%cd"
113-
} else {
114-
""
115-
}
117+
let pushed_at = ^git show --quiet --date='format-local:%Y-%m-%dT%H:%M:%SZ' --format="%cd"
116118

117119
{
118120
pushed_at: $pushed_at
@@ -125,18 +127,19 @@ export def clone [
125127
def parse_link []: string -> record<owner: string, name: string> {
126128
let data = $in
127129
| split row '/'
128-
| last 2
129130

130131
{
131-
owner: $data.0
132-
name: $data.1
132+
owner: $data.3
133+
name: $data.4
133134
}
134135
}
135136

136137
# Get extra information from themes and save it.
137138
export def main [
138-
token: string # API Token.
139-
--delay: duration = 1sec # Delay between API calls.
139+
--github: string # API Token for Github.
140+
--gitlab: string # API Token for Gitlab.
141+
--codeberg: string # API Token for Codeberg.
142+
--delay: duration = 2sec # Delay between API calls.
140143
--source: string = "../themes.json" # Themes data.
141144
--output: string = "./themes.json" # New data with themes.
142145
--timezone: string = "UTC0" # Timezone for git calls.
@@ -149,22 +152,40 @@ export def main [
149152

150153
let link = $item.repository
151154

155+
print $"Cloning ($link)."
156+
152157
let info = if ($link | str contains 'github') {
153158
sleep $delay
154-
$link | parse_link | github $token
159+
$link | parse_link | github $github
155160
} else if ($link | str contains 'gitlab') {
156161
sleep $delay
157-
$link | parse_link | gitlab $token
162+
$link | parse_link | gitlab $gitlab
158163
} else if ($link | str contains 'codeberg') {
159164
sleep $delay
160-
$link | parse_link | codeberg $token
165+
$link | parse_link | codeberg $codeberg
161166
} else {
162-
$link | parse_link | clone
167+
print "Using git cloning."
168+
$link | parse_link | clone $link
163169
}
164170

165-
{
166-
...$item
167-
...$info
171+
if ($info | is-empty) {
172+
print $"Could not clone this repository."
173+
print ""
174+
} else {
175+
print ""
176+
{
177+
...$item
178+
...$info
179+
}
168180
}
169181
}
182+
183+
$data | save --force $output
184+
185+
print "Replace the themes in the source directory? If no, will output the themes as JSON instead. To confirm, type either the word 'yes' or character 'y'."
186+
let ask = input "Answer: " | str downcase
187+
188+
if $ask == 'y' or $ask == 'yes' {
189+
mv --force $output $source
190+
}
170191
}

0 commit comments

Comments
 (0)