@akc42/server-utils 3.1.0 → 3.2.1
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 +1 -2
- package/debug.js +12 -44
- package/logger.js +2 -11
- package/package.json +1 -1
- package/version.js +8 -8
package/README.md
CHANGED
|
@@ -41,8 +41,7 @@ to `dumpDebugCache`
|
|
|
41
41
|
|
|
42
42
|
Breaking change as of 3.0.0 logger is now an async function returning a promise fulfilled when (if set) a log file entry is made
|
|
43
43
|
|
|
44
|
-
both `Debug` and `logger`
|
|
45
|
-
console. If it is not undefined these two modules will write to the log file rather than the console.
|
|
44
|
+
both `Debug` and `logger` have had their file logging removed as its causing more issues that its worth
|
|
46
45
|
|
|
47
46
|
|
|
48
47
|
These are installed with as many of few of the items that you want like so:-
|
package/debug.js
CHANGED
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
You should have received a copy of the GNU General Public License
|
|
18
18
|
along with Server Utils. If not, see <http://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
import chalk from 'chalk';
|
|
22
|
-
let config = '';
|
|
22
|
+
let config = process.env.DEBUG || '';
|
|
23
23
|
let cache = [];
|
|
24
24
|
let cachelimit = 50;
|
|
25
25
|
|
|
26
26
|
export function Debug (topic) {
|
|
27
27
|
const t = topic;
|
|
28
28
|
let timestamp = new Date().getTime();
|
|
29
|
-
return
|
|
29
|
+
return function (...args) {
|
|
30
30
|
let enabled = false;
|
|
31
31
|
if (config) {
|
|
32
32
|
const topics = config.split(':');
|
|
@@ -40,63 +40,31 @@ export function Debug (topic) {
|
|
|
40
40
|
}, '');
|
|
41
41
|
const output = `${chalk.greenBright(topic)} ${chalk.cyan(message)} ${chalk.whiteBright(`(${gap}ms)`)}`
|
|
42
42
|
if (enabled) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
console.log(output);
|
|
46
|
-
} else {
|
|
47
|
-
try {
|
|
48
|
-
await fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true})
|
|
49
|
-
} catch(err) {
|
|
50
|
-
console.warn('Failed to write following message to log file', output);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
43
|
+
//eslint-disable-next-line no-console
|
|
44
|
+
console.log(output);
|
|
53
45
|
}
|
|
54
46
|
cache.push(output);
|
|
55
47
|
if (cache.length > cachelimit) cache.splice(0,cache.length - cachelimit); //prevent it getting too big
|
|
56
48
|
}
|
|
57
49
|
};
|
|
58
|
-
export
|
|
50
|
+
export function dumpDebugCache() {
|
|
59
51
|
const output = chalk.white.bgBlue('Above are all the debug calls (most recent first) which lead up to, and then followed on from, the error above');
|
|
60
52
|
cache.reverse();
|
|
61
53
|
for(const line of cache) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
console.log(line);
|
|
65
|
-
} else {
|
|
66
|
-
try {
|
|
67
|
-
await fs.appendFile(process.env.LOG_FILE, line + '\n',{flush: true});
|
|
68
|
-
} catch (err) {
|
|
69
|
-
console.warn('Failed to write following message to log file', line);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
54
|
+
//eslint-disable-next-line no-console
|
|
55
|
+
console.log(line);
|
|
72
56
|
}
|
|
73
57
|
cache.reverse();
|
|
74
|
-
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
75
58
|
//eslint-disable-next-line no-console
|
|
76
|
-
|
|
77
|
-
} else {
|
|
78
|
-
try {
|
|
79
|
-
await fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true});
|
|
80
|
-
} catch(err) {
|
|
81
|
-
console.warn('Failed to write following message to log file', output);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
59
|
+
console.log(output);
|
|
84
60
|
};
|
|
85
|
-
export
|
|
61
|
+
export function setDebugConfig(con, limit = 50) {
|
|
86
62
|
cachelimit = limit;
|
|
87
63
|
if (con !== config) {
|
|
88
64
|
config = con;
|
|
89
65
|
const output = `${chalk.greenBright('debug server config')} ${chalk.redBright(`new server config "${config}"`)}`
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
console.log(output);
|
|
93
|
-
} else {
|
|
94
|
-
try {
|
|
95
|
-
await fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true})
|
|
96
|
-
} catch (err) {
|
|
97
|
-
console.warn('Failed to write following message to log file', output);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
66
|
+
//eslint-disable-next-line no-console
|
|
67
|
+
console.log(output);
|
|
100
68
|
}
|
|
101
69
|
};
|
|
102
70
|
|
package/logger.js
CHANGED
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
|
|
21
21
|
import chalk from 'chalk';
|
|
22
22
|
import { isIP } from 'node:net';
|
|
23
|
-
import fs from 'node:fs/promises'
|
|
24
23
|
|
|
25
24
|
const COLOURS = {
|
|
26
25
|
app: chalk.rgb(255, 136, 0).bold, //orange,
|
|
@@ -47,7 +46,7 @@ function cyrb53 (str, seed = 0) {
|
|
|
47
46
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
export default
|
|
49
|
+
export default function logger(ip,level, ...messages) {
|
|
51
50
|
if (process.env.LOG_NONE === undefined) {
|
|
52
51
|
let logLine = '';
|
|
53
52
|
if (typeof process.env.LOG_NO_DATE === 'undefined') logLine += new Date().toISOString() + ': ';
|
|
@@ -63,16 +62,8 @@ export default async function logger(ip,level, ...messages) {
|
|
|
63
62
|
message = messages.join(' ');
|
|
64
63
|
}
|
|
65
64
|
logLine += COLOURS[logcolor](message);
|
|
66
|
-
|
|
67
|
-
//eslint-disable-next-line no-console
|
|
65
|
+
//eslint-disable-next-line no-console
|
|
68
66
|
console.log(logLine.trim());
|
|
69
|
-
} else {
|
|
70
|
-
try {
|
|
71
|
-
await fs.appendFile(process.env.LOG_FILE, logLine.trim() + '\n',{flush: true})
|
|
72
|
-
} catch(err) {
|
|
73
|
-
console.warn('Error writing log file:',err,'message being logged', logLine.trim());
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
67
|
}
|
|
77
68
|
}
|
|
78
69
|
|
package/package.json
CHANGED
package/version.js
CHANGED
|
@@ -26,15 +26,15 @@ import { exec } from 'node:child_process';
|
|
|
26
26
|
const debug = Debug('version');
|
|
27
27
|
|
|
28
28
|
async function shCmd(cmd, root) {
|
|
29
|
-
|
|
29
|
+
debug('About to execute Command ', cmd);
|
|
30
30
|
return new Promise(async (accept, reject) => {
|
|
31
|
-
exec(cmd, { cwd: root },
|
|
31
|
+
exec(cmd, { cwd: root }, (err, stdout, stderr) => {
|
|
32
32
|
if (stderr) {
|
|
33
|
-
|
|
33
|
+
debug('Command ', cmd, 'about to fail with ', err);
|
|
34
34
|
reject(err);
|
|
35
35
|
} else {
|
|
36
36
|
const out = stdout.trim();
|
|
37
|
-
|
|
37
|
+
debug('Command ', cmd, 'Success with ', out);
|
|
38
38
|
accept(out);
|
|
39
39
|
}
|
|
40
40
|
});
|
|
@@ -46,9 +46,9 @@ export default async function(root) {
|
|
|
46
46
|
let vtime;
|
|
47
47
|
|
|
48
48
|
try {
|
|
49
|
-
|
|
49
|
+
debug('Look for git')
|
|
50
50
|
await access(resolve(root, '.git'));
|
|
51
|
-
|
|
51
|
+
debug('Git found, so use it to get data')
|
|
52
52
|
//we get here if there is a git directory, so we can look up version and latest commit from them
|
|
53
53
|
version = await shCmd('git describe --abbrev=0 --tags');
|
|
54
54
|
//git is installed and we found a tag
|
|
@@ -60,7 +60,7 @@ export default async function(root) {
|
|
|
60
60
|
} catch (e) {
|
|
61
61
|
//no git, or no tag, so we must look for a version file
|
|
62
62
|
try {
|
|
63
|
-
|
|
63
|
+
debug('Git approach failed, so look for release info');
|
|
64
64
|
version = await readFile(resolve(root, 'release.info'), 'utf8');
|
|
65
65
|
try {
|
|
66
66
|
const { mtime } = await stat(resolve(root, 'release.info'));
|
|
@@ -88,7 +88,7 @@ export default async function(root) {
|
|
|
88
88
|
} finally {
|
|
89
89
|
const finalversion = version.replace(/\s+/g, ' ').trim(); //trim out new lines and multiple spaces just one.
|
|
90
90
|
const copyrightTime = new Date(vtime);
|
|
91
|
-
|
|
91
|
+
debug('Resolving with Git copyright Year is ', copyrightTime.getUTCFullYear());
|
|
92
92
|
return({ version: finalversion, year: copyrightTime.getUTCFullYear() });
|
|
93
93
|
}
|
|
94
94
|
};
|