dollar-shell 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [npm-image]: https://img.shields.io/npm/v/dollar-shell.svg
4
4
  [npm-url]: https://npmjs.org/package/dollar-shell
5
5
 
6
- `dollar-shell` is a micro-library for running shell commands and using them in streams with ease in Node, Deno, Bun. It is a tiny, simple, no dependency module with TypeScript typings.
6
+ `dollar-shell` is a micro-library for running shell commands and using them in streams with ease in Node, Deno, Bun. It is a tiny, simple, no dependency package with TypeScript typings.
7
7
 
8
8
  The idea is to run OS/shell commands and/or use them in stream pipelines as sources, sinks,
9
9
  and transformation steps using [web streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).
@@ -204,6 +204,7 @@ BSD-3-Clause
204
204
 
205
205
  ## Release History
206
206
 
207
+ - 1.0.3 *Added TSDoc comments, improved docs, fixed typos, added the missing copying of properties.*
207
208
  - 1.0.2 *Technical release: fixed references in the package file.*
208
209
  - 1.0.1 *Technical release: more tests, better documentation.*
209
210
  - 1.0.0 *The initial release.*
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dollar-shell",
3
3
  "description": "Run shell commands and use them in streams with ease in Node, Deno, Bun. Tiny, simple, no dependency package.",
4
- "version": "1.0.2",
4
+ "version": "1.0.3",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
7
7
  "types": "./src/index.d.ts",
package/src/bq-shell.js CHANGED
@@ -25,11 +25,12 @@ const impl =
25
25
  return shell(result.join(''), options);
26
26
  };
27
27
 
28
- const bqShell =
29
- (shellEscape, shell, options = {}) =>
30
- (strings, ...args) => {
28
+ const bqShell = (shellEscape, shell, options = {}) => {
29
+ const bq = (strings, ...args) => {
31
30
  if (verifyStrings(strings)) return impl(shellEscape, shell, options)(strings, ...args);
32
- return bqShell(shellEscape, shell, {...options, ...strings});
31
+ return Object.assign(bqShell(shellEscape, shell, {...options, ...strings}), bq);
33
32
  };
33
+ return bq;
34
+ };
34
35
 
35
36
  export default bqShell;
package/src/bq-spawn.js CHANGED
@@ -55,11 +55,12 @@ const impl =
55
55
  return spawn(result, options);
56
56
  };
57
57
 
58
- const bqSpawn =
59
- (spawn, options = {}) =>
60
- (strings, ...args) => {
58
+ const bqSpawn = (spawn, options = {}) => {
59
+ const bq = (strings, ...args) => {
61
60
  if (verifyStrings(strings)) return impl(spawn, options)(strings, ...args);
62
- return bqSpawn(spawn, {...options, ...strings});
61
+ return Object.assign(bqSpawn(spawn, {...options, ...strings}), bq);
63
62
  };
63
+ return bq;
64
+ };
64
65
 
65
66
  export default bqSpawn;
package/src/index.d.ts CHANGED
@@ -1,88 +1,330 @@
1
+ /**
2
+ * State of a standard stream.
3
+ */
1
4
  export type SpawnStreamState = 'pipe' | 'ignore' | 'inherit' | 'piped' | null;
2
5
 
6
+ /**
7
+ * Options to configure the process.
8
+ */
3
9
  export interface SpawnOptions {
10
+ /**
11
+ * The working directory for the child process.
12
+ */
4
13
  cwd?: string;
14
+ /**
15
+ * Environment variables for the child process. Otherwise the current process environment is used.
16
+ */
5
17
  env?: {[key: string]: string | undefined};
18
+ /**
19
+ * State of the standard input stream.
20
+ */
6
21
  stdin?: SpawnStreamState;
22
+ /**
23
+ * State of the standard output stream.
24
+ */
7
25
  stdout?: SpawnStreamState;
26
+ /**
27
+ * State of the standard error stream.
28
+ */
8
29
  stderr?: SpawnStreamState;
9
30
  }
10
31
 
11
- export interface Subprocess<R = string> {
32
+ /**
33
+ * Sub-process object.
34
+ */
35
+ export interface Subprocess<R = any> {
36
+ /**
37
+ * The raw command that was run as an array of strings.
38
+ */
12
39
  readonly command: string[];
40
+ /**
41
+ * The options that were passed to `spawn()`.
42
+ */
13
43
  readonly options: SpawnOptions | undefined;
44
+ /**
45
+ * The promise that will be resolved when the process exits.
46
+ * It returns the exit code of the process: `exitCode`.
47
+ */
14
48
  readonly exited: Promise<number>;
49
+ /**
50
+ * Whether the process has finished running.
51
+ */
15
52
  readonly finished: boolean;
53
+ /**
54
+ * Whether the process was killed when finished.
55
+ */
16
56
  readonly killed: boolean;
57
+ /**
58
+ * The exit code of the process when it was finished.
59
+ */
17
60
  readonly exitCode: number | null;
61
+ /**
62
+ * The signal code of the process when it was finished.
63
+ */
18
64
  readonly signalCode: string | null;
65
+ /**
66
+ * The standard input stream.
67
+ * It is `null` if `options.stdin` was not set to `'pipe'`.
68
+ */
19
69
  readonly stdin: WritableStream<R> | null;
70
+ /**
71
+ * The standard output stream.
72
+ * It is `null` if `options.stdout` was not set to `'pipe'`.
73
+ */
20
74
  readonly stdout: ReadableStream<R> | null;
75
+ /**
76
+ * The standard error stream.
77
+ * It is `null` if `options.stderr` was not set to `'pipe'`.
78
+ */
21
79
  readonly stderr: ReadableStream<R> | null;
80
+ /**
81
+ * Kill the process.
82
+ */
22
83
  kill(): void;
23
84
  }
24
85
 
86
+ /**
87
+ * Spawn a process with advanced ways to configure and control it.
88
+ */
25
89
  export declare function spawn(command: string[], options?: SpawnOptions): Subprocess;
26
90
 
91
+ /**
92
+ * The current working directory.
93
+ */
27
94
  export declare function cwd(): string;
95
+
96
+ /**
97
+ * The path of the current executable (Node, Deno, Bun, etc).
98
+ */
28
99
  export declare function currentExecPath(): string;
100
+
101
+ /**
102
+ * The arguments that should be passed to run the current executable.
103
+ * They don't include any permission options.
104
+ */
29
105
  export declare const runFileArgs: string[];
30
106
 
107
+ /**
108
+ * The function marks a value as raw. The value will be passed as-is to the shell.
109
+ * It is used to bypass the default shell escaping rules.
110
+ */
31
111
  export declare function raw(value: unknown): object;
112
+
113
+ /**
114
+ * The function escapes the value for the Windows command line using an alternative method.
115
+ * See the documentation.
116
+ */
32
117
  export declare function winCmdEscape(value: unknown): object | string;
33
118
 
119
+ /**
120
+ * Escape configuration options.
121
+ */
34
122
  export interface ShellEscapeOptions {
123
+ /**
124
+ * Specify the path of the shell to use. Defaults to `currentShellPath()`.
125
+ */
35
126
  shellPath?: string;
36
127
  }
37
128
 
38
- export declare function shellEscape(s: {toString(): string}, options?: ShellEscapeOptions): string;
129
+ /**
130
+ * Escapes a value using the specified options.
131
+ */
132
+ export declare function shellEscape(value: {toString(): string}, options?: ShellEscapeOptions): string;
39
133
 
134
+ /**
135
+ * The path of the current shell.
136
+ */
40
137
  export declare function currentShellPath(): string;
138
+
139
+ /**
140
+ * Builds a shell command from a command using the `shell` and its `args`.
141
+ */
41
142
  export declare function buildShellCommand(shell: string | undefined, args: string[] | undefined, command: string): string[];
42
143
 
144
+ /**
145
+ * Backticks (tag) function.
146
+ */
43
147
  type Backticks<R> = (strings: TemplateStringsArray, ...args: unknown[]) => R;
44
148
 
149
+ /**
150
+ * The type of the $ (tag) function. It can be used as a tag function for a template string.
151
+ * Or it can take an options object and return self with updated defaults.
152
+ */
45
153
  interface Dollar<R, O = SpawnOptions> extends Backticks<R> {
154
+ /**
155
+ * The function can take an options object and return self with updated defaults.
156
+ */
46
157
  (options: O): Dollar<R, O>;
47
158
  }
48
159
 
160
+ /**
161
+ * $$ (tag) function that parses the template string, spawns a process and returns a sub-process object.
162
+ * It can take an option object and return self with updated defaults.
163
+ */
49
164
  export declare const $$: Dollar<Subprocess>;
50
165
 
166
+ /**
167
+ * Object with simplified result of the process.
168
+ */
51
169
  export interface DollarResult {
170
+ /**
171
+ * The exit code of the process.
172
+ */
52
173
  code: number | null;
174
+ /**
175
+ * The signal code of the process.
176
+ */
53
177
  signal: string | null;
178
+ /**
179
+ * Whether the process was killed.
180
+ */
54
181
  killed: boolean;
55
182
  }
56
183
 
57
- interface DuplexPair<R = string> {
184
+ /**
185
+ * Object with readable and writable streams. It can be used as a duplex stream.
186
+ */
187
+ interface DuplexPair<R = any> {
188
+ /**
189
+ * The readable stream.
190
+ */
58
191
  readable: ReadableStream<R>;
192
+ /**
193
+ * The writable stream.
194
+ */
59
195
  writable: WritableStream<R>;
60
196
  }
61
197
 
62
- interface DollarImpl<R = string> extends Dollar<Promise<DollarResult>> {
198
+ /**
199
+ * The type of the {@link $} function.
200
+ */
201
+ interface DollarImpl<R = any> extends Dollar<Promise<DollarResult>> {
202
+ /**
203
+ * The tag function that can be used as a template string.
204
+ * It can take an options object and return self with updated defaults.
205
+ *
206
+ * The tag function returns the `stdout` stream of the process as a `ReadableStream` .
207
+ */
63
208
  from: Dollar<ReadableStream<R>>;
209
+
210
+ /**
211
+ * The tag function that can be used as a template string.
212
+ * It can take an options object and return self with updated defaults.
213
+ *
214
+ * The tag function returns the `stdin` stream of the process as a `WritableStream` .
215
+ */
64
216
  to: Dollar<WritableStream<R>>;
217
+
218
+ /**
219
+ * The tag function that can be used as a template string.
220
+ * It can take an options object and return self with updated defaults.
221
+ *
222
+ * The tag function returns the `stdin` and `stdout` streams of the process as a {@link DuplexPair}.
223
+ *
224
+ * It is an alias of {@link $.io}.
225
+ */
65
226
  through: Dollar<DuplexPair<R>>;
227
+
228
+ /**
229
+ * The tag function that can be used as a template string.
230
+ * It can take an options object and return self with updated defaults.
231
+ *
232
+ * The tag function returns the `stdin` and `stdout` streams of the process as a {@link DuplexPair}.
233
+ *
234
+ * It is an alias of {@link $.through}.
235
+ */
66
236
  io: Dollar<DuplexPair<R>>;
67
237
  }
68
238
 
239
+ /**
240
+ * The $ function with custom properties ({@link $.from}, {@link $.to}, {@link $.through}, {@link $.io}).
241
+ * When used as a tag function with a template string it starts a new process and returns
242
+ * a promise with the simplified result (exit code, signal code, and the "killed" status).
243
+ * Or it can take an options object and return self with updated defaults.
244
+ */
69
245
  export declare const $: DollarImpl;
70
246
 
247
+ /**
248
+ * Options for the shell functions.
249
+ */
71
250
  export interface ShellOptions extends SpawnOptions {
251
+ /**
252
+ * Specify the path of the shell to use.
253
+ */
72
254
  shellPath?: string;
255
+ /**
256
+ * Specify the arguments of the shell.
257
+ */
73
258
  shellArgs?: string[];
74
259
  }
75
260
 
261
+ /**
262
+ * The shell function that can be used as a template string.
263
+ * It can take an options object and return self with updated defaults.
264
+ *
265
+ * When used as a tag function with a template string it starts a new process and returns
266
+ * a sub-process object that can be used to control the process.
267
+ */
76
268
  export declare const shell: Dollar<Subprocess, ShellOptions>;
269
+ /**
270
+ * The shell function that can be used as a template string.
271
+ * It can take an options object and return self with updated defaults.
272
+ *
273
+ * When used as a tag function with a template string it starts a new process and returns
274
+ * a sub-process object that can be used to control the process.
275
+ *
276
+ * This is an alias of `shell`.
277
+ */
77
278
  export declare const sh = shell;
78
279
 
79
- interface ShellImpl<R = string> extends Dollar<Promise<DollarResult>, ShellOptions> {
280
+ /**
281
+ * The type of the $sh function.
282
+ */
283
+ interface ShellImpl<R = any> extends Dollar<Promise<DollarResult>, ShellOptions> {
284
+ /**
285
+ * The tag function that can be used as a template string.
286
+ * It can take an options object and return self with updated defaults.
287
+ *
288
+ * The tag function runs a shell command and returns the `stdout` stream of the process as a `ReadableStream` .
289
+ */
80
290
  from: Dollar<ReadableStream<R>, ShellOptions>;
291
+
292
+ /**
293
+ * The tag function that can be used as a template string.
294
+ * It can take an options object and return self with updated defaults.
295
+ *
296
+ * The tag function runs a shell command and returns the `stdin` stream of the process as a `WritableStream` .
297
+ */
81
298
  to: Dollar<WritableStream<R>, ShellOptions>;
299
+
300
+ /**
301
+ * The tag function that can be used as a template string.
302
+ * It can take an options object and return self with updated defaults.
303
+ *
304
+ * The tag function runs a shell command and returns the `stdin` and `stdout` streams of the process as a {@link DuplexPair}.
305
+ *
306
+ * It is an alias of {@link $sh.io}.
307
+ */
82
308
  through: Dollar<DuplexPair<R>, ShellOptions>;
309
+
310
+ /**
311
+ * The tag function that can be used as a template string.
312
+ * It can take an options object and return self with updated defaults.
313
+ *
314
+ * The tag function runs a shell command and returns the `stdin` and `stdout` streams of the process as a {@link DuplexPair}.
315
+ *
316
+ * It is an alias of {@link $sh.through}.
317
+ */
83
318
  io: Dollar<DuplexPair<R>, ShellOptions>;
84
319
  }
85
320
 
321
+ /**
322
+ * The $sh function with custom properties ({@link $sh.from}, {@link $sh.to}, {@link $sh.through}, {@link $sh.io}).
323
+ *
324
+ * When used as a tag function with a template string it starts a new shell process and returns
325
+ * a promise with the simplified result (exit code, signal code, and the "killed" status).
326
+ * Or it can take an options object and return self with updated defaults.
327
+ */
86
328
  export declare const $sh: ShellImpl;
87
329
 
88
330
  export default $;