-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyavascript.d.ts
6904 lines (6337 loc) · 232 KB
/
yavascript.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NOTE: This copy of yavascript.d.ts reflects what is in git.
// APIs may differ from what you have installed.
// Run `yavascript --print-types > yavascript.d.ts` to get the types
// corresponding to your specific `yavascript` binary.
// ===============
// ---------------
// YavaScript APIs
// ---------------
// ===============
/**
* Prints a link to the YavaScript help docs for the currently-running version
* of YavaScript.
*
* For the latest help docs, see:
* https://github.com/suchipi/yavascript/blob/main/meta/generated-docs/README.md
*/
declare function help(): void;
/**
* The `yavascript` global contains metadata about the currently-running
* yavascript binary, as well as access to yavascript's compilers for
* compile-to-js languages.
*/
declare const yavascript: {
/**
* The version of the currently-running yavascript binary.
*
* Will be something formatted like one of these:
* - "v0.0.7"
* - "v0.1.3-alpha"
* - "git-286a3a336849"
* - "git-286a3a336849-dirty"
*
* Or, more formally: either a "V" version string or a "GIT" version string:
* - "V" version strings start with the character 'v', followed by a semver
* version string, optionally followed by the character '-' and any
* arbitrary content afterwards.
* - "GIT" version strings start with the prefix "git-", followed by the
* first 12 digits of a git commit SHA, optionally followed by the
* character '-' and any arbitrary content afterwards.
*/
version: string;
/**
* The processor architecture of the currently-running `yavascript` binary.
*/
arch: "x86_64" | "arm64";
/**
* The version of the ecma262 standard supported by the currently-running
* yavascript binary.
*
* Currently, this is always "ES2020", but if future versions of yavascript
* support a newer version of the standard, this will change. In that event,
* this property will always be in the format of "ES" + a year, and will never
* be lower than ES2020.
*/
ecmaVersion: string;
/**
* The compilers yavascript uses internally to load files.
*
* Each function returns a JavaScript source code string.
*/
compilers: {
/**
* The function yavascript uses internally to load JavaScript files.
*
* You might think this would be a no-op, but we do some CommonJS/ECMAScript
* Module interop transformations here.
*/
js(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load [TypeScript JSX](https://www.typescriptlang.org/docs/handbook/jsx.html) files.
*
* yavascript uses [Sucrase 3.35.0](https://sucrase.io/) to load TypeScript JSX syntax. yavascript doesn't do typechecking of TypeScript syntax.
*/
tsx(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load [TypeScript](https://www.typescriptlang.org/) files.
*
* yavascript uses [Sucrase 3.35.0](https://sucrase.io/) to load TypeScript syntax. yavascript doesn't do typechecking of TypeScript syntax.
*/
ts(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load JSX files.
*
* yavascript uses [Sucrase 3.35.0](https://sucrase.io/) to load JSX syntax.
*
* See {@link JSX} for info about configuring JSX pragma, swapping out the
* default `createElement` implementation, etc.
*/
jsx(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load [CoffeeScript](https://coffeescript.org/) files.
*
* yavascript embeds CoffeeScript 2.7.0.
*/
coffee(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load [Civet](https://civet.dev/) files.
*
* yavascript embeds Civet 0.9.0.
*/
civet(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
/**
* The function yavascript uses internally to load files which don't have an
* extension.
*
* It tries to parse the file as each of the following languages, in order,
* until it finds one which doesn't have a syntax error:
*
* - JSX
* - TSX
* - Civet
* - CoffeeScript
*
* If none of the languages work, the file's original content gets used so
* that a syntax error can be reported to the user.
*/
autodetect(
code: string,
options?: { filename?: string; expression?: boolean }
): string;
};
};
/**
* An object representing the process's environment variables. You can read
* from it to read environment variables, write into it to set environment
* variables, and/or delete properties from it to unset environment variables.
* Any value you write will be coerced into a string.
*/
declare const env: { [key: string]: string | undefined };
/**
* A function which parses command line `--flags` into an object of flags and an
* array of positional arguments. This function is opinionated; if it doesn't
* meet your needs, you can parse the {@link scriptArgs} global manually.
*
* Flags `--like-this`, `--like_this`, or `--LIKE_THIS` get converted into
* property names `likeThis` on the returned flags object.
*
* Flags like this: `-v` get converted into into property names like this: `v`
* on the returned flags object.
*
* Anything that appears after `--` is considered a positional argument instead
* of a flag. `--` is not present in the returned positional arguments Array.
*
* `parseScriptArgs` accepts two optional parameters: `hints` and `argv`.
*
* ## hints
*
* If present, `hints` should be an object whose keys are flag names (in
* lowerCamelCase) and whose values indicate what type to treat that flag as.
* Valid property values are `String`, `Boolean`, `Number`, and `Path`. `Path`
* will resolve relative paths into absolute paths for you. If no hints object
* is specified, `parseScriptArgs` will do its best to guess the types.
*
* ## argv
*
* The `argv` parameter, if present, should be an array containing the command
* line flags you want to parse. If you don't provide one, `scriptArgs.slice(2)`
* will be used (we slice 2 in order to skip the yavascript binary and script
* name). If you pass in an array here, it should only contain command-line
* flags, not the binary being called.
*
* ## Return Value
*
* `parseScriptArgs` returns an object with three properties: `flags`, `args`,
* and `metadata`.
*
* - `flags` is an object whose keys are lowerCamelCase flag names and whose
* values are strings, booleans, numbers, or `Path`s corresponding to the
* input command-line args.
* - `args` is an Array of positional arguments, as found on the command-line.
* - `metadata` contains information about what name and type the flags got
* mapped to.
*
* @param hints - An object whose keys are flag names (in lowerCamelCase) and
* whose values indicate what type to treat that flag as. Valid property values
* are `String`, `Boolean`, `Number`, and `Path`. `Path` will resolve relative
* paths into absolute paths for you. If no hints object is specified,
* `parseScriptArgs` will do its best to guess, based on the command-line args.
* @param argv - An array containing the command line flags you want to parse.
* If unspecified, `scriptArgs.slice(2)` will be used (we slice 2 in order to
* skip the yavascript binary and script name). If you pass in an array here, it
* should only contain command-line flags, not the binary being called.
*
* @returns A {@link ParseScriptArgsResult}, which is an object with three
* properties: `flags`, `args`, and `metadata`. `flags` is an object whose keys
* are camelCase flag names and whose values are strings, booleans, numbers, or
* `Path`s corresponding to the input command-line args. `args` is an Array of
* positional arguments, as found on the command-line. `metadata` contains
* information about what name and type the flags got mapped to.
*/
declare function parseScriptArgs(
hints?: {
[key: string]: typeof String | typeof Boolean | typeof Number | typeof Path;
},
args?: Array<string>
): ParseScriptArgsResult;
/**
* The return type of {@link parseScriptArgs}.
*
* The `flags` property contains the values for any command-line `--flags`, with
* key names converted to `lowerCamelCase`.
*
* The `args` property contains an array of those command-line arguments which
* weren't associated with a flag.
*
* The `metadata` property contains information about the parsing process,
* including what case changes were applied to the keys, which hints were used,
* and which properties had their type guessed because no corresponding hint was
* available.
*/
declare interface ParseScriptArgsResult {
/**
* The values for any command-line `--flags`, with key names converted to `lowerCamelCase`.
*/
flags: { [key: string]: any };
/**
* An array of those command-line arguments which weren't associated with a flag.
*/
args: Array<string>;
/**
* Information about the parsing process, including what case changes were
* applied to the keys, which hints were used, and which properties had their
* type guessed because no corresponding hint was available.
*/
metadata: {
/**
* An object whose keys are the verbatim flags from the command-line, and
* whose values are the lowerCamelCase names they were converted to in the
* `flags` property of the {@link ParseScriptArgsResult}.
*/
keys: {
[key: string]: string | undefined;
};
/**
* An object whose keys are the lowerCamelCase flag names, and whose values
* are strings indicating the hint values that were specified for those
* flags.
*/
hints: {
[key: string]: "path" | "number" | "boolean" | "string" | undefined;
};
/**
* An object indicating which flags we inferred the type of, because no
* corresponding hint was present.
*
* The keys are the lowerCamelCase flag names, and the values are strings
* indicating what type we guessed for that flag.
*
* If you're seeing incorrect inference, consider passing a `hints` argument
* to {@link parseScriptArgs}.
*/
guesses: {
[key: string]: "number" | "boolean" | "string" | undefined;
};
};
}
/**
* Read the contents of a file from disk.
*
* With no options specified, it reads the file as UTF-8 and returns a string:
*
* ```ts
* const contents = readFile("README.md");
* console.log(contents);
* // "# yavascript\n\nYavaScript is a cross-platform bash-like script runner and repl which is distributed as a single\nstatically-linked binary..."
* ```
*
* But, if you pass `{ binary: true }` as the second argument, it returns an
* ArrayBuffer containing the raw bytes from the file:
*
* ```ts
* const contents = readFile("README.md", { binary: true });
* console.log(contents);
* // ArrayBuffer {
* // │0x00000000│ 23 20 79 61 76 61 73 63 72 69 70 74 0A 0A 59 61
* // │0x00000010│ 76 61 53 63 72 69 70 74 20 69 73 20 61 20 63 72
* // │0x00000020│ 6F 73 73 2D 70 6C 61 74 66 6F 72 6D 20 62 61 73
* // │0x00000030│ 68 2D 6C 69 6B 65 20 73 63 72 69 70 74 20 72 75
* // ...
* ```
*/
declare const readFile: {
/**
* Read the contents of a file from disk, as a UTF-8 string.
*/
(path: string | Path): string;
/**
* Read the contents of a file from disk, as a UTF-8 string.
*/
(path: string | Path, options: {}): string;
/**
* Read the contents of a file from disk, as a UTF-8 string.
*/
(path: string | Path, options: { binary: false }): string;
/**
* Read the contents of a file from disk, as an ArrayBuffer.
*/
(path: string | Path, options: { binary: true }): ArrayBuffer;
};
/**
* Write the contents of a string or ArrayBuffer to a file.
*
* Strings are written using the UTF-8 encoding.
*/
declare function writeFile(
path: string | Path,
data: string | ArrayBuffer
): void;
/**
* Function which returns true if the path points to a regular file.
*/
declare function isFile(path: string | Path): boolean;
/**
* Function which returns true if the path points to a directory, or if the
* path points to a symlink which points to a directory. Otherwise, it returns
* false.
*/
declare function isDir(path: string | Path): boolean;
/**
* Returns true if the path points to a symlink.
*/
declare function isLink(path: string | Path): boolean;
/**
* Returns true if the resource at the provided path can be executed by the
* current user.
*
* If nothing exists at that path, an error will be thrown.
*/
declare function isExecutable(path: string | Path): boolean;
/**
* Returns true if the resource at the provided path can be read by the current
* user.
*
* If nothing exists at that path, an error will be thrown.
*/
declare function isReadable(path: string | Path): boolean;
/**
* Returns true if a resource at the provided path could be written to by the
* current user.
*/
declare function isWritable(path: string | Path): boolean;
/**
* Delete the file or directory at the specified path.
*
* If the directory isn't empty, its contents will be deleted, too.
*
* Provides the same functionality as the command `rm -rf`.
*/
declare function remove(path: string | Path): void;
/**
* Returns true if a file or directory exists at the specified path.
*
* Provides the same functionality as the command `test -e`.
*/
declare function exists(path: string | Path): boolean;
/**
* Copies a file or folder from one location to another.
* Folders are copied recursively.
*
* Provides the same functionality as the command `cp -R`.
*/
declare function copy(
from: string | Path,
to: string | Path,
options?: CopyOptions
): void;
/**
* Options for {@link copy}.
*/
declare type CopyOptions = {
/**
* What to do when attempting to copy something into a location where
* something else already exists.
*
* Defaults to "error".
*/
whenTargetExists?: "overwrite" | "skip" | "error";
/** Options which control logging. */
logging?: {
/**
* If provided, this function will be called multiple times as `copy`
* traverses the filesystem, to help you understand what's going on and/or
* troubleshoot things. In most cases, it makes sense to use a logging
* function here, like so:
*
* ```js
* copy("./source", "./destination", {
* logging: { trace: console.log },
* });
* ```
*
* Defaults to the current value of {@link logger.trace}. `logger.trace`
* defaults to a no-op function.
*/
trace?: (...args: Array<any>) => void;
/**
* An optional, user-provided logging function to be used for informational
* messages.
*
* Defaults to the current value of {@link logger.info}. `logger.info`
* defaults to a function which writes to stderr.
*/
info?: (...args: Array<any>) => void;
};
};
/**
* Rename the file or directory at the specified path.
*
* Provides the same functionality as the command `mv`.
*/
declare function rename(from: string | Path, to: string | Path): void;
/**
* A class which represents a filesystem path. The class contains various
* methods that make it easy to work with filesystem paths; there are methods
* for adding/removing path components, converting between absolute and relative
* paths, getting the basename and dirname, and more.
*
* All functions in yavascript which accept path strings as arguments also
* accept Path objects. As such, it is recommended that all filesystem paths in
* your programs are Path objects rather than strings.
*
* Every Path object has two properties: `segments` and `separator`. `segments`
* is an Array of strings containing all the non-slash portions of the path. For
* example, the path "one/two/three" would have segments `["one", "two",
* "three"]`. `separator` is which slash is used to separate the segments;
* either `"/"` or `"\"`.
*
* A Path object can represent either a POSIX-style path or a win32-style path.
* For the win32 style, UNC paths are supported. POSIX-style paths starting with
* "/" (eg. "/usr/bin") have an empty string at the beginning of their segments
* array to represent the left-hand-side of the leading slash. For instance,
* "/usr/bin" would have segments `["", "usr", "bin"]`.
*/
declare class Path {
/**
* The character used to separate path segments on the current operating
* system where yavascript is running.
*
* Its value is either a forward slash (`"/"`) or a backslash (`"\"`). Its value
* is a backslash on windows, and a forward slash on all other operating
* systems.
*/
static readonly OS_SEGMENT_SEPARATOR: "/" | "\\";
/**
* The character used to separate entries within the system's `PATH`
* environment variable on the current operating system where yavascript is
* running.
*
* The `PATH` environment variable contains a list of folders wherein
* command-line programs can be found, separated by either a colon (`:`) or a
* semicolon (`;`). The value of `OS_ENV_VAR_SEPARATOR` is a semicolon on
* windows, and a colon on all other operating systems.
*
* The `PATH` environment variable can be accessed by yavascript programs via
* `env.PATH`. Therefore, one can contain a list of all entries in the `PATH`
* environment variable via:
*
* ```ts
* const folders: Array<string> = env.PATH.split(Path.OS_ENV_VAR_SEPARATOR);
* ```
*/
static readonly OS_ENV_VAR_SEPARATOR: ":" | ";";
/**
* A Set of filename extension strings that command-line programs may end with
* on the current operating system where yavascript is running. For instance,
* on Windows, programs often end with ".exe". Each of these strings contains
* a leading dot (`.`).
*
* On windows, this value is based on the `PATHEXT` environment variable,
* which defaults to ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
* on Windows Vista and up. If `PATHEXT` is not defined, that default value is
* used.
*
* On all other operating systems, this Set is empty.
*/
static readonly OS_PROGRAM_EXTENSIONS: ReadonlySet<string>;
/**
* Converts a string (or array of strings) into an array of path segment
* strings (the parts between the slashes).
*
* Example:
*
* ```ts
* const input = ["hi", "there/every/one", "yeah\\yup"];
* const result = Path.splitToSegments(input);
* // result is ["hi", "there", "every", "one", "yeah", "yup"]
* ```
*/
static splitToSegments(inputParts: Array<string> | string): Array<string>;
/**
* Searches the provided path string or strings for a path separator character
* (either forward slash or backslash), and returns the one it finds. If
* neither is found, it returns the `fallback` arg, which defaults to the
* current OS's path segment separator (`Path.OS_SEGMENT_SEPARATOR`).
*/
static detectSeparator<Fallback extends string | null = string>(
input: Array<string> | string,
// @ts-ignore might be instantiated with a different subtype
fallback: Fallback = Path.OS_SEGMENT_SEPARATOR
): string | Fallback;
/**
* Creates a new Path by concatenating the input path(s) and then resolving all
* non-leading `.` and `..` segments. In other words:
*
* - Segments containing `.` are removed
* - Segments containing `..` are removed, along with the segment preceding
* them.
*
* Note that any `.` or `..` segments at the beginning of the path (ie.
* "leading segments") are not removed.
*/
static normalize(
...inputs: Array<string | Path | Array<string | Path>>
): Path;
/**
* Returns a boolean indicating whether the provided path is absolute; that
* is, whether it starts with either a slash (`/` or `\`) or a drive letter
* (ie `C:`).
*
* Note that Windows UNC Paths (eg. `\\MYSERVER\share$\`) are considered
* absolute.
*/
static isAbsolute(path: string | Path): boolean;
/**
* Creates a new Path containing the user-provided segments and separator. In
* most cases, you won't need to do this, and can use `new Path(...)` instead.
*
* If unspecified, the `separator` parameter defaults to
* `Path.OS_SEGMENT_SEPARATOR`.
*/
static fromRaw(segments: Array<string>, separator?: string): Path;
/**
* Creates a new Path object using the provided input(s), which will be
* concatenated together in order left-to-right.
*/
constructor(...inputs: Array<string | Path | Array<string | Path>>);
/**
* An array of the path segments that make up this path.
*
* For `/tmp/foo.txt`, it'd be `["", "tmp", "foo.txt"]`.
*
* For `C:\something\somewhere.txt`, it'd be `["C:", "something", "somewhere.txt"]`.
*/
segments: Array<string>;
/**
* The path separator that should be used to turn this path into a string.
*
* Will be either `"/"` or `"\"`.
*/
separator: string;
/**
* Creates a new Path by resolving all non-leading `.` and `..` segments in
* the target Path. In other words:
*
* - Segments containing `.` are removed
* - Segments containing `..` are removed, along with the segment preceding
* them.
*
* Note that any `.` or `..` segments at the beginning of the path (ie.
* "leading segments") are not removed.
*/
normalize(): Path;
/**
* Creates a new Path by appending additional path segments onto the end of
* the target Path's segments.
*
* The returned Path will use the same separator as the target Path.
*/
concat(...other: Array<string | Path | Array<string | Path>>): Path;
/**
* Returns a boolean indicating whether the target Path is absolute; that
* is, whether it starts with either a slash (`/` or `\`) or a drive letter
* (ie `C:`).
*
* Note that Windows UNC Paths (eg. `\\MYSERVER\share$\`) are considered
* absolute.
*/
isAbsolute(): boolean;
/**
* Creates a new Path object containing the same segments and separator as
* the target Path.
*
* Note that although it contains the same segments, the new Path does not use
* the same Array instance for segments as the target Path is was cloned from.
*/
clone(): this;
/**
* Creates a new Path which expresses the target Path relative to `dir`.
*
* @param dir - The directory to create a new path relative to.
* @param options - Options that affect the resulting path (see {@link PathRelativeToOptions}).
*/
relativeTo(dir: Path | string, options?: PathRelativeToOptions): Path;
/**
* Turns the target Path into a string by joining its segments using its
* separator as the delimiter.
*/
toString(): string;
/**
* Alias for `toString`. The presence of this method causes Path objects to be
* serialized as strings when they (or an object referencing them) get(s) passed
* into JSON.stringify.
*/
toJSON(): string;
/**
* Returns the final segment of the target Path. If the target Path has no
* segments, an empty string (`""`) is returned.
*/
basename(): string;
/**
* Returns the trailing file extension of this path.
*
* @param options - Works the same as the options parameter for the global {@link extname} (see {@link ExtnameOptions}).
*/
extname(options?: ExtnameOptions): string;
/**
* Creates a new Path containing all of the segments in the target Path except
* for the last one; ie. the path to the directory that contains the target Path.
*/
dirname(): Path;
/**
* Returns a boolean indicating whether the target Path starts with the
* provided value, by comparing one path segment at a time.
*
* The starting segments of the target Path must *exactly* match the segments in the
* provided value.
*
* This means that, given two Paths A and B:
*
* ```
* A: Path { /home/user/.config }
* B: Path { /home/user/.config2 }
* ```
*
* Path B does *not* start with Path A, because `".config" !== ".config2"`.
*/
startsWith(value: string | Path | Array<string | Path>): boolean;
/**
* Returns a boolean indicating whether the target Path ends with the provided
* value, by comparing one path segment at a time.
*
* The ending segments of the target Path must *exactly* match the segments in the
* provided value.
*
* This means that, given two Paths A and B:
*
* ```
* A: Path { /home/1user/.config }
* B: Path { user/.config }
* ```
*
* Path A does *not* end with Path B, because `"1user" !== "user"`.
*/
endsWith(value: string | Path | Array<string | Path>): boolean;
/**
* Returns the index at which `value` appears in the target Path's segments,
* or `-1` if `value` doesn't appear in the target Path.
*
* @param value - The value to search for. If the value contains more than one path segment, the returned index will refer to the location of the value's first path segment.
* @param fromIndex - The index into the target Path's segments to begin searching at. Defaults to `0`.
*/
indexOf(
value: string | Path | Array<string | Path>,
fromIndex?: number | undefined
): number;
/**
* Returns a boolean indicating whether `value` appears in the target Path.
*
* @param value - The value to search for.
* @param fromIndex - The index into the target Path's segments to begin searching at. Defaults to `0`.
*/
includes(
value: string | Path | Array<string | Path>,
fromIndex?: number | undefined
): boolean;
/**
* Creates a new Path based on the target Path wherein the segments in `value`
* have been replaced with the segments in `replacement`. If the segments in
* `value` are not present in the target Path, a clone of the target Path is
* returned.
*
* Note that only the first match is replaced. To replace more than one match,
* use {@link Path.prototype.replaceAll}.
*
* @param value - What should be replaced
* @param replacement - What it should be replaced with
*
* See also {@link Path.prototype.replaceLast}.
*/
replace(
value: string | Path | Array<string | Path>,
replacement: string | Path | Array<string | Path>
): Path;
/**
* Creates a new Path based on the target Path wherein all occurrences of the
* segments in `value` have been replaced with the segments in `replacement`.
* If the segments in `value` are not present in the target Path, a clone of
* the target Path is returned.
*
* Note that all matches are replaced. To replace only the first match,
* use {@link Path.prototype.replace}.
*
* @param value - What should be replaced
* @param replacement - What it should be replaced with
*
* See also {@link Path.prototype.replaceLast}.
*/
replaceAll(
value: string | Path | Array<string | Path>,
replacement: string | Path | Array<string | Path>
): Path;
/**
* Creates a new Path based on the target Path but with the final segment
* replaced with `replacement`.
*
* If the target Path has no segments, the newly created Path will be the same
* as `new Path(replacement)`; ie. non-empty.
*
* @param replacement - The new final segment(s) for the returned Path
*/
replaceLast(replacement: string | Path | Array<string | Path>): Path;
}
/**
* Options for {@link Path.prototype.relativeTo}.
*/
declare interface PathRelativeToOptions {
/**
* Defaults to false. When true, a leading `./` will be omitted from the
* path, if present. Note that a leading `../` will never be omitted.
*/
noLeadingDot?: boolean;
}
/**
* The absolute path to the currently-executing file (whether script or module).
*
* Behaves the same as in Node.js, except that it's also present within ES
* modules.
*
* Example: `/home/suchipi/some-folder/some-file.js`
*/
declare var __filename: string;
/**
* The absolute path to the directory containing the currently-executing file.
*
* Behaves the same as in Node.js, except that it's also present within ES
* modules.
*
* Example: `/home/suchipi/some-folder`
*/
declare var __dirname: string;
/**
* Return the last component of a path string.
*
* Provides the same functionality as the unix binary of the same name.
*
* > Example: `basename("/home/suchipi/something")` returns `"something"`, the last part.
*/
declare function basename(path: string | Path): string;
/**
* Reads the contents of one or more files from disk as either one UTF-8 string
* or one ArrayBuffer.
*
* Provides the same functionality as the unix binary of the same name.
*
* > Example: If you have a file called `hi.txt` in the current working
* > directory, and it contains the text "hello", running `cat("hi.txt")`
* > returns `"hello"`.
*/
declare const cat: {
/**
* Read the contents of one or more files from disk, as one UTF-8 string.
*/
(paths: string | Path | Array<string | Path>): string;
/**
* Read the contents of one or more files from disk, as one UTF-8 string.
*/
(paths: string | Path | Array<string | Path>, options: {}): string;
/**
* Read the contents of one or more files from disk, as one UTF-8 string.
*/
(
paths: string | Path | Array<string | Path>,
options: { binary: false }
): string;
/**
* Read the contents of one or more files from disk, as one ArrayBuffer.
*/
(
paths: string | Path | Array<string | Path>,
options: { binary: true }
): ArrayBuffer;
};
/**
* Changes the process's current working directory to the specified path. If no
* path is specified, moves to the user's home directory.
*
* Provides the same functionality as the shell builtin of the same name.
*/
declare function cd(path?: string | Path): void;
/**
* Set the permission bits for the specified file.
*
* @param permissions The permission bits to set. This can be a number, a string
* containing an octal number, or an object.
* @param path The path to the file.
*
* Provides the same functionality as the unix binary of the same name.
*
* The `permissions` argument can be either:
*
* - a number (best expressed using octal, eg `0o655`)
* - a string (which will be interpreted as an octal number, eg `'777'`)
* - or an object (see below).
*
* > NOTE: At this time there are no "add"/"remove" semantics; the existing
* > permissions will be completely overwritten with your specified permissions.
* > This will be changed later as this is not intuitive.
*
* When permissions is an object, each of the object's own properties' keys must
* be one of these strings:
*
* - `"user"`
* - `"group"`
* - `"others"`
* - `"all"` (meaning "user", "group", and "others")
* - `"u"` (alias for "user")
* - `"g"` (alias for "group")
* - `"o"` (alias for "others")
* - `"a"` (alias for "all")
* - `"ug"` ("user" plus "group")
* - `"go"` ("group" plus "others")
* - `"uo"` ("user" plus "others")
*
* and their values must be one of these strings:
*
* - `"read"` (permission to read the contents of the file)
* - `"write"` (permission to write to the file's contents)
* - `"execute"` (permission to run the file as an executable)
* - `"readwrite"` (both "read" and "write")
* - `"none"` (no permissions)
* - `"full"` ("read", "write", and "execute")
* - `"r"` (alias for "read")
* - `"w"` (alias for "write")
* - `"x"` (alias for "execute")
* - `"rw"` (alias for "readwrite")
* - `"rx"` ("read" and "execute")
* - `"wx"` ("write" and "execute")
* - `"rwx"` (alias for "full")
*
* Some example objects:
*
* ```ts
* chmod({ user: "readwrite", group: "read", others: "none" });
* chmod({ ug: "rw", o: "w" });
* chmod({ all: "full" });
* ```
*/
declare function chmod(
permissions:
| number
| string
| Record<ChmodPermissionsWho, ChmodPermissionsWhat>,
path: string | Path
): void;
/** A string representing who a permission applies to. */
declare type ChmodPermissionsWho =
| "user"
| "group"
| "others"
| "all"
| "u"
| "g"
| "o"
| "a"
| "ug"
| "go"
| "uo";
/** A string representing the access level for the given permission. */
declare type ChmodPermissionsWhat =
| "read"
| "write"
| "execute"
| "readwrite"
| "none"
| "full"
| "r"
| "w"
| "x"
| "rw"
| "rx"
| "wx"
| "rwx";
/**
* Removes the final component from a path string.
*
* Provides the same functionality as the unix binary of the same name.
*
* > Example: `dirname("/home/suchipi/something")` returns
* > `"/home/suchipi"`, everything except the last part.
*/
declare function dirname(path: string | Path): Path;
/**
* Print one or more values to stdout.
*
* Provides the same functionality as the shell builtin of the same name.
*
* > NOTE: This can print any value, not just strings.
*