Skip to content

Commit d2c412e

Browse files
[dev-tool] Removed repo checkout directory assumption (Azure#12681)
* [dev-tool] Removed repo checkout directory assumption. * Improved the logic even more. Co-authored-by: Will Temple <witemple@microsoft.com>
1 parent 5a4222e commit d2c412e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

common/tools/dev-tool/src/util/printer.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ const printModes = ["info", "warn", "error", "success", "debug"] as const;
1111
export type Fn<T = void> = (...values: any[]) => T;
1212
export type ModeMap<T> = { [k in typeof printModes[number]]: T };
1313

14+
// Compute the base directory of the dev-tool command package
15+
// We must do this specially for the printer module because using
16+
// the package resolution function from the utils/resolveProject
17+
// module would create a difficult circular dependency.
18+
const DEV_TOOL_PATH = (() => {
19+
const directoryFragment = path.sep + "dev-tool" + path.sep;
20+
const parts = __dirname.split(directoryFragment);
21+
// There might be "/dev-tool/" somewhere higher in the path, so
22+
// we will slice the end off of this array and re-join with "/dev-tool/"
23+
// to get the full directory part
24+
const baseDirectoryParts = parts.slice(0, -1);
25+
// Adding path.sep at the end makes the debug output a little cleaner by removing a leading "/"
26+
return path.resolve(baseDirectoryParts.join(directoryFragment) + directoryFragment) + path.sep;
27+
})();
28+
1429
/**
1530
* The interface that describes the Printer produced by {@link createPrinter}
1631
*/
@@ -99,11 +114,9 @@ const finalLogger: ModeMap<Fn> = {
99114
debug(...values: string[]) {
100115
if (process.env.DEBUG) {
101116
const caller = getCaller();
102-
const fileName = caller
103-
?.getFileName()
104-
?.split(path.join("azure-sdk-for-js", "common", "tools", "dev-tool"));
117+
const fileName = caller?.getFileName()?.split(DEV_TOOL_PATH)?.[1];
105118
const callerInfo = `(@ ${fileName ? fileName : "<unknown>"}#${caller?.getFunctionName() ??
106-
"<unknown>"}:${caller?.getLineNumber()}:${caller?.getColumnNumber()})`;
119+
"<anonymous>"}:${caller?.getLineNumber()}:${caller?.getColumnNumber()})`;
107120
backend.error(values[0], colors.debug(callerInfo), ...values.slice(1));
108121
}
109122
},

0 commit comments

Comments
 (0)