UNPKG

45.1 kBTypeScriptView Raw
1// Type definitions for ShellJS 0.8
2// Project: http://shelljs.org, http://github.com/shelljs/shelljs
3// Definitions by: Niklas Mollenhauer <https://github.com/nikeee>
4// Vojtech Jasny <https://github.com/voy>
5// George Kalpakas <https://github.com/gkalpak>
6// Paul Huynh <https://github.com/pheromonez>
7// Alexander Futász <https://github.com/aldafu>
8// ExE Boss <https://github.com/ExE-Boss>
9// Mirco Sanguineti <https://github.com/msanguineti>
10// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
12/// <reference types="node"/>
13
14import child = require("child_process");
15import glob = require("glob");
16
17/**
18 * Changes the current working directory dir for the duration of the script.
19 * Changes to the home directory if no argument is supplied.
20 *
21 * @param dir Directory to change to.
22 * @return Object with shell exit code, stderr and stdout.
23 */
24export function cd(dir?: string): ShellString;
25
26/**
27 * Returns the current directory.
28 * @return The current directory.
29 */
30export function pwd(): ShellString;
31
32export interface ListFunction {
33 /**
34 * Returns array of files in the given path, or in current directory if no path provided.
35 *
36 * @param options Available options:
37 * - `-R`: recursive
38 * - `-A`: all files (include files beginning with ., except for . and ..)
39 * - `-L`: follow symlinks
40 * - `-d`: list directories themselves, not their contents
41 * - `-l`: list objects representing each file, each with fields containing
42 * `ls -l` output fields. See fs.Stats for more info
43 * @param paths Paths to search.
44 * @return An array of files in the given path(s).
45 */
46 (options: string, paths: string[]): ShellArray;
47 (options: string, ...paths: string[]): ShellArray;
48
49 /**
50 * Returns array of files in the given path, or in current directory if no path provided.
51 *
52 * @param paths Paths to search.
53 * @return An array of files in the given path(s).
54 */
55 (paths: string[]): ShellArray;
56 (...paths: string[]): ShellArray;
57}
58
59/**
60 * Returns array of files in the given path, or in current directory if no path provided.
61 *
62 * @param options Available options:
63 * - `-R`: recursive
64 * - `-A`: all files (include files beginning with ., except for . and ..)
65 * - `-L`: follow symlinks
66 * - `-d`: list directories themselves, not their contents
67 * - `-l`: list objects representing each file, each with fields containing
68 * `ls -l` output fields. See fs.Stats for more info
69 * @param paths Paths to search.
70 * @return An array of files in the given path(s).
71 */
72export const ls: ListFunction;
73
74export interface FindFunction {
75 /**
76 * Returns array of all files (however deep) in the given paths.
77 *
78 * @param path The path(s) to search.
79 * @return An array of all files (however deep) in the given path(s).
80 */
81 (path: string[]): ShellArray;
82 (...path: string[]): ShellArray;
83}
84
85/**
86 * Returns array of all files (however deep) in the given paths.
87 *
88 * @param path The path(s) to search.
89 * @return An array of all files (however deep) in the given path(s).
90 */
91export const find: FindFunction;
92
93export interface CopyFunction {
94 /**
95 * Copies files. The wildcard `*` is accepted.
96 *
97 * @param options Available options:
98 * - `-f`: force (default behavior)
99 * - `-n`: no-clobber
100 * - `-u`: only copy if source is newer than dest
101 * - `-r`, `-R`: recursive
102 * - `-L`: follow symlinks
103 * - `-P`: don't follow symlinks
104 * @param source The source.
105 * @param dest The destination.
106 * @return Object with shell exit code, stderr and stdout.
107 */
108 (options: string, source: string | string[], dest: string): ShellString;
109
110 /**
111 * Copies files. The wildcard `*` is accepted.
112 *
113 * @param source The source.
114 * @param dest The destination.
115 * @return Object with shell exit code, stderr and stdout.
116 */
117 (source: string | string[], dest: string): ShellString;
118}
119
120/**
121 * Copies files. The wildcard `*` is accepted.
122 *
123 * @param options Available options:
124 * - `-f`: force (default behavior)
125 * - `-n`: no-clobber
126 * - `-u`: only copy if source is newer than dest
127 * - `-r`, -R: recursive
128 * - `-L`: follow symlinks
129 * - `-P`: don't follow symlinks
130 * @param source The source.
131 * @param dest The destination.
132 * @return Object with shell exit code, stderr and stdout.
133 */
134export const cp: CopyFunction;
135
136export interface RemoveFunction {
137 /**
138 * Removes files. The wildcard `*` is accepted.
139 *
140 * @param options Available options:
141 * - `-f`: force
142 * - `-r`, `-R`: recursive
143 * @param files Files to remove.
144 * @return Object with shell exit code, stderr and stdout.
145 */
146 (options: string, files: string[]): ShellString;
147 (options: string, ...files: string[]): ShellString;
148
149 /**
150 * Removes files. The wildcard `*` is accepted.
151 *
152 * @param files Files to remove.
153 * @return Object with shell exit code, stderr and stdout.
154 */
155 (files: string[]): ShellString;
156 (...files: string[]): ShellString;
157}
158
159/**
160 * Removes files. The wildcard `*` is accepted.
161 *
162 * @param options Available options:
163 * - `-f` (force),
164 * - `-r`, `-R` (recursive)
165 * @param files Files to remove.
166 * @return Object with shell exit code, stderr and stdout.
167 */
168export const rm: RemoveFunction;
169
170export interface MoveFunction {
171 /**
172 * Moves files. The wildcard `*` is accepted.
173 *
174 * @param options Available options:
175 * - `-f`: force (default behavior)
176 * - `-n`: no-clobber
177 * @param source The source.
178 * @param dest The destination.
179 * @return Object with shell exit code, stderr and stdout.
180 */
181 (options: string, source: string | string[], dest: string): ShellString;
182
183 /**
184 * Moves files. The wildcard `*` is accepted.
185 *
186 * @param source The source.
187 * @param dest The destination.
188 * @return Object with shell exit code, stderr and stdout.
189 */
190 (source: string | string[], dest: string): ShellString;
191}
192
193/**
194 * Moves files. The wildcard `*` is accepted.
195 *
196 * @param options Available options:
197 * - `-f`: force (default behavior)
198 * - `-n`: no-clobber
199 * @param source The source.
200 * @param dest The destination.
201 * @return Object with shell exit code, stderr and stdout.
202 */
203export const mv: MoveFunction;
204
205export interface MkdirFunction {
206 /**
207 * Creates directories.
208 *
209 * @param options Available options:
210 * - `-p`: full paths, will create intermediate dirs if necessary
211 * @param dir The directories to create.
212 * @return Object with shell exit code, stderr and stdout.
213 */
214 (options: string, dir: string[]): ShellString;
215 (options: string, ...dir: string[]): ShellString;
216
217 /**
218 * Creates directories.
219 *
220 * @param dir Directories to create.
221 * @return Object with shell exit code, stderr and stdout.
222 */
223 (dir: string[]): ShellString;
224 (...dir: string[]): ShellString;
225}
226
227/**
228 * Creates directories.
229 *
230 * @param options Available options:
231 * - `-p`: full paths, will create intermediate dirs if necessary
232 * @param dir The directories to create.
233 * @return Object with shell exit code, stderr and stdout.
234 */
235export const mkdir: MkdirFunction;
236
237/**
238 * Evaluates expression using the available primaries and returns corresponding value.
239 *
240 * @param option Valid options:
241 * - `-b`: true if path is a block device;
242 * - `-c`: true if path is a character device;
243 * - `-d`: true if path is a directory;
244 * - `-e`: true if path exists;
245 * - `-f`: true if path is a regular file;
246 * - `-L`: true if path is a symbolic link;
247 * - `-p`: true if path is a pipe (FIFO);
248 * - `-S`: true if path is a socket
249 * @param path The path.
250 * @return See option parameter.
251 */
252export function test(option: TestOptions, path: string): boolean;
253
254export type TestOptions = "-b" | "-c" | "-d" | "-e" | "-f" | "-L" | "-p" | "-S";
255
256export interface CatFunction {
257 /**
258 * Returns a string containing the given file, or a concatenated string
259 * containing the files if more than one file is given (a new line character
260 * is introduced between each file).
261 *
262 * @param files Files to use. Wildcard `*` accepted.
263 * @return A string containing the given file, or a concatenated string
264 * containing the files if more than one file is given
265 * (a new line character is introduced between each file).
266 */
267 (files: string[]): ShellString;
268 (...files: string[]): ShellString;
269}
270
271/**
272 * Returns a string containing the given file, or a concatenated string
273 * containing the files if more than one file is given (a new line character
274 * is introduced between each file).
275 *
276 * @param files Files to use. Wildcard `*` accepted.
277 * @return A string containing the given file, or a concatenated string
278 * containing the files if more than one file is given
279 * (a new line character is introduced between each file).
280 */
281export const cat: CatFunction;
282
283export interface SedFunction {
284 /**
285 * Reads an input string from file and performs a JavaScript `replace()`
286 * on the input using the given search regex and replacement string or function.
287 *
288 * @param options Available options:
289 * - `-i`: Replace contents of 'file' in-place. Note that no backups will be created!
290 * @param searchRegex The regular expression to use for search.
291 * @param replacement The replacement.
292 * @param files The files to process.
293 * @return The new string after replacement.
294 */
295 (
296 options: string,
297 searchRegex: string | RegExp,
298 replacement: string,
299 files: string[]
300 ): ShellString;
301 (
302 options: string,
303 searchRegex: string | RegExp,
304 replacement: string,
305 ...files: string[]
306 ): ShellString;
307
308 /**
309 * Reads an input string from file and performs a JavaScript `replace()`
310 * on the input using the given search regex and replacement string or function.
311 *
312 * @param searchRegex The regular expression to use for search.
313 * @param replacement The replacement.
314 * @param files The files to process.
315 * @return The new string after replacement.
316 */
317 (
318 searchRegex: string | RegExp,
319 replacement: string,
320 files: string[]
321 ): ShellString;
322 (
323 searchRegex: string | RegExp,
324 replacement: string,
325 ...files: string[]
326 ): ShellString;
327}
328
329/**
330 * Reads an input string from file and performs a JavaScript `replace()`
331 * on the input using the given search regex and replacement string or function.
332 *
333 * @param options Available options:
334 * - `-i`: Replace contents of 'file' in-place. Note that no backups will be created!
335 * @param searchRegex The regular expression to use for search.
336 * @param replacement The replacement.
337 * @param files The files to process.
338 * @return The new string after replacement.
339 */
340export const sed: SedFunction;
341
342export interface GrepFunction {
343 /**
344 * Reads input string from given files and returns a string containing all lines
345 * of the file that match the given `regex_filter`. Wildcard `*` accepted.
346 *
347 * @param options Available options:
348 * - `-v`: Inverse the sense of the regex and print
349 * the lines not matching the criteria.
350 * - `-l`: Print only filenames of matching files
351 * @param regex_filter The regular expression to use.
352 * @param files The files to process.
353 * @return Returns a string containing all lines of the file that match the given regex_filter.
354 */
355 (
356 options: string,
357 regex_filter: string | RegExp,
358 files: string[]
359 ): ShellString;
360 (
361 options: string,
362 regex_filter: string | RegExp,
363 ...files: string[]
364 ): ShellString;
365
366 /**
367 * Reads input string from given files and returns a string containing all lines
368 * of the file that match the given `regex_filter`. Wildcard `*` accepted.
369 *
370 * @param regex_filter The regular expression to use.
371 * @param files The files to process.
372 * @return Returns a string containing all lines of the file that match the given `regex_filter`.
373 */
374 (regex_filter: string | RegExp, files: string[]): ShellString;
375 (regex_filter: string | RegExp, ...files: string[]): ShellString;
376}
377
378/**
379 * Reads input string from given files and returns a string containing all lines
380 * of the file that match the given `regex_filter`. Wildcard `*` accepted.
381 *
382 * @param options Available options:
383 * - `-v`: Inverse the sense of the regex and print
384 * the lines not matching the criteria.
385 * - `-l`: Print only filenames of matching files
386 * @param regex_filter The regular expression to use.
387 * @param files The files to process.
388 * @return Returns a string containing all lines of the file that match the given `regex_filter`.
389 */
390export const grep: GrepFunction;
391
392/**
393 * Searches for command in the system's PATH. On Windows looks for .exe, .cmd, and .bat extensions.
394 *
395 * @param command The command to search for.
396 * @return Returns string containing the absolute path to the command.
397 */
398export function which(command: string): ShellString;
399
400export interface EchoFunction {
401 /**
402 * Prints string to stdout, and returns string with additional utility methods like .to().
403 *
404 * @param options Available options:
405 * - `-e`: interpret backslash escapes (default)
406 * - `-n`: remove trailing newline from output
407 * @param text The text to print.
408 * @return Returns the string that was passed as argument.
409 */
410 (options: string, ...text: string[]): ShellString;
411
412 /**
413 * Prints string to stdout, and returns string with additional utility methods like .to().
414 *
415 * @param text The text to print.
416 * @return Returns the string that was passed as argument.
417 */
418 (...text: string[]): ShellString;
419}
420
421/**
422 * Prints string to stdout, and returns string with additional utility methods like .to().
423 *
424 * @param options Available options:
425 * - `-e`: interpret backslash escapes (default)
426 * - `-n`: remove trailing newline from output
427 * @param text The text to print.
428 * @return Returns the string that was passed as argument.
429 */
430export const echo: EchoFunction;
431
432export interface PushDirFunction {
433 /**
434 * Saves the current directory on the top of the directory stack and then cd to dir.
435 * With no arguments, `pushd` exchanges the top two directories.
436 *
437 * @param options Available options:
438 * - `-n`: Suppresses the normal change of directory when adding directories
439 * to the stack, so that only the stack is manipulated
440 * - `-q`: Suppresses output to the console.
441 * @param dir Brings the Nth directory (counting from the left of the list printed by dirs,
442 * starting with zero) to the top of the list by rotating the stack.
443 * @return Returns an array of paths in the stack.
444 */
445 (options: string, dir: "+N"): ShellArray;
446
447 /**
448 * Saves the current directory on the top of the directory stack and then cd to dir.
449 * With no arguments, `pushd` exchanges the top two directories.
450 *
451 * @param options Available options:
452 * - `-n`: Suppresses the normal change of directory when adding directories
453 * to the stack, so that only the stack is manipulated
454 * - `-q`: Suppresses output to the console.
455 * @param dir Brings the Nth directory (counting from the right of the list printed by dirs,
456 * starting with zero) to the top of the list by rotating the stack.
457 * @return Returns an array of paths in the stack.
458 */
459 (options: string, dir: "-N"): ShellArray;
460
461 /**
462 * Saves the current directory on the top of the directory stack and then cd to dir.
463 * With no arguments, `pushd` exchanges the top two directories.
464 *
465 * @param options Available options:
466 * - `-n`: Suppresses the normal change of directory when adding directories
467 * to the stack, so that only the stack is manipulated
468 * - `-q`: Suppresses output to the console.
469 * @param dir Makes the current working directory be the top of the stack,
470 * and then executes the equivalent of `cd dir`.
471 * @return Returns an array of paths in the stack.
472 */
473 (options: string, dir: string): ShellArray;
474
475 /**
476 * Saves the current directory on the top of the directory stack and then cd to dir.
477 * With no arguments, `pushd` exchanges the top two directories.
478 *
479 * @param dir Brings the Nth directory (counting from the left of the list printed by dirs,
480 * starting with zero) to the top of the list by rotating the stack.
481 * @return Returns an array of paths in the stack.
482 */
483 (dir: "+N"): ShellArray;
484
485 /**
486 * Saves the current directory on the top of the directory stack and then cd to dir.
487 * With no arguments, `pushd` exchanges the top two directories.
488 *
489 * @param dir Brings the Nth directory (counting from the right of the list printed by dirs,
490 * starting with zero) to the top of the list by rotating the stack.
491 * @return Returns an array of paths in the stack.
492 */
493 (dir: "-N"): ShellArray;
494
495 /**
496 * Saves the current directory on the top of the directory stack and then cd to dir.
497 * With no arguments, `pushd` exchanges the top two directories.
498 *
499 * @param dir Makes the current working directory be the top of the stack,
500 * and then executes the equivalent of cd dir.
501 * @return Returns an array of paths in the stack.
502 */
503 (dir: string): ShellArray;
504
505 /**
506 * Saves the current directory on the top of the directory stack and then cd to dir.
507 * With no arguments, `pushd` exchanges the top two directories.
508 *
509 * @return Returns an array of paths in the stack.
510 */
511 (): ShellArray;
512}
513
514/**
515 * Saves the current directory on the top of the directory stack and then cd to dir.
516 * With no arguments, `pushd` exchanges the top two directories.
517 *
518 * @param options Available options:
519 * - `-n`: Suppresses the normal change of directory when adding directories
520 * to the stack, so that only the stack is manipulated
521 * - `-q`: Suppresses output to the console.
522 * @param dir Makes the current working directory be the top of the stack,
523 * and then executes the equivalent of `cd dir`.
524 * @return Returns an array of paths in the stack.
525 */
526export const pushd: PushDirFunction;
527
528export interface PopDirFunction {
529 /**
530 * When no arguments are given, popd removes the top directory from the stack
531 * and performs a `cd` to the new top directory.
532 *
533 * The elements are numbered from 0 starting at the first directory listed with dirs;
534 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
535 *
536 * @param options Available options:
537 * - `-n`: Suppresses the normal change of directory when removing directories
538 * from the stack, so that only the stack is manipulated
539 * - `-q`: Suppresses output to the console.
540 * @param dir Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
541 * @return Returns an array of paths in the stack.
542 */
543 (options: string, dir: "+N"): ShellArray;
544
545 /**
546 * When no arguments are given, popd removes the top directory from the stack
547 * and performs a `cd` to the new top directory.
548 *
549 * The elements are numbered from 0 starting at the first directory listed with dirs;
550 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
551 *
552 * @param options Available options:
553 * - `-n`: Suppresses the normal change of directory when removing directories
554 * from the stack, so that only the stack is manipulated
555 * - `-q`: Suppresses output to the console.
556 * @param dir Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
557 * @return Returns an array of paths in the stack.
558 */
559 (options: string, dir: "-N"): ShellArray;
560
561 /**
562 * When no arguments are given, popd removes the top directory from the stack
563 * and performs a `cd` to the new top directory.
564 *
565 * The elements are numbered from 0 starting at the first directory listed with dirs;
566 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
567 *
568 * @param options Available options:
569 * - `-n`: Suppresses the normal change of directory when removing directories
570 * from the stack, so that only the stack is manipulated
571 * - `-q`: Suppresses output to the console.
572 * @param dir You can only use -N and +N.
573 * @return Returns an array of paths in the stack.
574 */
575 (options: string, dir: string): ShellArray;
576
577 /**
578 * When no arguments are given, popd removes the top directory from the stack
579 * and performs a `cd` to the new top directory.
580 *
581 * The elements are numbered from 0 starting at the first directory listed with dirs;
582 * i.e., popd is equivalent to popd +0. Returns an array of paths in the stack.
583 *
584 * @param dir Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
585 * @return Returns an array of paths in the stack.
586 */
587 (dir: "+N"): ShellArray;
588
589 /**
590 * When no arguments are given, popd removes the top directory from the stack
591 * and performs a `cd` to the new top directory.
592 *
593 * The elements are numbered from 0 starting at the first directory listed with dirs;
594 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
595 *
596 * @param dir Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
597 * @return Returns an array of paths in the stack.
598 */
599 (dir: "-N"): ShellArray;
600
601 /**
602 * When no arguments are given, popd removes the top directory from the stack
603 * and performs a `cd` to the new top directory.
604 *
605 * The elements are numbered from 0 starting at the first directory listed with dirs;
606 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
607 *
608 * @param dir You can only use -N and +N.
609 * @return Returns an array of paths in the stack.
610 */
611 (dir: string): ShellArray;
612
613 /**
614 * When no arguments are given, popd removes the top directory from the stack
615 * and performs a `cd` to the new top directory.
616 *
617 * The elements are numbered from 0 starting at the first directory listed with dirs;
618 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
619 *
620 * @return Returns an array of paths in the stack.
621 */
622 (): ShellArray;
623}
624
625/**
626 * When no arguments are given, popd removes the top directory from the stack
627 * and performs a `cd` to the new top directory.
628 *
629 * The elements are numbered from 0 starting at the first directory listed with dirs;
630 * i.e., `popd` is equivalent to `popd +0`. Returns an array of paths in the stack.
631 *
632 * @param options Available options:
633 * - `-n`: Suppresses the normal change of directory when removing directories
634 * from the stack, so that only the stack is manipulated
635 * - `-q`: Suppresses output to the console.
636 * @param dir You can only use -N and +N.
637 * @return Returns an array of paths in the stack.
638 */
639export const popd: PopDirFunction;
640
641export interface DirsFunction {
642 /**
643 * Clears the directory stack by deleting all of the elements.
644 *
645 * @param options Clears the directory stack by deleting all of the elements.
646 * @return Returns an array of paths in the stack, or a single path if +N or -N was specified.
647 */
648 (options: "-c"): ShellArray;
649
650 /**
651 * Displays the list of currently remembered directories.
652 *
653 * @param options Displays the Nth directory (counting from the left of the list
654 * printed by dirs when invoked without options), starting with zero.
655 * @return Returns an array of paths in the stack, or a single path if +N or -N was specified.
656 */
657 (options: "+N"): ShellString;
658
659 /**
660 * Displays the list of currently remembered directories.
661 *
662 * @param options Displays the Nth directory (counting from the right of the list
663 * printed by dirs when invoked without options), starting with zero.
664 * @return Returns an array of paths in the stack, or a single path if +N or -N was specified.
665 */
666 (options: "-N"): ShellString;
667
668 /**
669 * Displays the list of currently remembered directories.
670 *
671 * @param options Available options:
672 * - `-c`: Clears the directory stack by deleting all of the elements.
673 * - `-N`: Displays the Nth directory (counting from the right of the list
674 * printed by dirs when invoked without options), starting with zero.
675 * - `+N`: Displays the Nth directory (counting from the left of the list
676 * printed by dirs when invoked without options), starting with zero.
677 * @return Returns an array of paths in the stack, or a single path if +N or -N was specified.
678 */
679 (options: string): ShellArray | ShellString;
680}
681
682/**
683 * Displays the list of currently remembered directories.
684 *
685 * @param options Available options:
686 * - `-c`: Clears the directory stack by deleting all of the elements.
687 * - `-N`: Displays the Nth directory (counting from the right of the list
688 * printed by dirs when invoked without options), starting with zero.
689 * - `+N`: Displays the Nth directory (counting from the left of the list
690 * printed by dirs when invoked without options), starting with zero.
691 * @return Returns an array of paths in the stack, or a single path if +N or -N was specified.
692 */
693export const dirs: DirsFunction;
694
695export interface LinkFunction {
696 /**
697 * Links source to dest. Use `-f` to force the link, should dest already exist.
698 *
699 * @param options Available options:
700 * - `-s`: Create a symbolic link, defaults to a hardlink
701 * - `-f`: Force creation
702 * @param source The source.
703 * @param dest The destination.
704 * @return Object with shell exit code, stderr and stdout.
705 */
706 (options: string, source: string, dest: string): ShellString;
707
708 /**
709 * Links source to dest. Use `-f` to force the link, should dest already exist.
710 *
711 * @param source The source.
712 * @param dest The destination.
713 * @return Object with shell exit code, stderr and stdout.
714 */
715 (source: string, dest: string): ShellString;
716}
717
718/**
719 * Links source to dest. Use `-f` to force the link, should dest already exist.
720 *
721 * @param options Available options:
722 * - `-s`: Create a symbolic link, defaults to a hardlink
723 * - `-f`: Force creation
724 * @param source The source.
725 * @param dest The destination.
726 * @return Object with shell exit code, stderr and stdout.
727 */
728export const ln: LinkFunction;
729
730/**
731 * Exits the current process with the given exit code.
732 *
733 * Equivalent to calling `process.exit(code)`.
734 *
735 * @param code The exit code.
736 */
737export function exit(code?: number): never;
738
739/**
740 * Object containing environment variables (both getter and setter). Shortcut to `process.env`.
741 */
742export const env: NodeJS.ProcessEnv;
743
744export interface ExecFunction {
745 /**
746 * Executes the given command synchronously.
747 *
748 * @param command The command to execute.
749 * @return Returns an object containing the return code and output as string.
750 */
751 (command: string): ShellString;
752
753 /**
754 * Executes the given command synchronously.
755 *
756 * @param command The command to execute.
757 * @param options Silence and synchronous options.
758 * @return Returns an object containing the return code and output as string,
759 * or if `{async: true}` was passed, a `ChildProcess`.
760 */
761 (command: string, options: ExecOptions & { async?: false }): ShellString;
762
763 /**
764 * Executes the given command asynchronously.
765 *
766 * @param command The command to execute.
767 * @param options Silence and synchronous options.
768 * @return Returns an object containing the return code and output as string,
769 * or if `{async: true}` was passed, a `ChildProcess`.
770 */
771 (
772 command: string,
773 options: ExecOptions & { async: true }
774 ): child.ChildProcess;
775
776 /**
777 * Executes the given command.
778 *
779 * @param command The command to execute.
780 * @param options Silence and synchronous options.
781 * @return Returns an object containing the return code and output as string,
782 * or if `{async: true}` was passed, a `ChildProcess`.
783 */
784 (command: string, options: ExecOptions): ShellString | child.ChildProcess;
785
786 /**
787 * Executes the given command synchronously.
788 *
789 * @param command The command to execute.
790 * @param options Silence and synchronous options.
791 * @param callback Receives code and output asynchronously.
792 */
793 (
794 command: string,
795 options: ExecOptions,
796 callback: ExecCallback
797 ): child.ChildProcess;
798
799 /**
800 * Executes the given command synchronously.
801 *
802 * @param command The command to execute.
803 * @param callback Receives code and output asynchronously.
804 */
805 (command: string, callback: ExecCallback): child.ChildProcess;
806}
807
808/**
809 * Executes the given command.
810 *
811 * @param command The command to execute.
812 * @param options Silence and synchronous options.
813 * @param [callback] Receives code and output asynchronously.
814 * @return Returns an object containing the return code and output as string,
815 * or if `{async: true}` or a `callback` was passed, a `ChildProcess`.
816 */
817export const exec: ExecFunction;
818
819export type ExecCallback = (
820 /** The process exit code. */
821 code: number,
822
823 /** The process standard output. */
824 stdout: string,
825
826 /** The process standard error output. */
827 stderr: string
828) => any;
829
830export interface ExecOptions extends child.ExecOptions {
831 /**
832 * Do not echo program output to the console.
833 *
834 * @default false
835 */
836 silent?: boolean;
837
838 /**
839 * Exit when command return code is non-zero.
840 *
841 * @default false
842 */
843 fatal?: boolean;
844
845 /**
846 * Asynchronous execution.
847 *
848 * If a callback is provided, it will be set to `true`, regardless of the passed value.
849 *
850 * @default false
851 */
852 async?: boolean;
853
854 /**
855 * Character encoding to use.
856 *
857 * Affects the values returned by `stdout` and `stderr`,
858 * and what is written to `stdout` and `stderr` when not in silent mode
859 *
860 * @default "utf8"
861 */
862 encoding?: string;
863}
864
865export interface ExecOutputReturnValue {
866 /** The process exit code. */
867 code: number;
868
869 /** The process standard output. */
870 stdout: string;
871
872 /** The process standard error output. */
873 stderr: string;
874}
875
876export interface ShellReturnValue extends ExecOutputReturnValue {
877 /**
878 * Analogous to the redirection operator `>` in Unix, but works with JavaScript strings
879 * (such as those returned by `cat`, `grep`, etc).
880 *
881 * Like Unix redirections, `to()` will overwrite any existing file!
882 *
883 * @param file The file to use.
884 */
885 to(file: string): void;
886
887 /**
888 * Analogous to the redirect-and-append operator `>>` in Unix, but works with JavaScript strings
889 * (such as those returned by `cat`, `grep`, etc).
890 *
891 * @param file The file to append to.
892 */
893 toEnd(file: string): void;
894
895 /**
896 * Returns a string containing the given pipeline, or a concatenated string
897 * containing the pipelines if more than one input stream is given
898 * (a new line character is introduced between each input).
899 *
900 * @return A string containing the given pipeline, or a concatenated string
901 * containing the pipelines if more than one input stream is given
902 * (a new line character is introduced between each input).
903 */
904 cat: CatFunction;
905
906 /**
907 * Executes the given command.
908 *
909 * @param command The command to execute.
910 * @param options Silence and synchronous options.
911 * @param [callback] Receives code and output asynchronously.
912 * @return Returns an object containing the return code and output as string,
913 * or if `{async: true}` or a `callback` was passed, a `ChildProcess`.
914 */
915 exec: ExecFunction;
916
917 /**
918 * Read the start of a pipeline input.
919 */
920 head: HeadFunction;
921
922 /**
923 * Reads input string from given files and returns a string containing all lines
924 * of the file that match the given `regex_filter`. Wildcard `*` accepted.
925 *
926 * @param options Available options:
927 * - `-v`: Inverse the sense of the regex and print
928 * the lines not matching the criteria.
929 * - `-l`: Print only filenames of matching files
930 * @param regex_filter The regular expression to use.
931 * @return Returns a string containing all lines of the file that match the given `regex_filter`.
932 */
933 grep: GrepFunction;
934
935 /**
936 * Reads an input string from pipeline and performs a JavaScript `replace()`
937 * on the input using the given search regex and replacement string or function.
938 *
939 * @param options Available options:
940 * - `-i`: Replace contents of 'file' in-place. Note that no backups will be created!
941 * @param searchRegex The regular expression to use for search.
942 * @param replacement The replacement.
943 * @return The new string after replacement.
944 */
945 sed: SedFunction;
946
947 /**
948 * Return the contents of the pipeline, sorted line-by-line.
949 *
950 * @param options Available options:
951 * - `-r`: Reverse the results
952 * - `-n`: Compare according to numerical value
953 */
954 sort: SortFunction;
955
956 /**
957 * Read the end of a pipeline input.
958 */
959 tail: TailFunction;
960
961 /**
962 * Filter adjacent matching lines from input.
963 *
964 * @param options Available options:
965 * - `-i`: Ignore case while comparing
966 * - `-c`: Prefix lines by the number of occurrences
967 * - `-d`: Only print duplicate lines, one for each group of identical lines
968 */
969 uniq: UniqFunction;
970}
971
972export type ShellString = string & ShellReturnValue;
973
974export type ShellArray = string[] & ShellReturnValue;
975
976export interface ShellStringConstructor {
977 /**
978 * Wraps a string (or array) value. This has all the string (or array) methods,
979 * but also exposes extra methods: `.to()`, `.toEnd()`, and all the pipe-able
980 * methods (ex. `.cat()`, `.grep()`, etc.).
981 *
982 * This can be easily converted into a string by calling `.toString()`.
983 *
984 * This type also exposes the corresponding command's stdout, stderr, and return status
985 * code via the `.stdout` (string), `.stderr` (string), and `.code` (number) properties
986 * respectively.
987 *
988 * Construct signature allows for:
989 *
990 * var foo = new ShellString('hello world');
991 *
992 * as per example in shelljs docs:
993 * https://github.com/shelljs/shelljs#shellstringstr
994 *
995 * @param value The string value to wrap.
996 * @return A string-like object with special methods.
997 */
998 new(value: string): ShellString;
999 new(value: string[]): ShellArray;
1000
1001 /**
1002 * Wraps a string (or array) value. This has all the string (or array) methods,
1003 * but also exposes extra methods: `.to()`, `.toEnd()`, and all the pipe-able
1004 * methods (ex. `.cat()`, `.grep()`, etc.).
1005 *
1006 * This can be easily converted into a string by calling `.toString()`.
1007 *
1008 * This type also exposes the corresponding command's stdout, stderr, and return status
1009 * code via the `.stdout` (string), `.stderr` (string), and `.code` (number) properties
1010 * respectively.
1011 *
1012 * @param value The string value to wrap.
1013 * @return A string-like object with special methods.
1014 */
1015 (value: string): ShellString;
1016 (value: string[]): ShellArray;
1017}
1018
1019export const ShellString: ShellStringConstructor;
1020
1021export interface ChmodFunction {
1022 /**
1023 * Alters the permissions of a file or directory by either specifying the absolute
1024 * permissions in octal form or expressing the changes in symbols.
1025 *
1026 * This command tries to mimic the POSIX behavior as much as possible.
1027 *
1028 * Notable exceptions:
1029 * - In symbolic modes, 'a-r' and '-r' are identical. No consideration is given to the umask.
1030 * - There is no "quiet" option since default behavior is to run silent.
1031 *
1032 * @param options Available options:
1033 * - `-v`: output a diagnostic for every file processed
1034 * - `-c`: like -v but report only when a change is made
1035 * - `-R`: change files and directories recursively
1036 * @param mode The access mode. Can be an octal string or a symbolic mode string.
1037 * @param file The file to use.
1038 * @return Object with shell exit code, stderr and stdout.
1039 */
1040 (options: string, mode: string | number, file: string): ShellString;
1041
1042 /**
1043 * Alters the permissions of a file or directory by either specifying the absolute
1044 * permissions in octal form or expressing the changes in symbols.
1045 *
1046 * This command tries to mimic the POSIX behavior as much as possible.
1047 *
1048 * Notable exceptions:
1049 * - In symbolic modes, 'a-r' and '-r' are identical. No consideration is given to the umask.
1050 * - There is no "quiet" option since default behavior is to run silent.
1051 *
1052 * @param mode The access mode. Can be an octal string or a symbolic mode string.
1053 * @param file The file to use.
1054 * @return Object with shell exit code, stderr and stdout.
1055 */
1056 (mode: string | number, file: string): ShellString;
1057}
1058
1059/**
1060 * Alters the permissions of a file or directory by either specifying the absolute
1061 * permissions in octal form or expressing the changes in symbols.
1062 *
1063 * This command tries to mimic the POSIX behavior as much as possible.
1064 *
1065 * Notable exceptions:
1066 * - In symbolic modes, 'a-r' and '-r' are identical. No consideration is given to the umask.
1067 * - There is no "quiet" option since default behavior is to run silent.
1068 *
1069 * @param options Available options:
1070 * - `-v`: output a diagnostic for every file processed
1071 * - `-c`: like -v but report only when a change is made
1072 * - `-R`: change files and directories recursively
1073 * @param mode The access mode. Can be an octal string or a symbolic mode string.
1074 * @param file The file to use.
1075 * @return Object with shell exit code, stderr and stdout.
1076 */
1077export const chmod: ChmodFunction;
1078
1079// Non-Unix commands
1080
1081/**
1082 * Searches and returns string containing a writeable, platform-dependent temporary directory.
1083 * Follows Python's tempfile algorithm.
1084 *
1085 * @return The temp file path.
1086 */
1087export function tempdir(): ShellString;
1088
1089/**
1090 * Tests if error occurred in the last command.
1091 *
1092 * @return Returns null if no error occurred, otherwise returns string explaining the error
1093 */
1094export function error(): ShellString;
1095
1096export type TouchOptionsLiteral = "-a" | "-c" | "-m" | "-d" | "-r";
1097
1098export interface TouchOptionsArray {
1099 "-d"?: string;
1100 "-r"?: string;
1101}
1102
1103export interface TouchFunction {
1104 (
1105 options: TouchOptionsLiteral | TouchOptionsArray,
1106 files: string[]
1107 ): ShellString;
1108 (
1109 options: TouchOptionsLiteral | TouchOptionsArray,
1110 ...files: string[]
1111 ): ShellString;
1112
1113 (files: string[]): ShellString;
1114 (...files: string[]): ShellString;
1115}
1116
1117/**
1118 * Update the access and modification times of each FILE to the current time.
1119 * A FILE argument that does not exist is created empty, unless `-c` is supplied
1120 */
1121export const touch: TouchFunction;
1122
1123export interface HeadOptions {
1124 /** Show the first <num> lines of the files. */
1125 "-n": number;
1126}
1127
1128export interface HeadFunction {
1129 (options: HeadOptions, files: string[]): ShellString;
1130 (options: HeadOptions, ...files: string[]): ShellString;
1131
1132 (files: string[]): ShellString;
1133 (...files: string[]): ShellString;
1134}
1135
1136/**
1137 * Read the start of a file.
1138 */
1139export const head: HeadFunction;
1140
1141export interface SortFunction {
1142 /**
1143 * Return the contents of the files, sorted line-by-line.
1144 * Sorting multiple files mixes their content (just as unix sort does).
1145 *
1146 * @param options Available options:
1147 * - `-r`: Reverse the results
1148 * - `-n`: Compare according to numerical value
1149 */
1150 (options: string, files: string[]): ShellString;
1151 (options: string, ...files: string[]): ShellString;
1152
1153 /**
1154 * Return the contents of the files, sorted line-by-line.
1155 * Sorting multiple files mixes their content (just as unix sort does).
1156 */
1157 (files: string[]): ShellString;
1158 (...files: string[]): ShellString;
1159}
1160
1161/**
1162 * Return the contents of the files, sorted line-by-line.
1163 * Sorting multiple files mixes their content (just as unix sort does).
1164 *
1165 * @param options Available options:
1166 * - `-r`: Reverse the results
1167 * - `-n`: Compare according to numerical value
1168 */
1169export const sort: SortFunction;
1170
1171export interface TailOptions {
1172 /** Show the last <num> lines of files. */
1173 "-n": number;
1174}
1175
1176export interface TailFunction {
1177 (options: TailOptions, files: string[]): ShellString;
1178 (options: TailOptions, ...files: string[]): ShellString;
1179
1180 (files: string[]): ShellString;
1181 (...files: string[]): ShellString;
1182}
1183
1184/**
1185 * Read the end of a file.
1186 */
1187export const tail: TailFunction;
1188
1189export interface UniqFunction {
1190 /**
1191 * Filter adjacent matching lines from input.
1192 *
1193 * @param options Available options:
1194 * - `-i`: Ignore case while comparing
1195 * - `-c`: Prefix lines by the number of occurrences
1196 * - `-d`: Only print duplicate lines, one for each group of identical lines
1197 */
1198 (options: string, input: string, output?: string): ShellString;
1199
1200 /**
1201 * Filter adjacent matching lines from input.
1202 */
1203 (input: string, output?: string): ShellString;
1204}
1205
1206/**
1207 * Filter adjacent matching lines from input.
1208 *
1209 * @param options Available options:
1210 * - `-i`: Ignore case while comparing
1211 * - `-c`: Prefix lines by the number of occurrences
1212 * - `-d`: Only print duplicate lines, one for each group of identical lines
1213 */
1214export const uniq: UniqFunction;
1215
1216/**
1217 * Sets global configuration variables
1218 * @param options Available options:
1219 * - `+/-e`: exit upon error (`config.fatal`),
1220 * - `+/-v`: verbose: show all commands (`config.verbose`),
1221 * - `+/-f`: disable filename expansion (globbing)
1222 */
1223export function set(options: string): void;
1224
1225// Configuration
1226
1227export interface ShellConfig {
1228 /**
1229 * Suppresses all command output if true, except for echo() calls. Default is false.
1230 */
1231 silent: boolean;
1232
1233 /**
1234 * If true the script will die on errors. Default is false.
1235 */
1236 fatal: boolean;
1237
1238 /**
1239 * Will print each executed command to the screen.
1240 *
1241 * @default false
1242 */
1243 verbose: boolean;
1244
1245 /**
1246 * Passed to glob.sync() instead of the default options ({}).
1247 */
1248 globOptions: glob.IOptions;
1249
1250 /**
1251 * Absolute path of the Node binary. Default is null (inferred).
1252 */
1253 execPath: string | null;
1254
1255 /**
1256 * Reset shell.config to the defaults.
1257 */
1258 reset(): void;
1259}
1260
1261/**
1262 * The shelljs configuration.
1263 */
1264export const config: ShellConfig;