@guritso/terminal 1.0.6 → 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/index.js CHANGED
@@ -1,176 +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
- 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
-
154
- // replace the console.error with the terminal.log
155
- console.error = terminal.log;
156
-
157
- return true
158
- };
159
-
160
- terminal.clear = function clear() {
161
- if (stdout.isTTY) {
162
- stdout.clearLine();
163
- }
164
- };
165
-
166
- /**
167
- * Set the verbose level
168
- *
169
- * @param {number} verbose - The verbose level
170
- */
171
- terminal.setVerbose = function setVerbose(verbose) {
172
- terminal.verbose = verbose;
173
- };
174
-
175
-
176
- 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.6",
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,6 +1,4 @@
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(() => {
package/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import terminal from "./index.js";
2
-
3
- declare module "@guritso/terminal" {
4
- export default terminal;
5
- }