Skip to content

Commit

Permalink
Use loglevel to suppress debug log (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
kota65535 authored Mar 2, 2023
1 parent d0fe027 commit a4ffe2d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 15 deletions.
12 changes: 8 additions & 4 deletions bin/openapi-merger.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

const program = require("commander");
const main = require("../src/main");
const log = require("loglevel");

log.setLevel("info");

function validate(val, pattern, message) {
if (val.match(pattern)) {
return val;
}
console.error("error: " + message);
log.error("error: " + message);
process.exit(1);
}

Expand All @@ -32,17 +35,18 @@ program
debug: args.debug,
};
if (params.debug) {
console.debug("params: ", params);
log.setLevel("debug");
log.debug("params: ", params);
}

try {
await main(params);
} catch (e) {
// show error message
if (params.debug) {
console.error(e);
log.error(e);
} else {
console.error("Error :" + e.message);
log.error("Error :" + e.message);
}
process.exit(1);
}
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"glob": "8.1.0",
"js-yaml": "4.1.0",
"lodash": "4.17.21",
"loglevel": "1.8.1",
"mktemp": "1.0.0",
"node-fetch": "2.6.9"
},
Expand Down
3 changes: 2 additions & 1 deletion src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Path = require("path");
const { readYAML } = require("./yaml");
const { download } = require("./http");
const { sliceObject, parseUrl } = require("./util");
const log = require("loglevel");

class Component {
constructor(type, name, content, url) {
Expand Down Expand Up @@ -106,7 +107,7 @@ class ComponentNameResolver {
for (let i = 0; i < cmps.length; i++) {
const resolved = `${name}${i + 1}`;
cToName[cmps[i].url] = resolved;
console.warn(`conflicted component name "${name}" resolved to "${resolved}". url=${cmps[i].url}`);
log.warn(`conflicted component name "${name}" resolved to "${resolved}". url=${cmps[i].url}`);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fetch = require("node-fetch");
const _ = require("lodash");
const { loadYAML } = require("./yaml");
const log = require("loglevel");

const cache = {};

Expand All @@ -16,16 +17,16 @@ async function download(url) {
return _.cloneDeep(cache[url]);
}

console.info(`fetching: ${url}`);
log.info(`fetching: ${url}`);
let res;
try {
res = await fetch(url);
} catch (e) {
console.error(`Failed to fetch: ${url}`);
log.error(`Failed to fetch: ${url}`);
return {};
}
if (!res.ok) {
console.error(`${res.status} returned: ${url}`);
log.error(`${res.status} returned: ${url}`);
return {};
}

Expand All @@ -36,7 +37,7 @@ async function download(url) {
} else if (url.match(/\.json$/)) {
doc = JSON.parse(body);
} else {
console.warn(`Cannot determine the file type: ${url}`);
log.warn(`Cannot determine the file type: ${url}`);
// assume YAML for now
doc = loadYAML(body);
}
Expand Down
7 changes: 4 additions & 3 deletions src/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
IncludedArray,
} = require("./util");
const { ComponentManager, ComponentNameResolver } = require("./components");
const log = require("loglevel");

class Merger {
static INCLUDE_PATTERN = /^\$include(#\w+?)?(\.\w+?)?$/;
Expand Down Expand Up @@ -98,7 +99,7 @@ class Merger {
* @param jsonPath a JSON path for accessing the target object
*/
handleRef = async (obj, key, val, file, jsonPath) => {
console.debug(`ref : ${jsonPath} file=${Path.relative(this.baseDir, file)}`);
log.debug(`ref : ${jsonPath} file=${Path.relative(this.baseDir, file)}`);

obj[key] = mergeOrOverwrite(obj[key], val);

Expand Down Expand Up @@ -161,7 +162,7 @@ class Merger {
* @returns {Promise<*>} a result object or array
*/
handleInclude = async (obj, key, val, file, jsonPath) => {
console.debug(`include: ${jsonPath} file=${Path.relative(this.baseDir, file)}`);
log.debug(`include: ${jsonPath} file=${Path.relative(this.baseDir, file)}`);

obj[key] = mergeOrOverwrite(obj[key], val);

Expand Down Expand Up @@ -240,7 +241,7 @@ function processInclude(key, obj, config) {
}
const clazzConfig = config.include[clazz];
if (!clazzConfig) {
console.warn(`$include classname '${clazz} specified, but no configuration found.`);
log.warn(`$include classname '${clazz} specified, but no configuration found.`);
return obj;
}
obj = filterObject(obj, clazzConfig.filter);
Expand Down
5 changes: 3 additions & 2 deletions src/ref.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const log = require("loglevel");

// cf. https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json
// https://github.com/OAI/OpenAPI-Specification/tree/master/versions

const FIELD_PATTERN = "([a-zA-Z0-9\\-_]|[^\x01-\x7E\uFF61-\uFF9F])+";
const MAX_JSON_PATH_DEPTH = 100;

Expand Down Expand Up @@ -69,7 +70,7 @@ function getRefType(path) {
return k;
}
}
console.warn(`could not infer $ref type at "${path}". fallback to include.`);
log.warn(`could not infer $ref type at "${path}". fallback to include.`);
if (path.split(".").length > MAX_JSON_PATH_DEPTH) {
throw new Error(`JSON path depth exceeds ${MAX_JSON_PATH_DEPTH}, aborting...`);
}
Expand Down
3 changes: 2 additions & 1 deletion src/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require("path");
const fs = require("fs");
const yaml = require("js-yaml");
const log = require("loglevel");

function loadYAML(str) {
return yaml.load(str);
Expand All @@ -22,7 +23,7 @@ function writeYAML(doc, filePath) {
}
fs.writeFileSync(filePath, dump);
} else {
console.log(dump);
log.info(dump);
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/test_merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const fs = require("fs-extra");
const path = require("path");
const glob = require("glob");
const main = require("../src/main");
const log = require("loglevel");
const assert = require("chai").assert;

log.setLevel("debug");

const runMerger = async (name) => {
const configFile = path.join("resources", name, "config.yaml");
const params = {
Expand Down

0 comments on commit a4ffe2d

Please sign in to comment.