@guritso/terminal 1.0.5 → 1.0.6

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
@@ -26,21 +26,29 @@ To use the package, import it into your project:
26
26
  import terminal from '@guritso/terminal';
27
27
  // Setup the terminal (this is necessary to use the console.error function)
28
28
  terminal.setup();
29
+
29
30
  // Start the terminal with a your project's specific host and port ( both are optional) its only used for the project info
30
31
  terminal.start('http://localhost', 3000);
32
+
31
33
  // Log an information message
32
34
  terminal.log('This is an info message');
35
+
33
36
  // Log a success message
34
37
  terminal.pass('This is a success message');
35
- // Log an error message (it detects if the data is an error and display it with the terminal.error function)
38
+
39
+ // Log an error message (it detects if the data is an error and display it with the fail log level)
36
40
  terminal.log(new Error('This is an error message'));
37
41
  ```
38
42
 
39
- ## API
43
+ ## Methods
40
44
 
41
- ### `terminal.start(host, port)`
45
+ ### `terminal.setup()`
46
+
47
+ Sets up the console.error to use the terminal.log function. Every error message will be displayed with the fail log level even if you don't use the terminal.log function.
42
48
 
43
- Displays the project info and the host and port.
49
+ ### `terminal.start(host, port)` (optional)
50
+
51
+ Displays the project info and the host and port. if you want to display the project info on start of your app, this is a nice way to do it.
44
52
 
45
53
  - `host` (string): The host to display.
46
54
  - `port` (number): The port number to display.
@@ -59,13 +67,12 @@ Displays a log message.
59
67
 
60
68
  ### `terminal.setVerbose(verbose)`
61
69
 
62
- Sets the verbose level. (0 = no output, 1 = same line output (does't apply for pass), 2 = new line output)
70
+ Sets the verbose level. (0 = no output (does't apply for start()), 1 = same line output (does't apply for pass), 2 = new line output)
63
71
 
64
72
  - `verbose` (number): The verbose level.
65
73
 
66
- ### `terminal.setup()`
67
74
 
68
- Sets up the console.error to use the terminal.log function.
75
+ ## Methods
69
76
 
70
77
  ## License
71
78
 
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import terminal from "./index.js";
2
2
 
3
- declare module "@guri/terminal" {
3
+ declare module "@guritso/terminal" {
4
4
  export default terminal;
5
5
  }
package/index.js CHANGED
@@ -25,9 +25,9 @@ import { readFileSync } from "fs";
25
25
  const terminal = {
26
26
  verbose: 2,
27
27
  levels: {
28
- info: co("%H100 info:%H"),
29
- fail: co("%H41 fail:%H"),
30
- pass: co("%H42 pass:%H"),
28
+ info: co("%H100 INFO:%H"),
29
+ fail: co("%H41 FAIL:%H"),
30
+ pass: co("%H42 PASS:%H"),
31
31
  },
32
32
  };
33
33
 
@@ -139,10 +139,22 @@ terminal.isError = (data) => {
139
139
  * Setup the console.error to use the terminal.log function
140
140
  */
141
141
  terminal.setup = function setup() {
142
- // backup the original console.error
143
- console.backup = console.error;
142
+ if (typeof console === "object" && console.error) {
143
+
144
+ if (typeof console.backup === "function") {
145
+ return false
146
+ }
147
+
148
+ // backup the original console.error
149
+ console.backup = console.error;
150
+ } else {
151
+ throw new Error("console.error is not found");
152
+ }
153
+
144
154
  // replace the console.error with the terminal.log
145
155
  console.error = terminal.log;
156
+
157
+ return true
146
158
  };
147
159
 
148
160
  terminal.clear = function clear() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@guritso/terminal",
3
3
  "description": "A terminal node utility for logging and error handling",
4
- "version": "1.0.5",
4
+ "version": "1.0.6",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "license": "GPL-3.0",
@@ -5,7 +5,6 @@ import terminal from "../index.js";
5
5
  describe("Terminal Module", () => {
6
6
  beforeEach(() => {
7
7
  jest.resetAllMocks();
8
- terminal.setup();
9
8
  });
10
9
 
11
10
  describe("start", () => {
@@ -29,7 +28,7 @@ describe("Terminal Module", () => {
29
28
  .mockImplementation(() => true);
30
29
  terminal.pass("Operation successful");
31
30
 
32
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("pass"));
31
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("PASS"));
33
32
  mockWrite.mockRestore();
34
33
  });
35
34
  });
@@ -54,7 +53,7 @@ describe("Terminal Module", () => {
54
53
  .mockImplementation(() => true);
55
54
 
56
55
  terminal.log("Info message");
57
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("info"));
56
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("INFO"));
58
57
  mockWrite.mockRestore();
59
58
  });
60
59
 
@@ -65,7 +64,7 @@ describe("Terminal Module", () => {
65
64
  .mockImplementation(() => true);
66
65
 
67
66
  terminal.log("Error: Something went wrong");
68
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("fail"));
67
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("FAIL"));
69
68
  mockWrite.mockRestore();
70
69
  });
71
70
 
@@ -106,6 +105,36 @@ describe("Terminal Module", () => {
106
105
 
107
106
  console.error = originalError;
108
107
  });
108
+
109
+ it("should backup the original console.error", () => {
110
+ const originalError = console.error;
111
+
112
+ terminal.setup();
113
+
114
+ expect(console.backup).toBe(originalError);
115
+
116
+ console.error = originalError;
117
+ });
118
+
119
+ it("should not replace console.error if console.backup exists", () => {
120
+ console.backup = jest.fn();
121
+ const originalError = console.error;
122
+ const result = terminal.setup();
123
+
124
+ expect(result).toBe(false);
125
+ expect(console.error).toBe(originalError);
126
+
127
+ delete console.backup;
128
+ });
129
+
130
+ it("should throw an error if console.error is not found", () => {
131
+ const originalConsole = global.console;
132
+ global.console = { log: jest.fn() };
133
+
134
+ expect(() => terminal.setup()).toThrow("console.error is not found");
135
+
136
+ global.console = originalConsole;
137
+ });
109
138
  });
110
139
 
111
140
  describe("clear", () => {
package/log.js DELETED
@@ -1,56 +0,0 @@
1
- import terminal from "./index.js";
2
-
3
-
4
- terminal.log(
5
- terminal.projectInfo
6
- );
7
-
8
- terminal.setup();
9
- terminal.setVerbose(1);
10
- terminal.start("ftp://example.com", 3000);
11
- terminal.start("localhost", 3000);
12
- terminal.start("localhost");
13
- terminal.start({ host: "localhost" }, 3000);
14
- terminal.start("http://localhost:3000");
15
- terminal.start("http://localhost", "5ç688o");
16
- terminal.start("https://localhost");
17
- terminal.start("http://127.0.0.1", 3000);
18
- terminal.start("http://127.0.0.1:3000");
19
- terminal.start("http://127.0.0.1");
20
- terminal.start("https://127.0.0.1");
21
- terminal.start("http://192.168.0.1", 3000);
22
- terminal.start("http://192.168.0.1:3000");
23
- terminal.start("http://192.168.0.1");
24
- terminal.start("https://192.168.0.1");
25
- terminal.start("http://www.google.com");
26
- terminal.start("http://example.com", 8080);
27
- terminal.start("http://example.com");
28
- terminal.start("https://example.com");
29
- terminal.start("http://www.example.com");
30
- terminal.start("http://test.com", 4000);
31
- terminal.start("http://test.com");
32
- terminal.start("https://test.com");
33
- terminal.start("http://www.test.com");
34
- terminal.start({
35
- host: "localhost",
36
- port: 3000,
37
- });
38
- terminal.log("This is a log message");
39
- terminal.pass("This is a pass message");
40
- terminal.pass("This is a pass message");
41
- terminal.pass("This is a pass message");
42
-
43
- terminal.pass("This is a pass message");
44
- terminal.log("This is a log message");
45
-
46
- terminal.setVerbose(2);
47
-
48
- terminal.start("http://localhost:3000");
49
- terminal.log("loaded routes!");
50
- terminal.pass("server online!");
51
-
52
- const error = new Error("failed to load users");
53
-
54
- error.stack = error.stack.replace("/israel/Documentos/", "/guri/projects/");
55
-
56
- terminal.log(error);