Skip to content

Commit

Permalink
Use debug instead of the console log
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoFrachet committed Dec 29, 2017
1 parent d5f9324 commit 2ad008f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ typings/
.env

package-lock.json
.idea/*
.idea/
3 changes: 2 additions & 1 deletion gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* eslint-disable no-underscore-dangle */

const infoLog = require('debug')('gtfsNodeLib:i');
const fs = require('fs-extra');

const forEachWithLog = require('./helpers/logging_iterator_wrapper');
Expand Down Expand Up @@ -37,7 +38,7 @@ function addItems(items, tableName, gtfs) {
function getIndexedTableOfGtfs(tableName, gtfs, options) {
if (gtfs._tables.has(tableName) === false) {
importTable(gtfs, tableName, options);
console.log(`[Importation] Table ${tableName} has been imported.`);
infoLog(`[Importation] Table ${tableName} has been imported.`);
}

return gtfs._tables.get(tableName);
Expand Down
21 changes: 12 additions & 9 deletions helpers/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

const acomb = require('acomb');
const async = require('async');
const infoLog = require('debug')('gtfsNodeLib:i');
const warningLog = require('debug')('gtfsNodeLib:w');
const errorLog = require('debug')('gtfsNodeLib:e');
const fs = require('fs-extra');

const { fromObjectToCsvString } = require('./csv');
Expand Down Expand Up @@ -42,22 +45,22 @@ function copyUntouchedTable(inputPath, outputPath, tableName, callback) {

fs.open(fullPathToInputFile, 'r', (err) => {
if (err && err.code === 'ENOENT') {
console.log(`[${getHHmmss()}] Table doesn't exist and won't be added: ${tableName}`);
warningLog(`[${getHHmmss()}] Table doesn't exist and won't be added: ${tableName}`);
callback();
return;
}
if (err) {
console.log(err);
errorLog(err);
callback();
return;
}

fs.copy(fullPathToInputFile, fullPathToOutputFile, (copyError) => {
if (copyError) {
console.log(copyError);
errorLog(copyError);
}

console.log(`[${getHHmmss()}] Table has been copied: ${tableName}`);
infoLog(`[${getHHmmss()}] Table has been copied: ${tableName}`);
callback();
});
});
Expand Down Expand Up @@ -138,15 +141,15 @@ function exportTable(tableName, gtfs, outputPath, callback) {
});
}), () => {
if (rowsBuffer.length === 0) {
console.log(`[${getHHmmss()}] Table has been exported: ${tableName}`);
infoLog(`[${getHHmmss()}] Table has been exported: ${tableName}`);
callback();
return;
}

fs.appendFile(outputFullPath, rowsBuffer.join(''), (appendingError) => {
if (appendingError) { throw appendingError; }

console.log(`[${getHHmmss()}] Table has been exported: ${tableName}`);
infoLog(`[${getHHmmss()}] Table has been exported: ${tableName}`);
callback();
});
});
Expand All @@ -171,14 +174,14 @@ exports.exportGtfs = (gtfs, outputPath, callback) => {
return;
}

console.log(`Will start exportation of tables: ${Array.from(gtfs.getTableNames()).join(', ')}`);
infoLog(`Will start exportation of tables: ${Array.from(gtfs.getTableNames()).join(', ')}`);

async.eachSeries(gtfs.getTableNames(), (tableName, done) => {
if (gtfs._tables.has(tableName) === true) {
console.log(`[${getHHmmss()}] Table will be exported: ${tableName}`);
infoLog(`[${getHHmmss()}] Table will be exported: ${tableName}`);
exportTable(tableName, gtfs, outputPath, done);
} else {
console.log(`[${getHHmmss()}] Table will be copied: ${tableName}`);
infoLog(`[${getHHmmss()}] Table will be copied: ${tableName}`);
copyUntouchedTable(gtfs.getPath(), outputPath, tableName, done);
}
}, callback);
Expand Down
3 changes: 2 additions & 1 deletion helpers/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* eslint-disable no-underscore-dangle */

const infoLog = require('debug')('gtfsNodeLib:i');
const fs = require('fs-extra');

const eachWithLog = require('./logging_iterator_wrapper');
Expand All @@ -21,7 +22,7 @@ exports.importTable = (gtfs, tableName, options) => {
return;
}

console.log(`Empty table will be set for table ${tableName} (no input file at path ${gtfs._path}).`);
infoLog(`Empty table will be set for table ${tableName} (no input file at path ${gtfs._path}).`);

gtfs._tables.set(tableName, new Map());
};
Expand Down
6 changes: 4 additions & 2 deletions helpers/logging_iterator_wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const infoLog = require('debug')('gtfsNodeLib:i');

module.exports = (prefix, valueByKey, iteratee) => {
if (
valueByKey instanceof Array === false &&
Expand All @@ -21,7 +23,7 @@ module.exports = (prefix, valueByKey, iteratee) => {

if (Date.now() - lastLogAt > interval && process.env.TEST === undefined) {
const percentageDone = (numberOfKeysDone / valueByKey.size()) * 100;
console.log(`[${prefix}] ${percentageDone.toPrecision(2)}% done`);
infoLog(`[${prefix}] ${percentageDone.toPrecision(2)}% done`);

lastLogAt = Date.now();
oneProgressionLogHasBeenPrinted = true;
Expand All @@ -30,6 +32,6 @@ module.exports = (prefix, valueByKey, iteratee) => {
});

if (oneProgressionLogHasBeenPrinted && process.env.TEST === undefined) {
console.log(`[${prefix}] Done`);
infoLog(`[${prefix}] Done`);
}
};
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

const infoLog = require('debug')('gtfsNodeLib:i');
const warningLog = require('debug')('gtfsNodeLib:w');

const Gtfs = require('./gtfs');

/* Fallback to replace Transit's internal notice system */

if (process.notices === undefined) {
process.notices = {
addInfo: (title, content) => { console.log(`[Info] ${title}:\n${content}`); },
addWarning: (title, content) => { console.log(`[Warning] ${title}:\n${content}`); },
addInfo: (title, content) => { infoLog(`[Info] ${title}:\n${content}`); },
addWarning: (title, content) => { warningLog(`[Warning] ${title}:\n${content}`); },
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"acomb": "1.2.2",
"async": "2.6.0",
"debug": "3.1.0",
"fs-extra": "5.0.0"
},
"jshintConfig": {
Expand Down
6 changes: 2 additions & 4 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,8 @@ describe('Tests on GTFS', () => {
});

const outputPath = `${__dirname}/temp_4865ce67d01f96a489fbd0e71ad8800b/`;
gtfs.exportAtPath(outputPath, (err) => {
if (err) {
console.log(err);
}
gtfs.exportAtPath(outputPath, (exportError) => {
if (exportError) { throw exportError; }

fs.readFile(`${outputPath}routes.txt`, (readRoutesError, routesTxt) => {
if (readRoutesError) { throw readRoutesError; }
Expand Down

0 comments on commit 2ad008f

Please sign in to comment.