|
| 1 | +#!/usr/bin/env -S nu --stdin |
| 2 | +# |
| 3 | +# Repository: FirefoxCSS-Store <https://github.com/FirefoxCSS-Store/FirefoxCSS-Store.github.io> |
| 4 | +# Author: BeyondMagic - João Farias 2024 <beyondmagic@mail.ru> |
| 5 | +# Maintainer: BeyondMagic - João Farias 2024 <beyondmagic@mail.ru> |
| 6 | + |
| 7 | +# Github API. |
| 8 | +export def github []: record<owner: string, name: string> -> record<pushed_at: string, stargazers_count: int, avatar: string> { |
| 9 | + let item = { |
| 10 | + #... |
| 11 | + pushed_at: '2023-03-06T16:47:46Z' |
| 12 | + stargazers_count: '2' |
| 13 | + owner: { |
| 14 | + avatar_url: 'https://avatars.githubusercontent.com/u/9977591?v=4' |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + { |
| 19 | + pushed_at: $item.pushed_at |
| 20 | + stargazers_count: ($item.stargazers_count | into int) |
| 21 | + avatar: $item.owner.avatar_url |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +# In case of not having API, clone and get information yourself. |
| 26 | +export def clone [ |
| 27 | + --temp: string = '/tmp/firefoxcss-store/' # Temporary folder to save themes. |
| 28 | +]: record<owner: string, name: string> -> record<pushed_at: string, stargazers_count: int, avatar: string> { |
| 29 | + |
| 30 | + mkdir $temp |
| 31 | + |
| 32 | + let repo = $in |
| 33 | + let folder = $temp | path join $repo.name |
| 34 | + |
| 35 | + let success = if ($folder | path exists) { |
| 36 | + cd $folder |
| 37 | + |
| 38 | + ^git pull |
| 39 | + |
| 40 | + true |
| 41 | + } else { |
| 42 | + |
| 43 | + let link = 'git@github.com:' + $repo.owner + '/' + $repo.name + '.git' |
| 44 | + |
| 45 | + let clone_status = ^git clone $link $folder |
| 46 | + | complete |
| 47 | + | get exit_code |
| 48 | + |
| 49 | + # Could not clone the repository for unknown reasons. |
| 50 | + if $clone_status != 0 { |
| 51 | + print --stderr $"Could not clone '($link)'." |
| 52 | + false |
| 53 | + } |
| 54 | + |
| 55 | + cd $folder |
| 56 | + |
| 57 | + true |
| 58 | + } |
| 59 | + |
| 60 | + let pushed_at = if $success { |
| 61 | + ^git show --quiet --date='format-local:%Y-%m-%dT%H:%M:%SZ' --format="%cd" |
| 62 | + } else { |
| 63 | + "" |
| 64 | + } |
| 65 | + |
| 66 | + { |
| 67 | + pushed_at: $pushed_at |
| 68 | + stargazers_count: -1 |
| 69 | + avatar: "" |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +# Parse link of repository. |
| 74 | +def parse_link []: string -> record<owner: string, name: string> { |
| 75 | + let data = $in |
| 76 | + | split row '/' |
| 77 | + | last 2 |
| 78 | + |
| 79 | + { |
| 80 | + owner: $data.0 |
| 81 | + name: $data.1 |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +# Get extra information from themes and save it. |
| 86 | +export def main [ |
| 87 | + token: string # API Token. |
| 88 | + --delay: duration = 1sec # Delay between API calls. |
| 89 | + --source: string = "../themes.json" # Themes data. |
| 90 | + --output: string = "./themes.json" # New data with themes. |
| 91 | + --timezone: string = "UTC0" # Timezone for git calls. |
| 92 | +]: nothing -> nothing { |
| 93 | + |
| 94 | + $env.TZ = $timezone |
| 95 | + |
| 96 | + let data = open $source |
| 97 | + | each {|item| |
| 98 | + |
| 99 | + let info = if ($item.link | str contains 'github') { |
| 100 | + sleep $delay |
| 101 | + $item.link | parse_link | github |
| 102 | + } else { |
| 103 | + $item.link | parse_link | clone |
| 104 | + } |
| 105 | + |
| 106 | + { |
| 107 | + ...$item |
| 108 | + ...$info |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments