@guritso/terminal 1.0.4 → 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/LICENSE CHANGED
@@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
631
631
  state the exclusion of warranty; and each file should have at least
632
632
  the "copyright" line and a pointer to where the full notice is found.
633
633
 
634
- <one line to give the program's name and a brief idea of what it does.>
635
- Copyright (C) <year> <name of author>
634
+ @guri/terminal enhanced terminal logging for node apps
635
+ Copyright (C) 2024 Guritso
636
636
 
637
637
  This program is free software: you can redistribute it and/or modify
638
638
  it under the terms of the GNU General Public License as published by
@@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
652
652
  If the program does terminal interaction, make it output a short
653
653
  notice like this when it starts in an interactive mode:
654
654
 
655
- @guri/terminal Copyright (C) 2024 GuriTsuki
655
+ @guri/terminal Copyright (C) 2024 Guritso
656
656
  This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657
657
  This is free software, and you are welcome to redistribute it
658
658
  under certain conditions; type `show c' for details.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @guri/terminal
1
+ # @guritso/terminal
2
2
 
3
3
  A terminal node utility for logging and error handling.
4
4
 
@@ -8,14 +8,14 @@ A terminal node utility for logging and error handling.
8
8
 
9
9
  ## Installation
10
10
 
11
- To install the package, use the following command:
11
+ To install the package:
12
12
 
13
13
  ```bash
14
- npm install github:GuriTsuki/terminal
14
+ npm install @guritso/terminal
15
15
  # or
16
- pnpm install github:GuriTsuki/terminal
16
+ pnpm install @guritso/terminal
17
17
  # or
18
- yarn add @guri/terminal@github:GuriTsuki/terminal
18
+ yarn add @guritso/terminal
19
19
  ```
20
20
 
21
21
  ## Usage
@@ -23,24 +23,32 @@ yarn add @guri/terminal@github:GuriTsuki/terminal
23
23
  To use the package, import it into your project:
24
24
 
25
25
  ```javascript
26
- import terminal from '@guri/terminal';
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.4",
4
+ "version": "1.0.6",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "license": "GPL-3.0",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "git+https://github.com/GuriTsuki/terminal.git"
13
+ "url": "git+https://github.com/guritso/terminal.git"
14
14
  },
15
15
  "keywords": [
16
16
  "terminal",
@@ -19,7 +19,7 @@
19
19
  "cli",
20
20
  "utility"
21
21
  ],
22
- "author": "GuriTsuki",
22
+ "author": "GuriTso",
23
23
  "devDependencies": {
24
24
  "jest": "^29.7.0"
25
25
  },
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "types": "./index.d.ts",
30
30
  "bugs": {
31
- "url": "https://github.com/GuriTsuki/terminal/issues"
31
+ "url": "https://github.com/guritso/terminal/issues"
32
32
  },
33
- "homepage": "https://github.com/GuriTsuki/terminal#readme"
33
+ "homepage": "https://github.com/guritso/terminal#readme"
34
34
  }
@@ -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);