Skip to content

Commit fe995aa

Browse files
authored
Merge pull request #34 from pkgjs/logger-refactor
2 parents cf5dae9 + 178e6a5 commit fe995aa

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

bin/detect-node-support

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ const NodeSupport = require('..');
1313
const internals = {};
1414

1515

16-
internals.log = Debug('detect-node-support');
17-
18-
1916
internals.help = () => {
2017

2118
return `
@@ -34,6 +31,14 @@ Options:
3431

3532
exports.main = async ({ _: [what], deps, deep, dev, json }) => {
3633

34+
const enabledLogs = ['detect-node-support:warn:*', 'detect-node-support:error:*'];
35+
36+
if (process.env.DEBUG) {
37+
enabledLogs.push(process.env.DEBUG);
38+
}
39+
40+
Debug.enable(enabledLogs.join(','));
41+
3742
if (!what) {
3843
console.log(internals.help());
3944
return;

lib/deps.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
'use strict';
22

3-
const Debug = require('debug');
43
const { Arborist } = require('@npmcli/arborist');
54
const Fs = require('fs');
65
const Path = require('path');
76
const Tmp = require('tmp');
87

8+
const Logger = require('./logger');
99
const Package = require('./package');
1010
const Utils = require('./utils');
1111

12-
const internals = {
13-
log: Debug('detect-node-support')
14-
};
12+
const internals = {};
1513

1614

1715
internals.resolve = async ({ packageJson, lockfile }, options) => {
@@ -72,7 +70,12 @@ internals.tryLoad = async (loadFile, filename) => {
7270
exports.detect = async ({ packageJson, loadFile }, options) => {
7371

7472
const lockfile = (await internals.tryLoad(loadFile, 'package-lock.json')) || (await internals.tryLoad(loadFile, 'npm-shrinkwrap.json'));
75-
internals.log(lockfile ? 'Lock file present' : 'Lock file missing - things will be a bit slower');
73+
if (lockfile) {
74+
Logger.log(['deps'], 'Lock file present');
75+
}
76+
else {
77+
Logger.warn(['deps'], 'Lock file missing - things will be a bit slower');
78+
}
7679

7780
const versions = await internals.resolve({ packageJson, lockfile }, options);
7881

@@ -86,7 +89,7 @@ exports.detect = async ({ packageJson, loadFile }, options) => {
8689
for (let i = 0; i < n; ++i) {
8790

8891
const packageName = packages[i];
89-
internals.log(`Resolving dependency ${i + 1} of ${n}: ${packageName}`);
92+
Logger.log(['deps'], `Resolving dependency ${i + 1} of ${n}: ${packageName}`);
9093

9194
try {
9295
const { result } = await Package.detect({ packageName });

lib/loader/repository.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict';
22

3-
const Debug = require('debug');
43
const GitUrlParse = require('git-url-parse');
54
const Wreck = require('@hapi/wreck');
65

6+
const Logger = require('../logger');
77
const Utils = require('../utils');
88

99

1010
const internals = {
11-
cache: new Map(),
12-
log: Debug('detect-node-support:loader'),
13-
error: Debug('detect-node-support:error')
11+
cache: new Map()
1412
};
1513

1614

@@ -39,10 +37,10 @@ exports.create = (repository) => {
3937
}
4038

4139
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
42-
internals.log('Loading: %s', url);
40+
Logger.log(['loader'], 'Loading: %s', url);
4341

4442
if (options === undefined && internals.cache.has(url)) {
45-
internals.log('From cache: %s', url);
43+
Logger.log(['loader'], 'From cache: %s', url);
4644
return internals.cache.get(url);
4745
}
4846

@@ -53,19 +51,19 @@ exports.create = (repository) => {
5351
internals.cache.set(url, payload);
5452
}
5553

56-
internals.log('Loaded: %s', url);
54+
Logger.log(['loader'], 'Loaded: %s', url);
5755
return payload;
5856
}
5957
catch (err) {
6058

6159
if (err.data && err.data.res.statusCode === 404) {
62-
internals.log('Not found: %s', url);
60+
Logger.log(['loader'], 'Not found: %s', url);
6361
const error = new Error(`${repository} does not contain a ${filename}`);
6462
error.code = 'ENOENT';
6563
throw error;
6664
}
6765

68-
internals.error('Failed to load: %s', url);
66+
Logger.error(['loader'], 'Failed to load: %s', url);
6967
throw err;
7068
}
7169
}

lib/logger.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const Debug = require('debug');
4+
5+
6+
const internals = {
7+
loggers: {}
8+
};
9+
10+
11+
internals.getLogger = (tags) => {
12+
13+
const suffix = tags.join(':');
14+
15+
if (!internals.loggers[suffix]) {
16+
internals.loggers[suffix] = Debug(`detect-node-support:${suffix}`);
17+
}
18+
19+
return internals.loggers[suffix];
20+
};
21+
22+
23+
exports.log = (tags, ...args) => {
24+
25+
const logger = internals.getLogger(tags);
26+
logger(...args);
27+
};
28+
29+
30+
exports.warn = (tags, ...args) => {
31+
32+
exports.log(['warn', ...tags], ...args);
33+
};
34+
35+
36+
exports.error = (tags, ...args) => {
37+
38+
exports.log(['error', ...tags], ...args);
39+
};

0 commit comments

Comments
 (0)