Skip to content

Refactor: extract logger #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions bin/detect-node-support
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const NodeSupport = require('..');
const internals = {};


internals.log = Debug('detect-node-support');


internals.help = () => {

return `
Expand All @@ -34,6 +31,14 @@ Options:

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

const enabledLogs = ['detect-node-support:warn:*', 'detect-node-support:error:*'];

if (process.env.DEBUG) {
enabledLogs.push(process.env.DEBUG);
}

Debug.enable(enabledLogs.join(','));

if (!what) {
console.log(internals.help());
return;
Expand Down
15 changes: 9 additions & 6 deletions lib/deps.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

const Debug = require('debug');
const { Arborist } = require('@npmcli/arborist');
const Fs = require('fs');
const Path = require('path');
const Tmp = require('tmp');

const Logger = require('./logger');
const Package = require('./package');
const Utils = require('./utils');

const internals = {
log: Debug('detect-node-support')
};
const internals = {};


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

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

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

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

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

try {
const { result } = await Package.detect({ packageName });
Expand Down
16 changes: 7 additions & 9 deletions lib/loader/repository.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict';

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

const Logger = require('../logger');
const Utils = require('../utils');


const internals = {
cache: new Map(),
log: Debug('detect-node-support:loader'),
error: Debug('detect-node-support:error')
cache: new Map()
};


Expand Down Expand Up @@ -39,10 +37,10 @@ exports.create = (repository) => {
}

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

if (options === undefined && internals.cache.has(url)) {
internals.log('From cache: %s', url);
Logger.log(['loader'], 'From cache: %s', url);
return internals.cache.get(url);
}

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

internals.log('Loaded: %s', url);
Logger.log(['loader'], 'Loaded: %s', url);
return payload;
}
catch (err) {

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

internals.error('Failed to load: %s', url);
Logger.error(['loader'], 'Failed to load: %s', url);
throw err;
}
}
Expand Down
39 changes: 39 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const Debug = require('debug');


const internals = {
loggers: {}
};


internals.getLogger = (tags) => {

const suffix = tags.join(':');

if (!internals.loggers[suffix]) {
internals.loggers[suffix] = Debug(`detect-node-support:${suffix}`);
}

return internals.loggers[suffix];
};


exports.log = (tags, ...args) => {

const logger = internals.getLogger(tags);
logger(...args);
};


exports.warn = (tags, ...args) => {

exports.log(['warn', ...tags], ...args);
};


exports.error = (tags, ...args) => {

exports.log(['error', ...tags], ...args);
};