@puya/ts 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +96 -3
- package/Timestamper.js +4 -4
- package/index.js +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,8 +1,101 @@
|
|
1
1
|
# Timestamper
|
2
|
-
This library provides timestamp and supports custom templates for its output file.
|
3
2
|
|
4
|
-
|
3
|
+
Timestamper is a Node.js library and CLI tool that provides timestamp functionality and supports custom templates for its output file. It allows you to easily add a timestamp to a JSON file or any other file format based on a template.
|
5
4
|
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Add Timestamp**: Automatically add a timestamp to a specified output file.
|
8
|
+
- **Custom Templates**: Use custom templates for the output file.
|
9
|
+
- **Locale Support**: Supports different locales for timestamp formatting (e.g., `en`, `fa`).
|
10
|
+
- **CLI and Programmatic Use**: Can be used as a command-line tool or imported as a module in your Node.js projects.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
You can install Timestamper globally for CLI use or locally in your project.
|
15
|
+
|
16
|
+
### Global Installation
|
17
|
+
|
18
|
+
Install globally to use the `ts` command:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
npm install -g @puya/ts
|
6
22
|
```
|
7
|
-
|
23
|
+
|
24
|
+
### Local Installation
|
25
|
+
|
26
|
+
Install locally in your project:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
npm install @puya/ts
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### CLI
|
35
|
+
|
36
|
+
Once installed globally, you can use the `ts` command in your terminal.
|
37
|
+
|
38
|
+
```bash
|
39
|
+
ts [options]
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Options
|
43
|
+
|
44
|
+
- `-l [locale]`: The locale to use for formatting the date/time. Default is `en`.
|
45
|
+
- `-o [outputfile]`: The name of the output file where the result will be saved. Default is `info.json`.
|
46
|
+
- `-t [template]`: The path to a template file.
|
47
|
+
- `-f [format]`: The format string to use for formatting the date/time. Default is `YYYYMMDDHHmm`.
|
48
|
+
- `-i [inline-template]`: Inline template string.
|
49
|
+
|
50
|
+
#### Examples
|
51
|
+
|
52
|
+
1. Add a timestamp using the default settings:
|
53
|
+
|
54
|
+
```bash
|
55
|
+
ts
|
56
|
+
```
|
57
|
+
|
58
|
+
2. Add a timestamp to a custom output file with a specific format:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
ts -l en -o result.json -f YYYYMMDDHHmmss
|
62
|
+
```
|
63
|
+
|
64
|
+
3. Use a custom template file:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
ts -l fa -o result.json -t template.txt
|
68
|
+
```
|
69
|
+
|
70
|
+
4. Use an inline template:
|
71
|
+
|
72
|
+
```bash
|
73
|
+
ts -l fa -o result.json -i "{ test: '{ts}' }"
|
74
|
+
```
|
75
|
+
|
76
|
+
5. Get help:
|
77
|
+
|
78
|
+
```bash
|
79
|
+
ts --help
|
80
|
+
```
|
81
|
+
|
82
|
+
### Programmatic Use
|
83
|
+
|
84
|
+
You can also use Timestamper as a module in your Node.js projects.
|
85
|
+
|
86
|
+
```javascript
|
87
|
+
const { Timestamper } = require('@puya/ts');
|
88
|
+
|
89
|
+
const result = Timestamper({
|
90
|
+
locale: 'en',
|
91
|
+
outputFileName: 'result.json',
|
92
|
+
template: '{ "hash": "{ts}" }',
|
93
|
+
format: 'YYYYMMDDHHmmss',
|
94
|
+
});
|
95
|
+
|
96
|
+
if (result.success) {
|
97
|
+
console.log('Timestamp generated successfully:', result.output);
|
98
|
+
} else {
|
99
|
+
console.error('Failed to generate timestamp:', result.err);
|
100
|
+
}
|
8
101
|
```
|
package/Timestamper.js
CHANGED
@@ -108,7 +108,7 @@ function parseArguments(args) {
|
|
108
108
|
}
|
109
109
|
|
110
110
|
function validateOptions(options) {
|
111
|
-
const fileRegex = /^[\w,-]+\.[A-Za-z]{1,15}$/;
|
111
|
+
// const fileRegex = /^[\w,-]+\.[A-Za-z]{1,15}$/;
|
112
112
|
|
113
113
|
Object.entries(options).forEach(([key, value]) => {
|
114
114
|
if (value === undefined) {
|
@@ -116,9 +116,9 @@ function validateOptions(options) {
|
|
116
116
|
}
|
117
117
|
});
|
118
118
|
|
119
|
-
if (!fileRegex.test(path.basename(options.outPutFilePath))) {
|
120
|
-
|
121
|
-
}
|
119
|
+
// if (!fileRegex.test(path.basename(options.outPutFilePath))) {
|
120
|
+
// throw new TimestampError("invalid_filename", `Please enter a valid file name.`);
|
121
|
+
// }
|
122
122
|
|
123
123
|
if (options.templatePath && !fs.existsSync(options.templatePath)) {
|
124
124
|
throw new TimestampError("template_not_exists", `Template file does not exist.`);
|
package/index.js
CHANGED