Skip to content

Commit

Permalink
feat: separate logs in 2 categories: info and trace (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Apr 16, 2024
1 parent ff330d5 commit dfc0a4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
4 changes: 2 additions & 2 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

let args = require('minimist')(process.argv.slice(2));

Check warning on line 5 in bin/build.js

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

const log = require('../src/log');
let flavorBuilder = require('../src/index');

Check warning on line 7 in bin/build.js

View workflow job for this annotation

GitHub Actions / lint

`../src/index` import should occur before import of `../src/log`

let configs = args.config.split(',');
Expand All @@ -14,7 +14,7 @@ for (let i = 0; i < configs.length; i++) {

Promise.all(prom)
.then(() => {
console.log('done build');
log.info('done build');
})
.catch((e) => {
console.error('Error building with flavor-builder', e, e.stack);
Expand Down
50 changes: 25 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const crypto = require('crypto');
const path = require('path');
const urlLib = require('url');

const debug = require('debug')('flavor-builder:main');
const FlavorUtils = require('flavor-utils');
const fs = require('fs-extra');
const _ = require('lodash');
Expand All @@ -17,6 +16,7 @@ const swig = require('swig');
const visualizerOnTabs = require('visualizer-on-tabs');

const utils = require('./utils');
const log = require('./log');

Check warning on line 19 in src/index.js

View workflow job for this annotation

GitHub Actions / lint

`./log` import should occur before import of `./utils`

const URL = urlLib.URL;

Expand Down Expand Up @@ -68,7 +68,7 @@ function call(configArg) {
};

async function build() {
debug('start build');
log.info('start build');
revisionById = checkFile(config.revisionByIdPath);
md5 = checkFile(config.md5Path);

Expand All @@ -95,15 +95,15 @@ function call(configArg) {

try {
sitemaps = readSiteMaps();
debug('get versions');
log.trace('get versions');
if (config.flavor) {
// Build single flavor
let exists = await hasFlavor(config.flavor);
if (!exists) {
debug('Flavor not found');
log.info(`Flavor ${config.flavor} not found`);
return;
}
debug('get flavor');
log.trace('get flavor');
if (config.flavorLayouts[config.flavor] === 'visualizer-on-tabs') {
await handleVisualizerOnTabs(config.flavor);
} else {
Expand All @@ -116,7 +116,7 @@ function call(configArg) {
let flavors = await getFlavors();
// Filter flavors to get only those that have changed
flavors = await filterFlavorsByMd5(flavors);
debug(`Processing ${flavors.length} flavors: ${flavors}`);
log.info(`Processing ${flavors.length} flavors: ${flavors}`);
for (let i = 0; i < flavors.length; i++) {
if (config.flavorLayouts[flavors[i]] === 'visualizer-on-tabs') {
await handleVisualizerOnTabs(flavors[i]);
Expand All @@ -129,12 +129,12 @@ function call(configArg) {
}
writeSiteMaps();
} catch (e) {
debug('error occured', e);
log.info('error occured', e);
}
}

function checkFile(path) {
debug(`check that ${path} can be written`);
log.trace(`check that ${path} can be written`);
try {
let fid = fs.openSync(path, 'a+');
fs.closeSync(fid);
Expand All @@ -155,7 +155,7 @@ function call(configArg) {
}

function readSiteMaps() {
debug('write site maps');
log.trace('write site maps');
try {
let r = {};
let content = fs.readFileSync(
Expand All @@ -175,10 +175,10 @@ function call(configArg) {

function writeSiteMaps() {
if (!config.rootUrl) {
debug('No root url specified, not creating sitemap.txt');
log.info('No root url specified, not creating sitemap.txt');
return;
}
debug('write site maps');
log.trace('write site maps');
fs.writeFileSync(
path.join(config.dir, 'sitemap.txt'),
Object.keys(sitemaps)
Expand Down Expand Up @@ -258,10 +258,10 @@ function call(configArg) {

// returns an array of flavors for which the md5 has changed
function filterFlavorsByMd5(flavors) {
debug('filter flavors by md5');
log.trace('filter flavors by md5');
return getFlavorMD5(flavors).then((result) => {
if (config.forceUpdate) {
debug('force update, no flavor filtering');
log.info('force update, no flavor filtering');
return Object.keys(result);
}
if (JSON.stringify(md5) === '{}') {
Expand All @@ -271,11 +271,11 @@ function call(configArg) {
let keys = [];
for (let key in result) {
if (result[key] !== md5[key]) {
debug(`flavor ${key} has changed, add to the list`);
log.trace(`flavor ${key} has changed, add to the list`);
md5[key] = result[key];
keys.push(key);
} else {
debug(`flavor ${key} has not changed, ignoring it`);
log.trace(`flavor ${key} has not changed, ignoring it`);
}
}
fs.writeJSONSync(config.md5Path, md5);
Expand All @@ -284,7 +284,7 @@ function call(configArg) {
}

function getFlavors() {
debug('get list of flavors');
log.trace('get list of flavors');
return flavorUtils.getFlavors().then((flavors) => {
return processFlavors(flavors);
});
Expand Down Expand Up @@ -370,21 +370,21 @@ function call(configArg) {
// data: the
async function handleFlavor(flavorName) {
let viewsList = await getFlavor(flavorName);
debug(`handle flavor ${flavorName}`);
log.info(`build flavor ${flavorName}`);
const flavorDir = getFlavorDir(flavorName, true);

// Transforms the array-representation of a flavor's views as returned by couchdb
// into a tree representation that reflects the views' hierarchy in a flavor
debug('get tree');
log.trace('get tree');
let viewTree = await flavorUtils.getTree(viewsList);
// Set the path of each view to end up to
debug('do path on tree');
log.trace('do path on tree');
await flavorUtils.traverseTree(viewTree, doPath(flavorDir));
// For each view fix version number
debug('fix version on tree');
log.trace('fix version on tree');
await flavorUtils.traverseTree(viewTree, fixVersion);
// For each view generate the html in the appropriate directory
debug('generate html on tree');
log.trace('generate html on tree');

let hasNew = false;
let nameChanged = false;
Expand Down Expand Up @@ -440,7 +440,7 @@ function call(configArg) {
}

function logProcessView(el, flavorName) {
debug(`process view - flavor: ${flavorName}, id: ${el.__id}`);
log.info(`process view - flavor: ${flavorName}, id: ${el.__id}`);
}

function updateRevision(cb, flavorName) {
Expand Down Expand Up @@ -824,9 +824,9 @@ function call(configArg) {
}

function copyFiles() {
debug('copy files');
log.trace('copy files');
for (let i = 0; i < toCopy.length; i++) {
debug(`copy ${toCopy[i].src} to ${toCopy[i].dest}`);
log.trace(`copy ${toCopy[i].src} to ${toCopy[i].dest}`);
fs.copySync(toCopy[i].src, toCopy[i].dest);
}
}
Expand Down Expand Up @@ -856,7 +856,7 @@ function call(configArg) {
return Promise.resolve();
} catch (e) {
return new Promise((resolve, reject) => {
debug('copying visualizer', version);
log.trace('copying visualizer', version);
fs.mkdirpSync(extractDir);
url = utils.getAuthUrl(config, url.href);

Expand Down
9 changes: 9 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const logInfo = require('debug')('flavor-builder:info');
const logTrace = require('debug')('flavor-builder:trace');

module.exports = {
info: logInfo,
trace: logTrace,
}

0 comments on commit dfc0a4b

Please sign in to comment.