Skip to content

Commit 102d970

Browse files
committed
PR feedback on addtion of registry values
1 parent 4c55581 commit 102d970

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

README.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,24 @@ Both of the above commands will make `nupm` and all its subcommands available in
3232
> ```
3333
3434
## :gear: configuration [[toc](#table-of-content)]
35-
One can change the location of the Nupm directory with `$env.NUPM_HOME`, e.g.
35+
One can change the location of the Nupm directory with `$env.nupm.home`, e.g.
3636
```nushell
3737
# env.nu
3838
39-
$env.NUPM_HOME = ($env.XDG_DATA_HOME | path join "nupm")
39+
$env.nupm.home = ($env.XDG_DATA_HOME | path join "nupm")
4040
```
4141
42-
Because Nupm will install modules and scripts in `{{nupm-home}}/modules/` and `{{nupm-home}}/scripts/` respectively, it is a good idea to add these paths to `$env.NU_LIB_DIRS` and `$env.PATH` respectively, e.g. if you have `$env.NUPM_HOME` defined:
42+
If you would like installed modules, scripts, and plugins to show up in [nushell search
43+
paths](https://www.nushell.sh/book/configuration.html#launch-stages), set the
44+
`nu_search_path` to `true` before calling `use nupm`:
4345
```nushell
4446
# env.nu
45-
46-
$env.NU_LIB_DIRS = [
47-
...
48-
($env.NUPM_HOME | path join "modules")
49-
]
50-
51-
$env.PATH = (
52-
$env.PATH
53-
| split row (char esep)
54-
| ....
55-
| prepend ($env.NUPM_HOME | path join "scripts")
56-
| uniq
57-
)
47+
$env.nupm = {
48+
home: "path/to/my_home"
49+
config: { nu_search_path: true }
50+
}
51+
# ...
52+
use path/to/nupm
5853
```
5954

6055
## :rocket: usage [[toc](#table-of-content)]

nupm/registry.nu

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Registry management for nupm
22

33
use utils/dirs.nu [nupm-home-prompt REGISTRY_FILENAME]
4-
use utils/log.nu throw-error
4+
use utils/log.nu [throw-error UNIMPLEMENTED]
5+
use utils/registry.nu registry-cache
56

67
# Manage nupm registires
78
@example "List all configured registries" { nupm registry }
@@ -30,25 +31,25 @@ export def describe [
3031
}
3132

3233
let registry_url = $env.NUPM_REGISTRIES | get $registry
33-
let registry_cache_dir = cache-dir --ensure | path join $registry
34-
let cached_registry = $registry_cache_dir | path join $REGISTRY_FILENAME
34+
let cache = registry-cache $registry
35+
let cached_registry = $cache.dir | path join $REGISTRY_FILENAME
3536

3637
# Always check cache first, only fall back to URL if cache doesn't exist
37-
let registry_data = if ($cached_registry | path exists) {
38-
open $cached_registry
38+
let registry_data = if ($cache.file | path exists) {
39+
open $cache.file
3940
} else if ($registry_url | path exists) {
4041
# Local registry file
4142
open $registry_url
4243
} else {
4344
# Remote registry - fetch and cache
4445
let data = http get $registry_url
45-
mkdir $registry_cache_dir
46-
$data | save $cached_registry
46+
mkdir $cache.dir
47+
$data | save $cache.file
4748
$data
4849
}
4950

5051
$registry_data | each {|entry|
51-
let package_cache_path = $registry_cache_dir | path join $"($entry.name).nuon"
52+
let package_cache_path = $cache.dir | path join $"($entry.name).nuon"
5253

5354
# Always check cache first for package data too
5455
let package_file_data = if ($package_cache_path | path exists) {
@@ -91,12 +92,17 @@ export def --env add [
9192
throw-error $"Registry '($name)' already exists. Use 'nupm registry update' to modify it."
9293
}
9394
$env.NUPM_REGISTRIES = $env.NUPM_REGISTRIES | insert $name $url
95+
mut add_success_msg = $"Registry '($name)' added successfully"
9496

9597
if $save {
9698
$env.NUPM_REGISTRIES | save --force $env.NUPM_INDEX_PATH
99+
$add_success_msg = $add_success_msg | append $" and written to ($env.NUPM_INDEX_PATH)." | str join " "
100+
101+
} else {
102+
$add_success_msg = $add_success_msg | append $". To commit the change to disk, use the `--save` flag." | str join " "
97103
}
98104

99-
print $"Registry '($name)' added successfully."
105+
print $add_success_msg
100106
}
101107

102108
# Remove a registry
@@ -174,5 +180,6 @@ export def init [--index] {
174180
return
175181
}
176182
# TODO initialize registry index here
183+
throw-error UNIMPLEMENTED "`nupm registry --index` is not implemented."
177184
}
178185

nupm/utils/log.nu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# denotes an error msg that a section of code is yet to be implemented
2+
export const UNIMPLEMENTED = "unimplemented"
3+
14
export def throw-error [
25
error: string
36
text?: string

nupm/utils/registry.nu

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export const REG_COLS = [ name path hash ]
1010
# Columns of a registry package file
1111
export const REG_PKG_COLS = [ name version path type info ]
1212

13+
14+
# return the respective registry cache directory and package index
15+
export def registry-cache [name: string]: nothing -> record<dir: path, file: path> {
16+
let dir = cache-dir --ensure | path join "registry" $name
17+
{ dir: $dir, file: ($dir | path join $REGISTRY_FILENAME) }
18+
}
19+
1320
# Search for a package in a registry
1421
export def search-package [
1522
package: string # Name of the package
@@ -47,23 +54,22 @@ export def search-package [
4754

4855
} else {
4956
try {
50-
let registry_cache_dir = cache-dir --ensure | path join $name
51-
let reg_file = $registry_cache_dir | path join $REGISTRY_FILENAME
57+
let cache = registry-cache $name
5258

53-
let reg = if ($reg_file | path exists) {
54-
open $reg_file
59+
let reg = if ($cache.file | path exists) {
60+
open $cache.file
5561
} else {
5662
let data = http get $url_or_path
57-
mkdir $registry_cache_dir
58-
$data | save --force $reg_file
63+
mkdir $cache.dir
64+
$data | save --force $cache.file
5965
$data
6066
}
6167

6268
{
6369
reg: $reg
64-
path: $reg_file
70+
path: $cache.file
6571
is_url: true
66-
cache_dir: $registry_cache_dir
72+
cache_dir: $cache.dir
6773
}
6874
} catch {
6975
throw-error $"Cannot open '($url_or_path)' as a file or URL."

0 commit comments

Comments
 (0)