Created
February 10, 2019 05:06
-
-
Save azu/bfa5ab3c1f68764345123af0f383ab75 to your computer and use it in GitHub Desktop.
debug: replace process.cwd() with <cwd>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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