@puya/ts 1.0.0 → 1.1.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/README.md CHANGED
@@ -1,8 +1,121 @@
1
1
  # Timestamper
2
- This library provides timestamp and supports custom templates for its output file.
3
2
 
4
- # Install
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
22
+ ```
23
+
24
+ ### Local Installation
25
+
26
+ Install locally in your project:
27
+
28
+ ```bash
29
+ npm install @puya/ts
6
30
  ```
7
- npm i @puya/ts
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
+ - `-so`: skip generating output file.
49
+ - `-i [inline-template]`: Inline template string.
50
+
51
+ #### Examples
52
+
53
+ 1. Add a timestamp using the default settings:
54
+
55
+ ```bash
56
+ ts
57
+ ```
58
+
59
+ 2. Add a timestamp to a custom output file with a specific format:
60
+
61
+ ```bash
62
+ ts -l en -o result.json -f YYYYMMDDHHmmss
63
+ ```
64
+
65
+ 3. Use a custom template file:
66
+
67
+ ```bash
68
+ ts -l fa -o result.json -t template.txt
69
+ ```
70
+
71
+ 4. Use an inline template:
72
+
73
+ ```bash
74
+ ts -l fa -o result.json -i "{ test: '{ts}' }"
75
+ ```
76
+
77
+ 5. Get help:
78
+
79
+ ```bash
80
+ ts --help
81
+ ```
82
+
83
+ ### Programmatic Use
84
+
85
+ You can also use Timestamper as a module in your Node.js projects.
86
+
87
+ Example 1.
88
+ ```javascript
89
+ const { Timestamper } = require('@puya/ts');
90
+
91
+ const result = Timestamper({
92
+ locale: 'en',
93
+ outputFileName: 'result.json',
94
+ template: '{ "hash": "{ts}" }',
95
+ format: 'YYYYMMDDHHmmss'
96
+ });
97
+
98
+ if (result.success) {
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);
118
+ } else {
119
+ console.error('Failed to generate timestamp:', result.err);
120
+ }
8
121
  ```
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 = '1.0.0';
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
- const ts = generateTimestamp(options.locale, options.format);
43
- const data = generateData(ts, result.options);
44
-
45
- writeOutput(data, result.options.outPutFilePath);
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
  }
@@ -108,37 +127,42 @@ function parseArguments(args) {
108
127
  }
109
128
 
110
129
  function validateOptions(options) {
111
- const fileRegex = /^[\w,-]+\.[A-Za-z]{1,15}$/;
130
+ // const fileRegex = /^[\w,-]+\.[A-Za-z]{1,15}$/;
112
131
 
113
- Object.entries(options).forEach(([key, value]) => {
114
- if (value === undefined) {
115
- throw new TimestampError(`${key}_required`, `${key} is required after -${key.slice(0, 1)}`);
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 --version version
10
- -l --locale locale to use for formatting the date/time. e.g. 'en' or 'fa' (default = en).
11
- -o --output output file name where the result will be saved. default is info.json.
12
- -t --template template file.
13
- -f --format format string to use for formatting the date/time. default is "YYYYMMDDHHmm".
14
- -i --inline-template inline template string.
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
- console.log(`File ${result.options.outputFileName} generated at ${result.options.outPutFilePath}`);
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)), '../test'));
2
+
3
+ var path = require('path');
4
+ var fs = require('fs');
5
+ require(path.join(path.dirname(fs.realpathSync(__filename)), '../run'));
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { ts } = require('./Timestamper')
2
- //const cli = require('./TimestamperCLI');
2
+ const cli = require('./TimestamperCLI');
3
3
 
4
- module.exports = ts;
4
+ module.exports.Timestamper = ts;
5
+ module.exports.TimestamperCLI = TimestamperCLI;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@puya/ts",
3
- "version": "1.0.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 test.js"
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
- "test.js",
33
+ "run.js",
34
34
  "Timestamper.js",
35
35
  "TimestamperCLI.js",
36
36
  "bin/ts"
File without changes