@decaf-ts/utils 0.2.12 → 0.3.0
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/LICENSE.md +21 -157
- package/README.md +403 -12
- package/bin/build-scripts.cjs +236 -52
- package/bin/tag-release.cjs +168 -47
- package/bin/update-scripts.cjs +207 -47
- package/dist/utils.cjs +364 -70
- package/dist/utils.esm.cjs +364 -70
- package/lib/cli/command.cjs +2 -2
- package/lib/cli/command.d.ts +1 -1
- package/lib/cli/commands/build-scripts.cjs +8 -6
- package/lib/cli/constants.cjs +3 -1
- package/lib/cli/constants.d.ts +2 -0
- package/lib/cli/types.cjs +1 -1
- package/lib/cli/types.d.ts +2 -0
- package/lib/esm/bin/build-scripts.js +2 -2
- package/lib/esm/bin/tag-release.js +2 -2
- package/lib/esm/bin/update-scripts.js +2 -2
- package/lib/esm/cli/command.d.ts +1 -1
- package/lib/esm/cli/command.js +7 -7
- package/lib/esm/cli/commands/build-scripts.js +11 -9
- package/lib/esm/cli/commands/index.js +4 -4
- package/lib/esm/cli/commands/tag-release.js +5 -5
- package/lib/esm/cli/commands/update-scripts.js +4 -4
- package/lib/esm/cli/constants.d.ts +2 -0
- package/lib/esm/cli/constants.js +3 -1
- package/lib/esm/cli/index.js +4 -4
- package/lib/esm/cli/types.d.ts +2 -0
- package/lib/esm/cli/types.js +1 -1
- package/lib/esm/index.d.ts +4 -4
- package/lib/esm/index.js +10 -10
- package/lib/esm/input/index.js +3 -3
- package/lib/esm/input/input.d.ts +65 -19
- package/lib/esm/input/input.js +61 -22
- package/lib/esm/input/types.d.ts +7 -7
- package/lib/esm/input/types.js +1 -1
- package/lib/esm/output/common.d.ts +2 -0
- package/lib/esm/output/common.js +4 -1
- package/lib/esm/output/index.js +2 -2
- package/lib/esm/utils/constants.d.ts +12 -6
- package/lib/esm/utils/constants.js +13 -7
- package/lib/esm/utils/environment.d.ts +13 -0
- package/lib/esm/utils/environment.js +16 -3
- package/lib/esm/utils/fs.d.ts +111 -5
- package/lib/esm/utils/fs.js +114 -8
- package/lib/esm/utils/http.d.ts +1 -1
- package/lib/esm/utils/http.js +2 -2
- package/lib/esm/utils/index.js +8 -8
- package/lib/esm/utils/md.d.ts +15 -15
- package/lib/esm/utils/md.js +1 -1
- package/lib/esm/utils/tests.d.ts +26 -3
- package/lib/esm/utils/tests.js +40 -2
- package/lib/esm/utils/text.d.ts +33 -7
- package/lib/esm/utils/text.js +34 -8
- package/lib/esm/utils/timeout.d.ts +11 -0
- package/lib/esm/utils/timeout.js +12 -1
- package/lib/esm/utils/types.d.ts +33 -2
- package/lib/esm/utils/types.js +1 -1
- package/lib/esm/utils/utils.d.ts +44 -2
- package/lib/esm/utils/utils.js +23 -5
- package/lib/esm/utils/web.d.ts +3 -2
- package/lib/esm/utils/web.js +4 -3
- package/lib/esm/writers/OutputWriter.d.ts +2 -2
- package/lib/esm/writers/OutputWriter.js +1 -1
- package/lib/esm/writers/RegexpOutputWriter.d.ts +51 -10
- package/lib/esm/writers/RegexpOutputWriter.js +53 -12
- package/lib/esm/writers/StandardOutputWriter.d.ts +49 -10
- package/lib/esm/writers/StandardOutputWriter.js +51 -12
- package/lib/esm/writers/index.js +5 -5
- package/lib/esm/writers/types.d.ts +2 -2
- package/lib/esm/writers/types.js +1 -1
- package/lib/index.cjs +5 -5
- package/lib/index.d.ts +4 -4
- package/lib/input/input.cjs +61 -22
- package/lib/input/input.d.ts +65 -19
- package/lib/input/types.cjs +1 -1
- package/lib/input/types.d.ts +7 -7
- package/lib/output/common.cjs +4 -1
- package/lib/output/common.d.ts +2 -0
- package/lib/utils/constants.cjs +13 -7
- package/lib/utils/constants.d.ts +12 -6
- package/lib/utils/environment.cjs +14 -1
- package/lib/utils/environment.d.ts +13 -0
- package/lib/utils/fs.cjs +112 -6
- package/lib/utils/fs.d.ts +111 -5
- package/lib/utils/http.cjs +2 -2
- package/lib/utils/http.d.ts +1 -1
- package/lib/utils/md.cjs +1 -1
- package/lib/utils/md.d.ts +15 -15
- package/lib/utils/tests.cjs +39 -1
- package/lib/utils/tests.d.ts +26 -3
- package/lib/utils/text.cjs +34 -8
- package/lib/utils/text.d.ts +33 -7
- package/lib/utils/timeout.cjs +12 -1
- package/lib/utils/timeout.d.ts +11 -0
- package/lib/utils/types.cjs +1 -1
- package/lib/utils/types.d.ts +33 -2
- package/lib/utils/utils.cjs +21 -3
- package/lib/utils/utils.d.ts +44 -2
- package/lib/utils/web.cjs +4 -3
- package/lib/utils/web.d.ts +3 -2
- package/lib/writers/OutputWriter.cjs +1 -1
- package/lib/writers/OutputWriter.d.ts +2 -2
- package/lib/writers/RegexpOutputWriter.cjs +52 -11
- package/lib/writers/RegexpOutputWriter.d.ts +51 -10
- package/lib/writers/StandardOutputWriter.cjs +50 -11
- package/lib/writers/StandardOutputWriter.d.ts +49 -10
- package/lib/writers/types.cjs +1 -1
- package/lib/writers/types.d.ts +2 -2
- package/package.json +2 -2
package/LICENSE.md
CHANGED
|
@@ -1,157 +1,21 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
than an Application or a Combined Work as defined below.
|
|
23
|
-
|
|
24
|
-
An "Application" is any work that makes use of an interface provided
|
|
25
|
-
by the Library, but which is not otherwise based on the Library.
|
|
26
|
-
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
-
of using an interface provided by the Library.
|
|
28
|
-
|
|
29
|
-
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
-
Application with the Library. The particular version of the Library
|
|
31
|
-
with which the Combined Work was made is also called the "Linked
|
|
32
|
-
Version".
|
|
33
|
-
|
|
34
|
-
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
-
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
-
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
-
based on the Application, and not on the Linked Version.
|
|
38
|
-
|
|
39
|
-
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
-
object code and/or source code for the Application, including any data
|
|
41
|
-
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
-
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
-
|
|
44
|
-
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
-
|
|
46
|
-
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
-
without being bound by section 3 of the GNU GPL.
|
|
48
|
-
|
|
49
|
-
## 2. Conveying Modified Versions.
|
|
50
|
-
|
|
51
|
-
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
-
facility refers to a function or data to be supplied by an Application
|
|
53
|
-
that uses the facility (other than as an argument passed when the
|
|
54
|
-
facility is invoked), then you may convey a copy of the modified
|
|
55
|
-
version:
|
|
56
|
-
|
|
57
|
-
- a) under this License, provided that you make a good faith effort
|
|
58
|
-
to ensure that, in the event an Application does not supply the
|
|
59
|
-
function or data, the facility still operates, and performs
|
|
60
|
-
whatever part of its purpose remains meaningful, or
|
|
61
|
-
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
-
this License applicable to that copy.
|
|
63
|
-
|
|
64
|
-
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
-
|
|
66
|
-
The object code form of an Application may incorporate material from a
|
|
67
|
-
header file that is part of the Library. You may convey such object
|
|
68
|
-
code under terms of your choice, provided that, if the incorporated
|
|
69
|
-
material is not limited to numerical parameters, data structure
|
|
70
|
-
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
-
(ten or fewer lines in length), you do both of the following:
|
|
72
|
-
|
|
73
|
-
- a) Give prominent notice with each copy of the object code that
|
|
74
|
-
the Library is used in it and that the Library and its use are
|
|
75
|
-
covered by this License.
|
|
76
|
-
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
-
license document.
|
|
78
|
-
|
|
79
|
-
## 4. Combined Works.
|
|
80
|
-
|
|
81
|
-
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
-
together, effectively do not restrict modification of the portions of
|
|
83
|
-
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
-
debugging such modifications, if you also do each of the following:
|
|
85
|
-
|
|
86
|
-
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
-
the Library is used in it and that the Library and its use are
|
|
88
|
-
covered by this License.
|
|
89
|
-
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
-
license document.
|
|
91
|
-
- c) For a Combined Work that displays copyright notices during
|
|
92
|
-
execution, include the copyright notice for the Library among
|
|
93
|
-
these notices, as well as a reference directing the user to the
|
|
94
|
-
copies of the GNU GPL and this license document.
|
|
95
|
-
- d) Do one of the following:
|
|
96
|
-
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
-
this License, and the Corresponding Application Code in a form
|
|
98
|
-
suitable for, and under terms that permit, the user to
|
|
99
|
-
recombine or relink the Application with a modified version of
|
|
100
|
-
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
-
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
-
Corresponding Source.
|
|
103
|
-
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
-
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
-
time a copy of the Library already present on the user's
|
|
106
|
-
computer system, and (b) will operate properly with a modified
|
|
107
|
-
version of the Library that is interface-compatible with the
|
|
108
|
-
Linked Version.
|
|
109
|
-
- e) Provide Installation Information, but only if you would
|
|
110
|
-
otherwise be required to provide such information under section 6
|
|
111
|
-
of the GNU GPL, and only to the extent that such information is
|
|
112
|
-
necessary to install and execute a modified version of the
|
|
113
|
-
Combined Work produced by recombining or relinking the Application
|
|
114
|
-
with a modified version of the Linked Version. (If you use option
|
|
115
|
-
4d0, the Installation Information must accompany the Minimal
|
|
116
|
-
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
-
use option 4d1, you must provide the Installation Information in
|
|
118
|
-
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
-
Corresponding Source.)
|
|
120
|
-
|
|
121
|
-
## 5. Combined Libraries.
|
|
122
|
-
|
|
123
|
-
You may place library facilities that are a work based on the Library
|
|
124
|
-
side by side in a single library together with other library
|
|
125
|
-
facilities that are not Applications and are not covered by this
|
|
126
|
-
License, and convey such a combined library under terms of your
|
|
127
|
-
choice, if you do both of the following:
|
|
128
|
-
|
|
129
|
-
- a) Accompany the combined library with a copy of the same work
|
|
130
|
-
based on the Library, uncombined with any other library
|
|
131
|
-
facilities, conveyed under the terms of this License.
|
|
132
|
-
- b) Give prominent notice with the combined library that part of it
|
|
133
|
-
is a work based on the Library, and explaining where to find the
|
|
134
|
-
accompanying uncombined form of the same work.
|
|
135
|
-
|
|
136
|
-
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
-
|
|
138
|
-
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
-
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
-
versions will be similar in spirit to the present version, but may
|
|
141
|
-
differ in detail to address new problems or concerns.
|
|
142
|
-
|
|
143
|
-
Each version is given a distinguishing version number. If the Library
|
|
144
|
-
as you received it specifies that a certain numbered version of the
|
|
145
|
-
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
-
it, you have the option of following the terms and conditions either
|
|
147
|
-
of that published version or of any later version published by the
|
|
148
|
-
Free Software Foundation. If the Library as you received it does not
|
|
149
|
-
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
-
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
-
published by the Free Software Foundation.
|
|
152
|
-
|
|
153
|
-
If the Library as you received it specifies that a proxy can decide
|
|
154
|
-
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
-
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
-
permanent authorization for you to choose that version for the
|
|
157
|
-
Library.
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tiago Venceslau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Decaf's Utils Module
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A comprehensive TypeScript utility library providing robust tools for command-line interfaces, input handling, output management, file system operations, HTTP requests, text processing, and more. This module serves as a foundation for building powerful CLI applications with standardized input parsing, flexible output formatting, and a rich set of utility functions.
|
|
6
|
+
|
|
6
7
|
|
|
7
8
|

|
|
8
9
|

|
|
@@ -18,12 +19,6 @@ Provides basic text, fs and cli functionality
|
|
|
18
19
|

|
|
19
20
|

|
|
20
21
|
|
|
21
|
-

|
|
22
|
-

|
|
23
|
-

|
|
24
|
-

|
|
25
|
-
|
|
26
|
-
|
|
27
22
|

|
|
28
23
|

|
|
29
24
|

|
|
@@ -35,15 +30,41 @@ Documentation available [here](https://decaf-ts.github.io/utils/)
|
|
|
35
30
|
|
|
36
31
|
### Description
|
|
37
32
|
|
|
38
|
-
|
|
33
|
+
The Decaf Utils module is a comprehensive TypeScript utility library designed to standardize APIs across repositories and provide a robust foundation for building command-line interface (CLI) applications. The library is organized into several key components:
|
|
34
|
+
|
|
35
|
+
#### CLI Module
|
|
36
|
+
The CLI module provides a structured framework for creating command-line applications:
|
|
37
|
+
- Abstract `Command` class for implementing custom CLI commands with standardized input handling, logging, and execution flow
|
|
38
|
+
- Command option handling with support for common flags like verbose, version, help, etc.
|
|
39
|
+
- Standardized command execution flow with proper error handling
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
#### Input Module
|
|
42
|
+
The Input module offers tools for handling user input and command-line arguments:
|
|
43
|
+
- `UserInput` class for creating interactive prompts with various input types (text, number, confirmation, etc.)
|
|
44
|
+
- Support for input validation, formatting, and default values
|
|
45
|
+
- Command-line argument parsing with type checking and validation
|
|
46
|
+
- Methods for repeatedly asking for input until valid responses are received
|
|
44
47
|
|
|
48
|
+
#### Utils Module
|
|
49
|
+
The Utils module contains a wide range of utility functions:
|
|
50
|
+
- **File System Operations**: Reading, writing, copying, and deleting files and directories
|
|
51
|
+
- **Package Management**: Retrieving package information, managing dependencies, and version handling
|
|
52
|
+
- **HTTP Utilities**: Simple client for downloading files from URLs
|
|
53
|
+
- **Text Processing**: String interpolation, case conversion (camelCase, snake_case, etc.), and regular expression utilities
|
|
54
|
+
- **Environment Handling**: Working with environment variables and configuration
|
|
45
55
|
|
|
56
|
+
#### Writers Module
|
|
57
|
+
The Writers module provides different strategies for handling command output:
|
|
58
|
+
- `OutputWriter` interface defining a standard contract for output handling
|
|
59
|
+
- `StandardOutputWriter` for handling standard command-line output with proper logging
|
|
60
|
+
- `RegexpOutputWriter` for processing output with regular expressions and pattern matching
|
|
46
61
|
|
|
62
|
+
#### Output Module
|
|
63
|
+
The Output module offers utilities for formatting and displaying output:
|
|
64
|
+
- Common output formatting functions
|
|
65
|
+
- Banner display capabilities
|
|
66
|
+
|
|
67
|
+
This library serves as a light version of the Decaf CLI tool, providing all the essential utilities needed for building robust command-line applications with consistent input handling, output formatting, and error management.
|
|
47
68
|
|
|
48
69
|
|
|
49
70
|
### How to Use
|
|
@@ -51,7 +72,377 @@ Provides Basic functionality to standardize APIs across repositories:
|
|
|
51
72
|
- [Initial Setup](./tutorials/For%20Developers.md#_initial-setup_)
|
|
52
73
|
- [Installation](./tutorials/For%20Developers.md#installation)
|
|
53
74
|
|
|
75
|
+
## Examples
|
|
76
|
+
|
|
77
|
+
### CLI Module
|
|
78
|
+
|
|
79
|
+
#### Creating a Custom Command
|
|
80
|
+
|
|
81
|
+
The Command class provides a foundation for creating custom CLI commands with standardized input handling and execution flow.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Command, CommandOptions } from '@decaf-ts/utils';
|
|
85
|
+
|
|
86
|
+
// Define input options interface
|
|
87
|
+
interface MyCommandOptions {
|
|
88
|
+
filename: string;
|
|
89
|
+
dryRun: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Create a custom command class
|
|
93
|
+
class MyCommand extends Command<MyCommandOptions, string> {
|
|
94
|
+
constructor() {
|
|
95
|
+
// Define command options with their configurations
|
|
96
|
+
const options: CommandOptions<MyCommandOptions> = {
|
|
97
|
+
filename: {
|
|
98
|
+
type: 'string',
|
|
99
|
+
short: 'f',
|
|
100
|
+
default: 'output.txt'
|
|
101
|
+
},
|
|
102
|
+
dryRun: {
|
|
103
|
+
type: 'boolean',
|
|
104
|
+
short: 'd',
|
|
105
|
+
default: false
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
super('my-command', options);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Override the help method to provide custom help information
|
|
113
|
+
protected help(): void {
|
|
114
|
+
this.log.info(`
|
|
115
|
+
Usage: my-command [options]
|
|
116
|
+
|
|
117
|
+
Options:
|
|
118
|
+
-f, --filename Specify output filename (default: output.txt)
|
|
119
|
+
-d, --dryRun Run without making changes (default: false)
|
|
120
|
+
-h, --help Show help information
|
|
121
|
+
-v, --version Show version information
|
|
122
|
+
`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Implement the run method to define command behavior
|
|
126
|
+
protected async run(answers: { filename: string; dryRun: boolean }): Promise<string> {
|
|
127
|
+
this.log.info(`Running command with filename: ${answers.filename}, dryRun: ${answers.dryRun}`);
|
|
54
128
|
|
|
129
|
+
// Command implementation here
|
|
130
|
+
|
|
131
|
+
return 'Command executed successfully';
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Execute the command
|
|
136
|
+
async function main() {
|
|
137
|
+
const command = new MyCommand();
|
|
138
|
+
const result = await command.execute();
|
|
139
|
+
console.log(result);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
main().catch(console.error);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Input Module
|
|
146
|
+
|
|
147
|
+
#### Creating Interactive Prompts
|
|
148
|
+
|
|
149
|
+
The UserInput class allows you to create interactive prompts for collecting user input.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { UserInput } from '@decaf-ts/utils';
|
|
153
|
+
|
|
154
|
+
async function collectUserInfo() {
|
|
155
|
+
// Create a text input for name
|
|
156
|
+
const nameInput = new UserInput('name')
|
|
157
|
+
.setMessage('What is your name?')
|
|
158
|
+
.setInitial('User');
|
|
159
|
+
|
|
160
|
+
// Create a number input for age with validation
|
|
161
|
+
const ageInput = new UserInput('age')
|
|
162
|
+
.setType('number')
|
|
163
|
+
.setMessage('How old are you?')
|
|
164
|
+
.setMin(0)
|
|
165
|
+
.setMax(120)
|
|
166
|
+
.setValidate(value => {
|
|
167
|
+
if (value < 18) return 'You must be at least 18 years old';
|
|
168
|
+
return true;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Create a confirmation input
|
|
172
|
+
const confirmInput = new UserInput('confirm')
|
|
173
|
+
.setType('confirm')
|
|
174
|
+
.setMessage('Is this information correct?')
|
|
175
|
+
.setInitial(true);
|
|
176
|
+
|
|
177
|
+
// Ask for all inputs and get the answers
|
|
178
|
+
const answers = await UserInput.ask([nameInput, ageInput, confirmInput]);
|
|
179
|
+
|
|
180
|
+
console.log(`Hello ${answers.name}, you are ${answers.age} years old.`);
|
|
181
|
+
console.log(`Information confirmed: ${answers.confirm ? 'Yes' : 'No'}`);
|
|
182
|
+
|
|
183
|
+
return answers;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
collectUserInfo().catch(console.error);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### Using Convenience Methods for Input
|
|
190
|
+
|
|
191
|
+
UserInput provides static convenience methods for common input scenarios.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { UserInput } from '@decaf-ts/utils';
|
|
195
|
+
|
|
196
|
+
async function quickInputExample() {
|
|
197
|
+
// Ask for text input
|
|
198
|
+
const name = await UserInput.askText('name', 'What is your name?', undefined, 'User');
|
|
199
|
+
|
|
200
|
+
// Ask for number input
|
|
201
|
+
const age = await UserInput.askNumber('age', 'How old are you?', 0, 120);
|
|
202
|
+
|
|
203
|
+
// Ask for confirmation
|
|
204
|
+
const confirm = await UserInput.askConfirmation('confirm', 'Is this information correct?', true);
|
|
205
|
+
|
|
206
|
+
// Ask for text with validation and retry until valid
|
|
207
|
+
const email = await UserInput.insistForText(
|
|
208
|
+
'email',
|
|
209
|
+
'What is your email address?',
|
|
210
|
+
(value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
|
|
211
|
+
undefined,
|
|
212
|
+
undefined,
|
|
213
|
+
false,
|
|
214
|
+
3 // Maximum 3 attempts
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
console.log(`Name: ${name}, Age: ${age}, Email: ${email}, Confirmed: ${confirm}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
quickInputExample().catch(console.error);
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### Parsing Command-Line Arguments
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { UserInput, ParseArgsOptionsConfig } from '@decaf-ts/utils';
|
|
227
|
+
|
|
228
|
+
// Define command-line options
|
|
229
|
+
const options: ParseArgsOptionsConfig = {
|
|
230
|
+
verbose: {
|
|
231
|
+
type: 'boolean',
|
|
232
|
+
short: 'V',
|
|
233
|
+
default: false
|
|
234
|
+
},
|
|
235
|
+
output: {
|
|
236
|
+
type: 'string',
|
|
237
|
+
short: 'o',
|
|
238
|
+
default: 'output.txt'
|
|
239
|
+
},
|
|
240
|
+
count: {
|
|
241
|
+
type: 'string',
|
|
242
|
+
short: 'c',
|
|
243
|
+
default: '10'
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// Parse command-line arguments
|
|
248
|
+
const args = UserInput.parseArgs(options);
|
|
249
|
+
|
|
250
|
+
console.log('Parsed arguments:', args.values);
|
|
251
|
+
console.log('Positional arguments:', args.positionals);
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Utils Module
|
|
255
|
+
|
|
256
|
+
#### File System Operations
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import {
|
|
260
|
+
readFile,
|
|
261
|
+
writeFile,
|
|
262
|
+
patchFile,
|
|
263
|
+
getAllFiles,
|
|
264
|
+
copyFile,
|
|
265
|
+
renameFile,
|
|
266
|
+
deletePath
|
|
267
|
+
} from '@decaf-ts/utils';
|
|
268
|
+
|
|
269
|
+
// Read a file
|
|
270
|
+
const content = readFile('./config.json');
|
|
271
|
+
console.log('File content:', content);
|
|
272
|
+
|
|
273
|
+
// Write to a file
|
|
274
|
+
writeFile('./output.txt', 'Hello, world!');
|
|
275
|
+
|
|
276
|
+
// Patch a file with replacements
|
|
277
|
+
patchFile('./template.txt', {
|
|
278
|
+
'{{name}}': 'John Doe',
|
|
279
|
+
'{{date}}': new Date().toISOString()
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Get all files in a directory
|
|
283
|
+
const files = getAllFiles('./src', (file) => file.endsWith('.ts'));
|
|
284
|
+
console.log('TypeScript files:', files);
|
|
285
|
+
|
|
286
|
+
// Copy a file
|
|
287
|
+
copyFile('./source.txt', './destination.txt');
|
|
288
|
+
|
|
289
|
+
// Rename a file
|
|
290
|
+
renameFile('./old-name.txt', './new-name.txt');
|
|
291
|
+
|
|
292
|
+
// Delete a file or directory
|
|
293
|
+
deletePath('./temp');
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
#### Package Management
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
import {
|
|
300
|
+
getPackage,
|
|
301
|
+
getPackageVersion,
|
|
302
|
+
setPackageAttribute,
|
|
303
|
+
getDependencies,
|
|
304
|
+
installDependencies
|
|
305
|
+
} from '@decaf-ts/utils';
|
|
306
|
+
|
|
307
|
+
// Get package information
|
|
308
|
+
const pkg = getPackage();
|
|
309
|
+
console.log('Package:', pkg);
|
|
310
|
+
|
|
311
|
+
// Get package version
|
|
312
|
+
const version = getPackageVersion();
|
|
313
|
+
console.log('Version:', version);
|
|
314
|
+
|
|
315
|
+
// Set a package attribute
|
|
316
|
+
setPackageAttribute('version', '1.0.1');
|
|
317
|
+
|
|
318
|
+
// Get dependencies
|
|
319
|
+
async function manageDependencies() {
|
|
320
|
+
const deps = await getDependencies();
|
|
321
|
+
console.log('Production dependencies:', deps.prod);
|
|
322
|
+
console.log('Development dependencies:', deps.dev);
|
|
323
|
+
console.log('Peer dependencies:', deps.peer);
|
|
324
|
+
|
|
325
|
+
// Install dependencies
|
|
326
|
+
await installDependencies({
|
|
327
|
+
prod: ['lodash', 'axios'],
|
|
328
|
+
dev: ['typescript', 'jest']
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
manageDependencies().catch(console.error);
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
#### HTTP Utilities
|
|
336
|
+
|
|
337
|
+
```typescript
|
|
338
|
+
import { HttpClient } from '@decaf-ts/utils';
|
|
339
|
+
|
|
340
|
+
async function downloadExample() {
|
|
341
|
+
try {
|
|
342
|
+
// Download a file from a URL
|
|
343
|
+
const content = await HttpClient.downloadFile('https://example.com/api/data.json');
|
|
344
|
+
console.log('Downloaded content:', content);
|
|
345
|
+
|
|
346
|
+
// Parse JSON content
|
|
347
|
+
const data = JSON.parse(content);
|
|
348
|
+
console.log('Parsed data:', data);
|
|
349
|
+
|
|
350
|
+
return data;
|
|
351
|
+
} catch (error) {
|
|
352
|
+
console.error('Download failed:', error);
|
|
353
|
+
throw error;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
downloadExample().catch(console.error);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Text Processing
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
import {
|
|
364
|
+
padEnd,
|
|
365
|
+
patchPlaceholders,
|
|
366
|
+
patchString,
|
|
367
|
+
toCamelCase,
|
|
368
|
+
toSnakeCase,
|
|
369
|
+
toKebabCase,
|
|
370
|
+
toPascalCase,
|
|
371
|
+
toENVFormat
|
|
372
|
+
} from '@decaf-ts/utils';
|
|
373
|
+
|
|
374
|
+
// Pad a string
|
|
375
|
+
const padded = padEnd('Hello', 10, '-');
|
|
376
|
+
console.log(padded); // 'Hello-----'
|
|
377
|
+
|
|
378
|
+
// Replace placeholders
|
|
379
|
+
const template = 'Hello, ${name}! Today is ${day}.';
|
|
380
|
+
const filled = patchPlaceholders(template, {
|
|
381
|
+
name: 'Alice',
|
|
382
|
+
day: 'Monday'
|
|
383
|
+
});
|
|
384
|
+
console.log(filled); // 'Hello, Alice! Today is Monday.'
|
|
385
|
+
|
|
386
|
+
// Replace strings
|
|
387
|
+
const patched = patchString('Hello, world!', {
|
|
388
|
+
'world': 'universe'
|
|
389
|
+
});
|
|
390
|
+
console.log(patched); // 'Hello, universe!'
|
|
391
|
+
|
|
392
|
+
// Case conversion
|
|
393
|
+
const text = 'hello world';
|
|
394
|
+
console.log(toCamelCase(text)); // 'helloWorld'
|
|
395
|
+
console.log(toSnakeCase(text)); // 'hello_world'
|
|
396
|
+
console.log(toKebabCase(text)); // 'hello-world'
|
|
397
|
+
console.log(toPascalCase(text)); // 'HelloWorld'
|
|
398
|
+
console.log(toENVFormat(text)); // 'HELLO_WORLD'
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
### Writers Module
|
|
402
|
+
|
|
403
|
+
#### Using StandardOutputWriter
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
import { StandardOutputWriter } from '@decaf-ts/utils';
|
|
407
|
+
|
|
408
|
+
// Create a promise executor
|
|
409
|
+
const executor = {
|
|
410
|
+
resolve: (value: string) => console.log(`Command succeeded with: ${value}`),
|
|
411
|
+
reject: (error: Error) => console.error(`Command failed with: ${error.message}`)
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
// Create a standard output writer
|
|
415
|
+
const writer = new StandardOutputWriter('ls -la', executor);
|
|
416
|
+
|
|
417
|
+
// Handle command output
|
|
418
|
+
writer.data('File list output...');
|
|
419
|
+
writer.data('file1.txt');
|
|
420
|
+
writer.data('file2.txt');
|
|
421
|
+
|
|
422
|
+
// Handle command completion
|
|
423
|
+
writer.exit(0, ['Command executed successfully']);
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
#### Using RegexpOutputWriter
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
import { RegexpOutputWriter } from '@decaf-ts/utils';
|
|
430
|
+
|
|
431
|
+
// Create a promise executor
|
|
432
|
+
const executor = {
|
|
433
|
+
resolve: (value: string) => console.log(`Found version: ${value}`),
|
|
434
|
+
reject: (error: Error) => console.error(`Error: ${error.message}`)
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// Create a regexp output writer that matches version numbers
|
|
438
|
+
const writer = new RegexpOutputWriter('node --version', executor, /v(\d+\.\d+\.\d+)/);
|
|
439
|
+
|
|
440
|
+
// Process output that contains a version number
|
|
441
|
+
writer.data('v14.17.0'); // This will automatically resolve with "v14.17.0"
|
|
442
|
+
|
|
443
|
+
// Process error output
|
|
444
|
+
writer.error('Command not found: node'); // This will be logged as an error
|
|
445
|
+
```
|
|
55
446
|
|
|
56
447
|
|
|
57
448
|
### Related
|