Skip to content

Instantly share code, notes, and snippets.

@azu
Created February 10, 2019 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azu/bfa5ab3c1f68764345123af0f383ab75 to your computer and use it in GitHub Desktop.
Save azu/bfa5ab3c1f68764345123af0f383ab75 to your computer and use it in GitHub Desktop.
debug: replace process.cwd() with <cwd>
const cwd = process.cwd();
const replaceCwd = target => {
if (typeof target === "string") {
return target.split(cwd).join("<cwd>");
}
return target;
};
const walkJSON = function walkJSON(data, callback) {
Object.keys(data).forEach(key => {
const item = data[key];
if (typeof item === "string") {
data[key] = callback(item);
} else if (item !== null && typeof item === "object") {
walkJSON(item, callback);
}
});
return data;
};
/**
* Replace current dir to <cwd>
* @param args
*/
const filterArgs = args => {
return args.map(arg => {
if (typeof arg === "string") {
return replaceCwd(arg);
} else if (typeof arg === "object" && arg !== null) {
return walkJSON(arg, replaceCwd);
}
});
};
const debug = require("debug");
debug.log = (...args) => {
// eslint-disable-next-line no-console
console.log(...filterArgs(args));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment