Skip to content

Commit 006ed8a

Browse files
author
Markus Seebauer
committed
Changed type of add-node-modules-path-command from string to list-of-strings, so that multiple commands can be specified as list, not as comma-separated values in a string.
1 parent 9810425 commit 006ed8a

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ To automatically run it when opening a new buffer:
2020

2121
## Monorepo Support
2222
In a monorepo scenario it might make sense to add multiple directories.
23-
To achieve this, a comma-separated list of commands can be specified:
23+
To achieve this, additional commands can be specified:
2424

2525
```
2626
(use-package add-node-modules-path
2727
:custom
28-
(add-node-modules-path-command "pnpm bin, pnpm bin -w"))
28+
(add-node-modules-path-command '("pnpm bin" "pnpm bin -w")))
2929
```

add-node-modules-path.el

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,52 @@
3838
:group 'environment)
3939

4040
;;;###autoload
41-
(defcustom add-node-modules-path-command "npm bin"
42-
"Command to find the bin path. To add multiple bin paths, specify a
43-
comma-separated list of commands, e.g. 'pnpm bin, pnpm bin -w'"
44-
:type 'string)
41+
(defcustom add-node-modules-path-command '("npm bin")
42+
"Command(s) to find the bin path. To add multiple bin paths, simply add
43+
multiple commands to the list, e.g. \\='(\"pnpm bin\" \"pnpm bin -w\")"
44+
:type '(repeat string)
45+
:set (lambda (symbol value)
46+
"Converts a non-list value to a single-element list of the same value.
47+
This is necessary to be backward compatible, since previous versions of this
48+
custom var were of type string."
49+
(set-default symbol (if (listp value) value (list value)))))
4550

4651
;;;###autoload
4752
(defcustom add-node-modules-path-debug nil
4853
"Enable verbose output when non nil."
4954
:type 'boolean
5055
:group 'add-node-modules-path)
5156

52-
(defun add-node-modules-path/split-comma-separated-list (list-as-string)
53-
"Interprets the provided LIST-AS-STRING argument as comma-separated list of strings
54-
and returns the values as list. Values are trimmed and empty values are removed."
55-
(if (stringp list-as-string)
56-
(seq-filter 's-present? (mapcar 's-trim (s-split "," list-as-string t)))))
57+
(defun add-node-modules-path/trim-list-and-elements (list)
58+
"Trims all string values in LIST and empty / non-string values are removed."
59+
(if (listp list)
60+
(seq-filter 's-present? (mapcar 's-trim (seq-filter 'stringp list)))))
5761

5862
(defun add-node-modules-path/exec-command (command)
59-
"Executes the given COMMAND and returns a plist containing the command, its shell execution result
60-
and a boolean indicating, whether the execution result denotes a valid directory"
63+
"Executes the given COMMAND and returns a plist containing the command,
64+
its shell execution result and a boolean indicating, whether the execution
65+
result denotes a valid directory"
6166
(if (and (stringp command) (s-present? command))
6267
(let ((result (s-chomp (shell-command-to-string command))))
6368
(list 'command command 'result result 'directory-p (file-directory-p result)))))
6469

6570
(defun add-node-modules-path/exec-command-list (command-list)
66-
"Executes all commands in COMMAND-LIST and returns a list of plists containing the various
67-
comnand execution results. Elements in COMMAND-LIST which are not strings are ignored
68-
and will not appear in the result."
71+
"Executes all commands in COMMAND-LIST and returns a list of plists
72+
containing the various command execution results. Elements in COMMAND-LIST which
73+
are not strings are ignoredand will not appear in the result."
6974
(if (listp command-list)
7075
(seq-filter 'consp (mapcar 'add-node-modules-path/exec-command command-list))))
7176

7277
(defun add-node-modules-path/get-valid-directories (command-executions)
73-
"Filters the provided COMMAND-EXECUTIONS for entries, whose execution result denotes
74-
an existing directory"
78+
"Filters the provided COMMAND-EXECUTIONS for entries, whose execution result
79+
denotes an existing directory"
7580
(if (listp command-executions)
7681
(let ((filtered (seq-filter '(lambda (elt) (plist-get elt 'directory-p)) command-executions)))
7782
(mapcar #'(lambda (elt) (plist-get elt 'result)) filtered))))
7883

7984
(defun add-node-modules-path/get-invalid-executions (command-executions)
80-
"Filters the provided COMMAND-EXECUTIONS for entries, whose execution result denotes
81-
an invalid or non-existing directory"
85+
"Filters the provided COMMAND-EXECUTIONS for entries, whose execution result
86+
denotes an invalid or non-existing directory"
8287
(if (listp command-executions)
8388
(seq-filter #'(lambda (elt) (and (plist-member elt 'directory-p) (not (plist-get elt 'directory-p)))) command-executions)))
8489

@@ -100,7 +105,7 @@ an invalid or non-existing directory"
100105
"Run `npm bin` command and add the path to the `exec-path`.
101106
If `npm` command fails, it does nothing."
102107
(interactive)
103-
(let* ((commands (add-node-modules-path/split-comma-separated-list add-node-modules-path-command))
108+
(let* ((commands (add-node-modules-path/trim-list-and-elements add-node-modules-path-command))
104109
(executions (add-node-modules-path/exec-command-list commands))
105110
(dirs (add-node-modules-path/get-valid-directories executions)))
106111
(if (length> dirs 0)

test/add-node-modules-path-test.el

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
;;
33
;;
44

5-
(ert-deftest add-node-modules-path/split-comma-separated-list-test ()
6-
(should (equal (add-node-modules-path/split-comma-separated-list "pnpm bin, pnpm bin -w, ls -al ")
5+
(ert-deftest add-node-modules-path/trim-list-and-elements-test ()
6+
(should (equal (add-node-modules-path/trim-list-and-elements '("pnpm bin" "pnpm bin -w" "ls -al "))
77
'("pnpm bin" "pnpm bin -w" "ls -al")))
8-
(should (equal (add-node-modules-path/split-comma-separated-list " pnpm bin -w") '("pnpm bin -w")))
9-
(should (equal (add-node-modules-path/split-comma-separated-list ",,, ls -al , ,,") '("ls -al")))
10-
(should (eq (add-node-modules-path/split-comma-separated-list "") nil))
11-
(should (eq (add-node-modules-path/split-comma-separated-list " ") nil))
12-
(should (eq (add-node-modules-path/split-comma-separated-list " , ,, ,") nil)))
13-
8+
(should (equal (add-node-modules-path/trim-list-and-elements '(" pnpm bin -w")) '("pnpm bin -w")))
9+
(should (equal (add-node-modules-path/trim-list-and-elements '(nil "" " ls -al " "" nil "" nil)) '("ls -al")))
10+
(should (eq (add-node-modules-path/trim-list-and-elements "") nil))
11+
(should (eq (add-node-modules-path/trim-list-and-elements " ") nil))
12+
(should (eq (add-node-modules-path/trim-list-and-elements 1701) nil))
13+
(should (eq (add-node-modules-path/trim-list-and-elements "a string") nil)))
1414

1515
(ert-deftest add-node-modules-path/exec-command-test ()
1616
(should (eq (add-node-modules-path/exec-command nil) nil))
@@ -78,10 +78,10 @@
7878
(kill-local-variable 'add-node-modules-path-command))
7979

8080
(ert-deftest add-node-modules-path-single-command-test ()
81-
(add-node-modules-path/exec-add-node-modules-path-test "echo \"/etc\"" '("/etc")))
81+
(add-node-modules-path/exec-add-node-modules-path-test '("echo \"/usr\"") '("/usr")))
8282

8383
(ert-deftest add-node-modules-path-multiple-commands-test ()
84-
(add-node-modules-path/exec-add-node-modules-path-test "echo \"/etc\", echo \"/var\"" '("/etc" "/var")))
84+
(add-node-modules-path/exec-add-node-modules-path-test '("echo \"/etc\"" "echo \"/var\"") '("/etc" "/var")))
8585

8686
(ert-deftest add-node-modules-path-multiple-commands-with-failures-test ()
87-
(add-node-modules-path/exec-add-node-modules-path-test "ls -al, echo \"/var\", date, echo \"/etc\", clear" '("/var" "/etc")))
87+
(add-node-modules-path/exec-add-node-modules-path-test '("ls -al" "echo \"/var\"" "date" "echo \"/etc\"" "clear") '("/var" "/etc")))

0 commit comments

Comments
 (0)