@puya/ts 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +22 -2
- package/Timestamper.js +58 -34
- package/TimestamperCLI.js +10 -7
- package/bin/ts +4 -4
- package/package.json +3 -3
- /package/{test.js → run.js} +0 -0
package/README.md
CHANGED
@@ -45,6 +45,7 @@ ts [options]
|
|
45
45
|
- `-o [outputfile]`: The name of the output file where the result will be saved. Default is `info.json`.
|
46
46
|
- `-t [template]`: The path to a template file.
|
47
47
|
- `-f [format]`: The format string to use for formatting the date/time. Default is `YYYYMMDDHHmm`.
|
48
|
+
- `-so`: skip generating output file.
|
48
49
|
- `-i [inline-template]`: Inline template string.
|
49
50
|
|
50
51
|
#### Examples
|
@@ -83,6 +84,7 @@ ts [options]
|
|
83
84
|
|
84
85
|
You can also use Timestamper as a module in your Node.js projects.
|
85
86
|
|
87
|
+
Example 1.
|
86
88
|
```javascript
|
87
89
|
const { Timestamper } = require('@puya/ts');
|
88
90
|
|
@@ -90,11 +92,29 @@ const result = Timestamper({
|
|
90
92
|
locale: 'en',
|
91
93
|
outputFileName: 'result.json',
|
92
94
|
template: '{ "hash": "{ts}" }',
|
93
|
-
format: 'YYYYMMDDHHmmss'
|
95
|
+
format: 'YYYYMMDDHHmmss'
|
94
96
|
});
|
95
97
|
|
96
98
|
if (result.success) {
|
97
|
-
console.log(
|
99
|
+
console.log(`Timestamp "result.json" generated successfully`);
|
100
|
+
} else {
|
101
|
+
console.error('Failed to generate timestamp:', result.err);
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
Example 2. No output
|
106
|
+
```javascript
|
107
|
+
const { Timestamper } = require('@puya/ts');
|
108
|
+
|
109
|
+
const result = Timestamper({
|
110
|
+
locale: 'en',
|
111
|
+
skipOutput: true,
|
112
|
+
template: '{ "hash": "{ts}" }',
|
113
|
+
format: 'YYYYMMDDHHmmss'
|
114
|
+
});
|
115
|
+
|
116
|
+
if (result.success) {
|
117
|
+
console.log('Timestamp: ', result.data);
|
98
118
|
} else {
|
99
119
|
console.error('Failed to generate timestamp:', result.err);
|
100
120
|
}
|
package/Timestamper.js
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
const path = require("path");
|
2
2
|
const fs = require("fs");
|
3
3
|
const moment = require("jalali-moment");
|
4
|
+
const package = require('./package.json');
|
4
5
|
|
5
|
-
const version =
|
6
|
+
const version = package.version;
|
6
7
|
|
7
8
|
let default_options = {
|
8
9
|
locale: "en",
|
@@ -12,7 +13,8 @@ let default_options = {
|
|
12
13
|
templatePath: "",
|
13
14
|
format: "YYYYMMDDHHmm",
|
14
15
|
inlineTemplate: "",
|
15
|
-
hasInlineTemplate: false
|
16
|
+
hasInlineTemplate: false,
|
17
|
+
skipOutput: false
|
16
18
|
};
|
17
19
|
|
18
20
|
class TimestampError extends Error {
|
@@ -39,10 +41,19 @@ function start(args) {
|
|
39
41
|
if (result.state === "successful") {
|
40
42
|
result.options = Object.assign(default_options, options);
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
if (default_options.version === true) {
|
45
|
+
console.log(version);
|
46
|
+
} else {
|
47
|
+
|
48
|
+
const ts = generateTimestamp(options.locale, options.format);
|
49
|
+
const data = generateData(ts, result.options);
|
50
|
+
|
51
|
+
if (!options.skipOutput) {
|
52
|
+
writeOutput(data, result.options.outPutFilePath);
|
53
|
+
}
|
54
|
+
|
55
|
+
result.data = data;
|
56
|
+
}
|
46
57
|
|
47
58
|
result.success = true;
|
48
59
|
}
|
@@ -93,12 +104,20 @@ function parseArguments(args) {
|
|
93
104
|
default_options.format = args[i + 1];
|
94
105
|
i += 2;
|
95
106
|
break;
|
107
|
+
case '-so':
|
108
|
+
default_options.skipOutput = true;
|
109
|
+
i++;
|
110
|
+
break;
|
96
111
|
case '-i':
|
97
112
|
default_options.inlineTemplate = args[i + 1];
|
98
113
|
default_options.template = default_options.inlineTemplate;
|
99
114
|
default_options.hasInlineTemplate = true;
|
100
115
|
i += 2;
|
101
116
|
break;
|
117
|
+
case '-v':
|
118
|
+
default_options.version = true;
|
119
|
+
i++;
|
120
|
+
break;
|
102
121
|
default:
|
103
122
|
throw new Error(`Invalid argument provided: ${args[i]}`);
|
104
123
|
}
|
@@ -110,35 +129,40 @@ function parseArguments(args) {
|
|
110
129
|
function validateOptions(options) {
|
111
130
|
// const fileRegex = /^[\w,-]+\.[A-Za-z]{1,15}$/;
|
112
131
|
|
113
|
-
|
114
|
-
if (
|
115
|
-
throw new TimestampError(
|
132
|
+
if (options.version !== true) {
|
133
|
+
if (options.skipOutput && !options.outPutFilePath) {
|
134
|
+
throw new TimestampError("output_confusion", `Please make up your mind buddy. Do you want me to generate the output for you or not?`);
|
135
|
+
}
|
136
|
+
|
137
|
+
Object.entries(options).forEach(([key, value]) => {
|
138
|
+
if (value === undefined) {
|
139
|
+
throw new TimestampError(`${key}_required`, `${key} is required after -${key.slice(0, 1)}`);
|
140
|
+
}
|
141
|
+
});
|
142
|
+
|
143
|
+
// if (!fileRegex.test(path.basename(options.outPutFilePath))) {
|
144
|
+
// throw new TimestampError("invalid_filename", `Please enter a valid file name.`);
|
145
|
+
// }
|
146
|
+
|
147
|
+
if (options.templatePath && !fs.existsSync(options.templatePath)) {
|
148
|
+
throw new TimestampError("template_not_exists", `Template file does not exist.`);
|
149
|
+
}
|
150
|
+
|
151
|
+
if (!isValidDateFormat(options.format)) {
|
152
|
+
throw new TimestampError("invalid_format", `Please enter a valid format.`);
|
153
|
+
}
|
154
|
+
|
155
|
+
if (options.inlineTemplate !== "") {
|
156
|
+
options.hasInlineTemplate = true;
|
157
|
+
}
|
158
|
+
|
159
|
+
if (options.hasInlineTemplate && options.inlineTemplate === "") {
|
160
|
+
throw new TimestampError("invalid_inline_template", `Please provide a valid inline template.`);
|
161
|
+
}
|
162
|
+
|
163
|
+
if (options.templatePath && options.inlineTemplate != "") {
|
164
|
+
throw new TimestampError("extra_template", `You can use only one template.`);
|
116
165
|
}
|
117
|
-
});
|
118
|
-
|
119
|
-
// if (!fileRegex.test(path.basename(options.outPutFilePath))) {
|
120
|
-
// throw new TimestampError("invalid_filename", `Please enter a valid file name.`);
|
121
|
-
// }
|
122
|
-
|
123
|
-
if (options.templatePath && !fs.existsSync(options.templatePath)) {
|
124
|
-
throw new TimestampError("template_not_exists", `Template file does not exist.`);
|
125
|
-
}
|
126
|
-
|
127
|
-
if (!isValidDateFormat(options.format)) {
|
128
|
-
|
129
|
-
throw new TimestampError("invalid_format", `Please enter a valid format.`);
|
130
|
-
}
|
131
|
-
|
132
|
-
if (options.inlineTemplate !== "") {
|
133
|
-
options.hasInlineTemplate = true;
|
134
|
-
}
|
135
|
-
|
136
|
-
if (options.hasInlineTemplate && options.inlineTemplate === "") {
|
137
|
-
throw new TimestampError("invalid_inline_template", `Please provide a valid inline template.`);
|
138
|
-
}
|
139
|
-
|
140
|
-
if (options.templatePath && options.inlineTemplate != "") {
|
141
|
-
throw new TimestampError("extra_template", `You can use only one template.`);
|
142
166
|
}
|
143
167
|
|
144
168
|
return "successful";
|
package/TimestamperCLI.js
CHANGED
@@ -6,12 +6,13 @@ Timestamper v${version}
|
|
6
6
|
Usage: ts [options]
|
7
7
|
|
8
8
|
Options:
|
9
|
-
-v
|
10
|
-
-l
|
11
|
-
-o
|
12
|
-
-t
|
13
|
-
-f
|
14
|
-
-i
|
9
|
+
-v version
|
10
|
+
-l locale to use for formatting the date/time. e.g. 'en' or 'fa' (default = en).
|
11
|
+
-o output file name where the result will be saved. default is info.json.
|
12
|
+
-t template file.
|
13
|
+
-f format string to use for formatting the date/time. default is "YYYYMMDDHHmm".
|
14
|
+
-i inline template string.
|
15
|
+
-so skip generating output file.
|
15
16
|
|
16
17
|
Examples:
|
17
18
|
ts
|
@@ -31,7 +32,9 @@ function TimestamperCLI(args) {
|
|
31
32
|
const result = ts(args);
|
32
33
|
|
33
34
|
if (result.success) {
|
34
|
-
|
35
|
+
if (result.options.version !== true) {
|
36
|
+
console.log(`File ${result.options.outputFileName} generated at ${result.options.outPutFilePath}`);
|
37
|
+
}
|
35
38
|
} else {
|
36
39
|
if (result.err) {
|
37
40
|
throw result.err;
|
package/bin/ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
|
3
|
-
var path = require('path');
|
4
|
-
var fs = require('fs');
|
5
|
-
require(path.join(path.dirname(fs.realpathSync(__filename)), '../
|
2
|
+
|
3
|
+
var path = require('path');
|
4
|
+
var fs = require('fs');
|
5
|
+
require(path.join(path.dirname(fs.realpathSync(__filename)), '../run'));
|
package/package.json
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "@puya/ts",
|
3
|
-
"version": "1.0
|
3
|
+
"version": "1.1.0",
|
4
4
|
"description": "This library provides timestamp and supports custom templates for its output file.",
|
5
5
|
"main": "index.js",
|
6
6
|
"bin": {
|
7
7
|
"ts": "bin/ts"
|
8
8
|
},
|
9
9
|
"scripts": {
|
10
|
-
"test": "node
|
10
|
+
"test": "node run.js"
|
11
11
|
},
|
12
12
|
"repository": {
|
13
13
|
"type": "git",
|
@@ -30,7 +30,7 @@
|
|
30
30
|
"jalali-moment": "^3.3.11"
|
31
31
|
},
|
32
32
|
"files": [
|
33
|
-
"
|
33
|
+
"run.js",
|
34
34
|
"Timestamper.js",
|
35
35
|
"TimestamperCLI.js",
|
36
36
|
"bin/ts"
|
/package/{test.js → run.js}
RENAMED
File without changes
|