Skip to content

Commit 526e21d

Browse files
committed
Refactor to resolve a CLI based on pkg name
1 parent 643d758 commit 526e21d

2 files changed

Lines changed: 54 additions & 44 deletions

File tree

bin/cli

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ function findCommand( cmd ) {
6161
return null;
6262
}
6363

64+
/**
65+
* Resolves a CLI file path.
66+
*
67+
* @private
68+
* @param {string} pkg - package name
69+
* @returns {string} file path
70+
*/
71+
function getPath( pkg ) {
72+
// WARNING: we assume that CLIs follow the `./bin/cli` convention
73+
return resolve( __dirname, '..', 'lib', 'node_modules', pkg, 'bin', 'cli' )
74+
}
75+
6476

6577
// MAIN //
6678

@@ -99,15 +111,13 @@ function main() {
99111
if ( cmd === null ) {
100112
return onError( new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + args[ 1 ] + '`.' ) );
101113
}
102-
cmd = resolve( __dirname, '..', cmd.path );
103-
subargs = [ cmd, '--help' ];
114+
subargs = [ getPath( cmd.pkg ), '--help' ];
104115
} else {
105116
cmd = findCommand( args[ 0 ] );;
106117
if ( cmd === null ) {
107118
return onError( new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + args[ 0 ] + '`.' ) );
108119
}
109-
cmd = resolve( __dirname, '..', cmd.path );
110-
subargs = [ cmd ];
120+
subargs = [ getPath( cmd.pkg ) ];
111121
for ( i = 1; i < args.length; i++ ) {
112122
subargs.push( args[ i ] );
113123
}

bin/cli_commands.json

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,241 +2,241 @@
22
{
33
"cmd": "help [command]",
44
"desc": "Print a help message.",
5-
"path": "",
5+
"pkg": "",
66
"group": ""
77
},
88
{
99
"cmd": "arch",
1010
"desc": "Operating system CPU architecture.",
11-
"path": "./lib/node_modules/@stdlib/os/arch/bin/cli",
11+
"pkg": "@stdlib/os/arch",
1212
"group": "System"
1313
},
1414
{
1515
"cmd": "bundle-pkg-list",
1616
"desc": "Bundle a list of stdlib packages.",
17-
"path": "./lib/node_modules/@stdlib/_tools/bundle/pkg-list/bin/cli",
17+
"pkg": "@stdlib/_tools/bundle/pkg-list",
1818
"group": "Tools"
1919
},
2020
{
2121
"cmd": "capitalize",
2222
"desc": "Capitalize the first character in a string.",
23-
"path": "./lib/node_modules/@stdlib/string/capitalize/bin/cli",
23+
"pkg": "@stdlib/string/capitalize",
2424
"group": "Utils"
2525
},
2626
{
2727
"cmd": "configdir",
2828
"desc": "Configuration directory path.",
29-
"path": "./lib/node_modules/@stdlib/os/configdir/bin/cli",
29+
"pkg": "@stdlib/os/configdir",
3030
"group": "System"
3131
},
3232
{
3333
"cmd": "cwd",
3434
"desc": "Current working directory.",
35-
"path": "./lib/node_modules/@stdlib/process/cwd/bin/cli",
35+
"pkg": "@stdlib/process/cwd",
3636
"group": "System"
3737
},
3838
{
3939
"cmd": "datasets",
4040
"desc": "Retrieve a dataset.",
41-
"path": "./lib/node_modules/@stdlib/datasets/bin/cli",
41+
"pkg": "@stdlib/datasets",
4242
"group": ""
4343
},
4444
{
4545
"cmd": "ends-with",
4646
"desc": "Test if a string ends with another string.",
47-
"path": "./lib/node_modules/@stdlib/string/ends-with/bin/cli",
47+
"pkg": "@stdlib/string/ends-with",
4848
"group": "Utils"
4949
},
5050
{
5151
"cmd": "from-code-point",
5252
"desc": "Create a string from a sequence of code points.",
53-
"path": "./lib/node_modules/@stdlib/string/from-code-point/bin/cli",
53+
"pkg": "@stdlib/string/from-code-point",
5454
"group": "Utils"
5555
},
5656
{
5757
"cmd": "homedir",
5858
"desc": "Home directory path.",
59-
"path": "./lib/node_modules/@stdlib/os/homedir/bin/cli",
59+
"pkg": "@stdlib/os/homedir",
6060
"group": "System"
6161
},
6262
{
6363
"cmd": "lpad",
6464
"desc": "Left pad a string.",
65-
"path": "./lib/node_modules/@stdlib/string/left-pad/bin/cli",
65+
"pkg": "@stdlib/string/left-pad",
6666
"group": "Utils"
6767
},
6868
{
6969
"cmd": "ltrim",
7070
"desc": "Left trim a string.",
71-
"path": "./lib/node_modules/@stdlib/string/left-trim/bin/cli",
71+
"pkg": "@stdlib/string/left-trim",
7272
"group": "Utils"
7373
},
7474
{
7575
"cmd": "lowercase",
7676
"desc": "Lowercase a string.",
77-
"path": "./lib/node_modules/@stdlib/string/lowercase/bin/cli",
77+
"pkg": "@stdlib/string/lowercase",
7878
"group": "Utils"
7979
},
8080
{
8181
"cmd": "ls",
8282
"desc": "List stdlib package names.",
83-
"path": "./lib/node_modules/@stdlib/_tools/pkgs/names/bin/cli",
83+
"pkg": "@stdlib/_tools/pkgs/names",
8484
"group": "Meta"
8585
},
8686
{
8787
"cmd": "ls-tree",
8888
"desc": "Show stdlib package tree.",
89-
"path": "./lib/node_modules/@stdlib/_tools/pkgs/tree/bin/cli",
89+
"pkg": "@stdlib/_tools/pkgs/tree",
9090
"group": "Meta"
9191
},
9292
{
9393
"cmd": "node-version",
9494
"desc": "Node.js version.",
95-
"path": "./lib/node_modules/@stdlib/process/node-version/bin/cli",
95+
"pkg": "@stdlib/process/node-version",
9696
"group": "Meta"
9797
},
9898
{
9999
"cmd": "ns",
100100
"desc": "Print stdlib namespace.",
101-
"path": "./lib/node_modules/@stdlib/namespace/bin/cli",
101+
"pkg": "@stdlib/namespace",
102102
"group": "Meta"
103103
},
104104
{
105105
"cmd": "num-cpus",
106106
"desc": "Number of CPUs.",
107-
"path": "./lib/node_modules/@stdlib/os/num-cpus/bin/cli",
107+
"pkg": "@stdlib/os/num-cpus",
108108
"group": "System"
109109
},
110110
{
111111
"cmd": "padstr",
112112
"desc": "Pad a string.",
113-
"path": "./lib/node_modules/@stdlib/string/pad/bin/cli",
113+
"pkg": "@stdlib/string/pad",
114114
"group": "Utils"
115115
},
116116
{
117117
"cmd": "percent-encode",
118118
"desc": "Percent-encode a UTF-16 encoded string.",
119-
"path": "./lib/node_modules/@stdlib/string/percent-encode/bin/cli",
119+
"pkg": "@stdlib/string/percent-encode",
120120
"group": "Utils"
121121
},
122122
{
123123
"cmd": "platform",
124124
"desc": "Platform.",
125-
"path": "./lib/node_modules/@stdlib/os/platform/bin/cli",
125+
"pkg": "@stdlib/os/platform",
126126
"group": "System"
127127
},
128128
{
129129
"cmd": "remove-first",
130130
"desc": "Remove the first character in a string.",
131-
"path": "./lib/node_modules/@stdlib/string/remove-first/bin/cli",
131+
"pkg": "@stdlib/string/remove-first",
132132
"group": "Utils"
133133
},
134134
{
135135
"cmd": "remove-last",
136136
"desc": "Remove the last character in a string.",
137-
"path": "./lib/node_modules/@stdlib/string/remove-last/bin/cli",
137+
"pkg": "@stdlib/string/remove-last",
138138
"group": "Utils"
139139
},
140140
{
141141
"cmd": "remove-punctuation",
142142
"desc": "Remove punctuation characters from a string.",
143-
"path": "./lib/node_modules/@stdlib/string/remove-punctuation/bin/cli",
143+
"pkg": "@stdlib/string/remove-punctuation",
144144
"group": "Utils"
145145
},
146146
{
147147
"cmd": "remove-utf8-bom",
148148
"desc": "Remove a UTF-8 byte order mark (BOM).",
149-
"path": "./lib/node_modules/@stdlib/string/remove-first/bin/cli",
149+
"pkg": "@stdlib/string/remove-first",
150150
"group": "Utils"
151151
},
152152
{
153153
"cmd": "remove-words",
154154
"desc": "Remove a list of words from a string.",
155-
"path": "./lib/node_modules/@stdlib/string/remove-words/bin/cli",
155+
"pkg": "@stdlib/string/remove-words",
156156
"group": "Utils"
157157
},
158158
{
159159
"cmd": "repstr",
160160
"desc": "Repeat a string.",
161-
"path": "./lib/node_modules/@stdlib/string/repeat/bin/cli",
161+
"pkg": "@stdlib/string/repeat",
162162
"group": "Utils"
163163
},
164164
{
165165
"cmd": "repl",
166166
"desc": "Start a REPL.",
167-
"path": "./lib/node_modules/@stdlib/repl/bin/cli",
167+
"pkg": "@stdlib/repl",
168168
"group": ""
169169
},
170170
{
171171
"cmd": "replace",
172172
"desc": "Replace search occurrences in a string.",
173-
"path": "./lib/node_modules/@stdlib/string/replace/bin/cli",
173+
"pkg": "@stdlib/string/replace",
174174
"group": "Utils"
175175
},
176176
{
177177
"cmd": "reverse",
178178
"desc": "Reverse a string.",
179-
"path": "./lib/node_modules/@stdlib/string/reverse/bin/cli",
179+
"pkg": "@stdlib/string/reverse",
180180
"group": "Utils"
181181
},
182182
{
183183
"cmd": "rpad",
184184
"desc": "Right pad a string.",
185-
"path": "./lib/node_modules/@stdlib/string/right-pad/bin/cli",
185+
"pkg": "@stdlib/string/right-pad",
186186
"group": "Utils"
187187
},
188188
{
189189
"cmd": "rtrim",
190190
"desc": "Right trim a string.",
191-
"path": "./lib/node_modules/@stdlib/string/right-trim/bin/cli",
191+
"pkg": "@stdlib/string/right-trim",
192192
"group": "Utils"
193193
},
194194
{
195195
"cmd": "sparkline",
196196
"desc": "Plot a sparkline.",
197-
"path": "./lib/node_modules/@stdlib/plot/sparklines/unicode/bin/cli",
197+
"pkg": "@stdlib/plot/sparklines/unicode",
198198
"group": "Plot"
199199
},
200200
{
201201
"cmd": "startcase",
202202
"desc": "Capitalize each word in a string.",
203-
"path": "./lib/node_modules/@stdlib/string/startcase/bin/cli",
203+
"pkg": "@stdlib/string/startcase",
204204
"group": "Utils"
205205
},
206206
{
207207
"cmd": "starts-with",
208208
"desc": "Test if a string starts with another string.",
209-
"path": "./lib/node_modules/@stdlib/string/starts-with/bin/cli",
209+
"pkg": "@stdlib/string/starts-with",
210210
"group": "Utils"
211211
},
212212
{
213213
"cmd": "tmpdir",
214214
"desc": "Directory path for storing temporary files.",
215-
"path": "./lib/node_modules/@stdlib/os/tmpdir/bin/cli",
215+
"pkg": "@stdlib/os/tmpdir",
216216
"group": "System"
217217
},
218218
{
219219
"cmd": "trim",
220220
"desc": "Trim a string.",
221-
"path": "./lib/node_modules/@stdlib/string/trim/bin/cli",
221+
"pkg": "@stdlib/string/trim",
222222
"group": "Utils"
223223
},
224224
{
225225
"cmd": "umask",
226226
"desc": "Process mask.",
227-
"path": "./lib/node_modules/@stdlib/process/umask/bin/cli",
227+
"pkg": "@stdlib/process/umask",
228228
"group": "System"
229229
},
230230
{
231231
"cmd": "uncapitalize",
232232
"desc": "Uncapitalize a string.",
233-
"path": "./lib/node_modules/@stdlib/string/uncapitalize/bin/cli",
233+
"pkg": "@stdlib/string/uncapitalize",
234234
"group": "Utils"
235235
},
236236
{
237237
"cmd": "uppercase",
238238
"desc": "Uppercase a string.",
239-
"path": "./lib/node_modules/@stdlib/string/uppercase/bin/cli",
239+
"pkg": "@stdlib/string/uppercase",
240240
"group": "Utils"
241241
}
242242
]

0 commit comments

Comments
 (0)