@guritso/terminal 1.0.5 → 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
@@ -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.js CHANGED
@@ -1,164 +1 @@
1
- "use strict";
2
-
3
- import { color as co, getErrorNames, formatUrl } from "./utils.js";
4
- import { readFileSync } from "fs";
5
-
6
-
7
- /**
8
- * @typedef {Object} Terminal
9
- * @property {number} verbose
10
- * @property {Object} levels
11
- * @property {string} levels.info
12
- * @property {string} levels.fail
13
- * @property {string} levels.pass
14
- * @property {function(string, number): void} start
15
- * @property {function(string): void} pass
16
- * @property {function(string): void} log
17
- * @property {function(): void} setup
18
- * @property {function(): void} clear
19
- * @property {function(number): void} setVerbose
20
- * @property {function(any): boolean} isError
21
- * @property {Object} projectInfo
22
- */
23
-
24
- /** @type {Terminal} */
25
- const terminal = {
26
- verbose: 2,
27
- levels: {
28
- info: co("%H100 info:%H"),
29
- fail: co("%H41 fail:%H"),
30
- pass: co("%H42 pass:%H"),
31
- },
32
- };
33
-
34
- const { stdout } = process;
35
-
36
- terminal.projectInfo = (() => {
37
- try {
38
- const { name, version } = JSON.parse(
39
- readFileSync("./package.json", "utf-8")
40
- );
41
-
42
- return { name, version };
43
- } catch (error) {
44
- return { name: "unknown", version: "unknown" };
45
- }
46
- })();
47
-
48
- /**
49
- * display the project info and the port
50
- *
51
- * @param {string} host - The host to display
52
- * @param {number} port - The port number to display
53
- */
54
- terminal.start = function start(host, port) {
55
- const projectInfo = terminal.projectInfo;
56
- const hostInfo = formatUrl(host, port);
57
-
58
- const headLines = [
59
- `\n%H46 name:%H%H44 ${projectInfo.name} `,
60
- `%H105 version:%H%H41 ${projectInfo.version} %H\n`,
61
- hostInfo?.url ? `%H43 host:%H95 ${hostInfo.url}\n` : "",
62
- hostInfo?.port ? `%H45 port:%H94 ${hostInfo.port}\n` : "",
63
- ];
64
-
65
- for (const line of headLines) {
66
- stdout.write(co(line));
67
- }
68
- };
69
-
70
- /**
71
- * display the online message
72
- *
73
- * @param {string} data
74
- */
75
- terminal.pass = function pass(data) {
76
- terminal.clear();
77
- stdout.write(co(`\r%H1 ${terminal.levels.pass}%H ${data}\n`));
78
- };
79
-
80
- /**
81
- * display logs messages
82
- *
83
- * @param {string} data
84
- */
85
- terminal.log = function log(data) {
86
- const { verbose, levels } = terminal;
87
-
88
- let level = levels.info;
89
-
90
- if (Number(verbose) === 0) return;
91
-
92
- if (terminal.isError(data)) {
93
- level = levels.fail;
94
- }
95
-
96
- if (Number(verbose) === 1 && stdout.isTTY) {
97
- terminal.clear();
98
-
99
- stdout.write(co(`\r%H1 ${level}%H ${data}`));
100
- } else {
101
- stdout.write(co(`%H1 ${level}%H `));
102
-
103
- if (level === terminal.levels.info) {
104
- if (typeof data === "object") {
105
- // skipcq: JS-0002
106
- console.log(data);
107
- } else {
108
- stdout.write(`${co(data)}\n`);
109
- }
110
- } else {
111
- // skipcq: JS-0002
112
- console.log(data);
113
- }
114
- }
115
- };
116
-
117
- /**
118
- * Check if the data is an error
119
- *
120
- * @param {any} data
121
- * @returns {boolean}
122
- */
123
- terminal.isError = (data) => {
124
- if (data instanceof Error) {
125
- return true;
126
- }
127
-
128
- if (typeof data === "string") {
129
- const errorKeywords = getErrorNames().map((name) => `Error: ${name}`);
130
- errorKeywords.push("Error:");
131
-
132
- return errorKeywords.some((keyword) => data.includes(keyword));
133
- }
134
-
135
- return false;
136
- };
137
-
138
- /**
139
- * Setup the console.error to use the terminal.log function
140
- */
141
- terminal.setup = function setup() {
142
- // backup the original console.error
143
- console.backup = console.error;
144
- // replace the console.error with the terminal.log
145
- console.error = terminal.log;
146
- };
147
-
148
- terminal.clear = function clear() {
149
- if (stdout.isTTY) {
150
- stdout.clearLine();
151
- }
152
- };
153
-
154
- /**
155
- * Set the verbose level
156
- *
157
- * @param {number} verbose - The verbose level
158
- */
159
- terminal.setVerbose = function setVerbose(verbose) {
160
- terminal.verbose = verbose;
161
- };
162
-
163
-
164
- export default terminal;
1
+ module.exports = require('./lib/terminal.js');
@@ -0,0 +1,190 @@
1
+ /*!
2
+ * Terminal
3
+ * Copyright (c) 2024 @GuriTso
4
+ * @license GPL-3.0
5
+ */
6
+
7
+ "use strict";
8
+
9
+ const getErrorNames = require("./utils").getErrorNames;
10
+ const formatUrl = require("./utils").formatUrl;
11
+ const co = require("./utils").color;
12
+ const { readFileSync } = require("fs");
13
+
14
+ /**
15
+ * @typedef {Object} Terminal
16
+ * @property {number} verbose
17
+ * @property {Object} levels
18
+ * @property {string} levels.info
19
+ * @property {string} levels.fail
20
+ * @property {string} levels.pass
21
+ * @property {function(string, number): void} start
22
+ * @property {function(string): void} pass
23
+ * @property {function(string): void} log
24
+ * @property {function(): void} setup
25
+ * @property {function(): void} clear
26
+ * @property {function(number): void} setVerbose
27
+ * @property {function(any): boolean} isError
28
+ * @property {Object} projectInfo
29
+ */
30
+
31
+ /** @type {Terminal} */
32
+ const terminal = {
33
+ verbose: 2,
34
+ levels: {
35
+ info: co("%H100 INFO:%H"),
36
+ fail: co("%H41 FAIL:%H"),
37
+ pass: co("%H42 PASS:%H"),
38
+ },
39
+ };
40
+
41
+ const { stdout } = process;
42
+
43
+ terminal.projectInfo = (() => {
44
+ try {
45
+ const { name, version } = JSON.parse(
46
+ readFileSync("./package.json", "utf-8")
47
+ );
48
+
49
+ return { name, version };
50
+ } catch (error) {
51
+ return { name: "unknown", version: "unknown" };
52
+ }
53
+ })();
54
+
55
+ /**
56
+ * display the project info and the port
57
+ *
58
+ * @param {string} host - The host to display
59
+ * @param {number} port - The port number to display
60
+ */
61
+ terminal.start = function start(host, port) {
62
+ const projectInfo = terminal.projectInfo;
63
+ const hostInfo = formatUrl(host, port);
64
+
65
+ const headLines = [
66
+ `\n%H46 name:%H%H44 ${projectInfo.name} `,
67
+ `%H105 version:%H%H41 ${projectInfo.version} %H\n`,
68
+ hostInfo?.url ? `%H43 host:%H95 ${hostInfo.url}\n` : "",
69
+ hostInfo?.port ? `%H45 port:%H94 ${hostInfo.port}\n` : "",
70
+ ];
71
+
72
+ for (const line of headLines) {
73
+ stdout.write(co(line));
74
+ }
75
+ };
76
+
77
+ /**
78
+ * display the online message
79
+ *
80
+ * @param {string} data
81
+ */
82
+ terminal.pass = function pass(data) {
83
+ terminal.clear();
84
+ stdout.write(co(`\r%H1 ${terminal.levels.pass}%H ${data}\n`));
85
+ };
86
+
87
+ /**
88
+ * display logs messages
89
+ *
90
+ * @param {string} data
91
+ */
92
+ terminal.log = function log(data) {
93
+ const { verbose, levels } = terminal;
94
+
95
+ let level = levels.info;
96
+
97
+ if (Number(verbose) === 0) return;
98
+
99
+ if (terminal.isError(data)) {
100
+ level = levels.fail;
101
+ }
102
+
103
+ if (Number(verbose) === 1 && stdout.isTTY) {
104
+ terminal.clear();
105
+
106
+ stdout.write(co(`\r%H1 ${level}%H ${data}`));
107
+ } else {
108
+ stdout.write(co(`%H1 ${level}%H `));
109
+
110
+ if (level === terminal.levels.info) {
111
+ if (typeof data === "object") {
112
+ // skipcq: JS-0002
113
+ console.log(data);
114
+ } else {
115
+ stdout.write(`${co(data)}\n`);
116
+ }
117
+ } else {
118
+ // skipcq: JS-0002
119
+ console.log(data);
120
+ }
121
+ }
122
+ };
123
+
124
+ /**
125
+ * Check if the data is an error
126
+ *
127
+ * @param {any} data
128
+ * @returns {boolean}
129
+ */
130
+ terminal.isError = (data) => {
131
+ if (data instanceof Error) {
132
+ return true;
133
+ }
134
+
135
+ if (typeof data === "string") {
136
+ const errorKeywords = getErrorNames().map((name) => `Error: ${name}`);
137
+ errorKeywords.push("Error:");
138
+
139
+ return errorKeywords.some((keyword) => data.includes(keyword));
140
+ }
141
+
142
+ return false;
143
+ };
144
+
145
+ /**
146
+ * Setup the console.error to use the terminal.log function
147
+ *
148
+ * @returns {boolean} - true if the setup was successful, false otherwise
149
+ */
150
+ terminal.setup = () => {
151
+ if (typeof console === "object" && console.error) {
152
+
153
+ if (typeof console.backup === "function") {
154
+ return false
155
+ }
156
+
157
+ // backup the original console.error
158
+ console.backup = console.error;
159
+ } else {
160
+ throw new Error("console.error is not found");
161
+ }
162
+
163
+ // replace the console.error with the terminal.log
164
+ console.error = terminal.log;
165
+
166
+ return true
167
+ };
168
+
169
+ /**
170
+ * Clear the terminal
171
+ */
172
+ terminal.clear = () => {
173
+ if (stdout.isTTY) {
174
+ stdout.clearLine();
175
+ return true
176
+ }
177
+
178
+ return false
179
+ };
180
+
181
+ /**
182
+ * Set the verbose level
183
+ *
184
+ * @param {number} verbose - The verbose level
185
+ */
186
+ terminal.setVerbose = (verbose) => {
187
+ terminal.verbose = verbose;
188
+ };
189
+
190
+ module.exports = terminal;
@@ -1,3 +1,11 @@
1
+ /*!
2
+ * Terminal
3
+ * Copyright (c) 2024 @GuriTso
4
+ * @license GPL-3.0
5
+ */
6
+
7
+ const { URL } = require("url");
8
+
1
9
  /**
2
10
  * This function is used to colorize the text
3
11
  * @param {string} txt - The text to colorize
@@ -6,7 +14,7 @@
6
14
  * color("Hello %H1 World") // "Hello \x1b[1mWorld\x1b[0m"
7
15
  * color("%h1 start here%H nothing here") // "\x1b[1m start here\x1b[0m nothing here"
8
16
  */
9
- export const color = (txt) => {
17
+ function color(txt) {
10
18
  if (typeof txt !== "string") return txt;
11
19
 
12
20
  const arr = txt.split("%H");
@@ -29,7 +37,7 @@ export const color = (txt) => {
29
37
  *
30
38
  * @returns {string[]}
31
39
  */
32
- export const getErrorNames = () => {
40
+ function getErrorNames() {
33
41
  return Object.getOwnPropertyNames(global).filter((name) => {
34
42
  try {
35
43
  return (
@@ -49,7 +57,7 @@ export const getErrorNames = () => {
49
57
  * @param {number} port - The port to format.
50
58
  * @returns {string} - The formatted URL.
51
59
  */
52
- export const formatUrl = (host, port) => {
60
+ function formatUrl(host, port) {
53
61
  const locals = ["localhost", "127.0.0.1", "0.0.0.0"];
54
62
 
55
63
  try {
@@ -78,3 +86,8 @@ export const formatUrl = (host, port) => {
78
86
  }
79
87
  }
80
88
  };
89
+
90
+ exports.color = color;
91
+ exports.getErrorNames = getErrorNames;
92
+ exports.formatUrl = formatUrl;
93
+
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@guritso/terminal",
3
3
  "description": "A terminal node utility for logging and error handling",
4
- "version": "1.0.5",
5
- "type": "module",
6
- "main": "index.js",
4
+ "version": "1.1.0",
5
+ "main": "index.cjs",
6
+ "module": "index.js",
7
7
  "license": "GPL-3.0",
8
8
  "scripts": {
9
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
9
+ "test": "jest"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -24,9 +24,9 @@
24
24
  "jest": "^29.7.0"
25
25
  },
26
26
  "directories": {
27
+ "lib": "lib",
27
28
  "test": "tests"
28
29
  },
29
- "types": "./index.d.ts",
30
30
  "bugs": {
31
31
  "url": "https://github.com/guritso/terminal/issues"
32
32
  },
@@ -1,11 +1,8 @@
1
- import { jest } from "@jest/globals";
2
-
3
- import terminal from "../index.js";
1
+ const terminal = require("../index.js");
4
2
 
5
3
  describe("Terminal Module", () => {
6
4
  beforeEach(() => {
7
5
  jest.resetAllMocks();
8
- terminal.setup();
9
6
  });
10
7
 
11
8
  describe("start", () => {
@@ -29,7 +26,7 @@ describe("Terminal Module", () => {
29
26
  .mockImplementation(() => true);
30
27
  terminal.pass("Operation successful");
31
28
 
32
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("pass"));
29
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("PASS"));
33
30
  mockWrite.mockRestore();
34
31
  });
35
32
  });
@@ -54,7 +51,7 @@ describe("Terminal Module", () => {
54
51
  .mockImplementation(() => true);
55
52
 
56
53
  terminal.log("Info message");
57
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("info"));
54
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("INFO"));
58
55
  mockWrite.mockRestore();
59
56
  });
60
57
 
@@ -65,7 +62,7 @@ describe("Terminal Module", () => {
65
62
  .mockImplementation(() => true);
66
63
 
67
64
  terminal.log("Error: Something went wrong");
68
- expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("fail"));
65
+ expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining("FAIL"));
69
66
  mockWrite.mockRestore();
70
67
  });
71
68
 
@@ -106,6 +103,36 @@ describe("Terminal Module", () => {
106
103
 
107
104
  console.error = originalError;
108
105
  });
106
+
107
+ it("should backup the original console.error", () => {
108
+ const originalError = console.error;
109
+
110
+ terminal.setup();
111
+
112
+ expect(console.backup).toBe(originalError);
113
+
114
+ console.error = originalError;
115
+ });
116
+
117
+ it("should not replace console.error if console.backup exists", () => {
118
+ console.backup = jest.fn();
119
+ const originalError = console.error;
120
+ const result = terminal.setup();
121
+
122
+ expect(result).toBe(false);
123
+ expect(console.error).toBe(originalError);
124
+
125
+ delete console.backup;
126
+ });
127
+
128
+ it("should throw an error if console.error is not found", () => {
129
+ const originalConsole = global.console;
130
+ global.console = { log: jest.fn() };
131
+
132
+ expect(() => terminal.setup()).toThrow("console.error is not found");
133
+
134
+ global.console = originalConsole;
135
+ });
109
136
  });
110
137
 
111
138
  describe("clear", () => {
package/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import terminal from "./index.js";
2
-
3
- declare module "@guri/terminal" {
4
- export default terminal;
5
- }
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);