Skip to content

Commit 156a011

Browse files
authored
fix(npm): update incompatible npm scripts (#1137)
I use Nushell and npm, and noticed that the npm section hasn't been updated for a long time, to the point where it no longer works with current npm versions. In fact, the current script produces the following result: ![image](https://github.com/user-attachments/assets/272a6a37-c623-452a-94ab-2f3c0ee54beb) So I've updated the npm-related scripts to fix this issue. Now it works correctly: ![image](https://github.com/user-attachments/assets/fcf8b590-0ede-4571-bd52-611cf4c9fdd3) Additionally, since npm -l | lines generates over 900 lines of output, this causes each run to take a long time (at least on my machine). Therefore, I've implemented caching using stor to store the results, which significantly reduces the execution time for subsequent calls. With caching enabled, the delay is now barely noticeable.
1 parent 32cdc96 commit 156a011

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

custom-completions/npm/npm-completions.nu

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
def "nu-complete npm" [] {
2-
^npm -l
3-
|lines
4-
|find 'Run "'
5-
|str trim
6-
|split column -c ' '
7-
|get column4
8-
|str replace '"' ''
2+
let db = stor open
3+
4+
try {
5+
# query commanders from in-mem db
6+
$db | query db "SELECT * FROM npm_commanders_table"
7+
} catch {
8+
# if catched error, create table and insert all data
9+
stor create --table-name npm_commanders_table --columns { value: str, description: str }
10+
11+
let npm_commanders = ^npm -l
12+
| lines
13+
| where $it =~ '\s{4}[a-z\-]+.*\s{4,}'
14+
| parse -r '\s*(?P<value>[^ ]+)\s*(?P<description>\w.*)'
15+
16+
$npm_commanders | stor insert --table-name npm_commanders_table
17+
18+
$npm_commanders
19+
}
920
}
1021

1122
export extern "npm" [
@@ -14,8 +25,9 @@ export extern "npm" [
1425

1526
def "nu-complete npm run" [] {
1627
open ./package.json
17-
|get scripts
18-
|columns
28+
| get scripts
29+
| transpose
30+
| rename value description
1931
}
2032

2133
export extern "npm run" [

0 commit comments

Comments
 (0)