Skip to content

Commit

Permalink
Tweaks cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Mar 8, 2025
1 parent b12b839 commit 50bd19c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
6 changes: 5 additions & 1 deletion packages/compiler/src/core/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export function withCliHostAndDiagnostics<T extends CliHostArgs>(
}

export function createCLICompilerHost(options: CliHostArgs): CliCompilerHost {
const logSink = createConsoleSink({ pretty: options.pretty, pathRelativeTo: process.cwd() });
const logSink = createConsoleSink({
pretty: options.pretty,
pathRelativeTo: process.cwd(),
trackAction: true,
});
const logger = createLogger({ sink: logSink, level: options.debug ? "trace" : "warning" });
return { ...NodeHost, logSink, logger, debug: options.debug ?? false };
}
Expand Down
17 changes: 13 additions & 4 deletions packages/compiler/src/core/logger/console-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import isUnicodeSupported from "is-unicode-supported";
import { relative } from "path/posix";
import pc from "picocolors";
import { Formatter } from "picocolors/types.js";
import { getRelativePathFromDirectory } from "../path-utils.js";
import { LogLevel, LogSink, ProcessedLog, SourceLocation } from "../types.js";
import { supportsHyperlink } from "./support-hyperlinks.js";

Expand All @@ -12,7 +13,10 @@ export interface FormatLogOptions {
excludeLogLevel?: boolean;
}

export interface ConsoleSinkOptions extends FormatLogOptions {}
export interface ConsoleSinkOptions extends FormatLogOptions {
/** @internal */
trackAction?: boolean;
}

export function createConsoleSink(options: ConsoleSinkOptions = {}): LogSink {
function log(data: ProcessedLog) {
Expand All @@ -27,8 +31,13 @@ export function createConsoleSink(options: ConsoleSinkOptions = {}): LogSink {

return {
log,
trackAction: (message, finalMessage, action) =>
trackAction(message, finalMessage, action, options),
getPath: (path) =>
options.pathRelativeTo
? getRelativePathFromDirectory(options.pathRelativeTo, path, false)
: path,
trackAction: options.trackAction
? (message, finalMessage, action) => trackAction(message, finalMessage, action, options)
: undefined,
};
}

Expand Down Expand Up @@ -80,7 +89,7 @@ function formatLevel(options: FormatLogOptions, level: LogLevel) {
function formatSourceLocation(options: FormatLogOptions, location: SourceLocation) {
const postition = getLineAndColumn(location);
const prePath = options.pathRelativeTo
? relative(process.cwd(), location.file.path)
? relative(options.pathRelativeTo, location.file.path)
: location.file.path;

const path = color(options, prePath, pc.cyan);
Expand Down
29 changes: 14 additions & 15 deletions packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pc from "picocolors";
import { EmitterOptions } from "../config/types.js";
import { setCurrentProgram } from "../experimental/typekit/index.js";
import { validateEncodedNamesConflicts } from "../lib/encoded-names.js";
Expand Down Expand Up @@ -32,12 +33,7 @@ import { createDiagnostic } from "./messages.js";
import { createResolver } from "./name-resolver.js";
import { CompilerOptions } from "./options.js";
import { parse, parseStandaloneTypeReference } from "./parser.js";
import {
getDirectoryPath,
getRelativePathFromDirectory,
joinPaths,
resolvePath,
} from "./path-utils.js";
import { getDirectoryPath, joinPaths, resolvePath } from "./path-utils.js";
import {
SourceLoader,
SourceResolution,
Expand All @@ -59,6 +55,7 @@ import {
LibraryMetadata,
LiteralType,
LocationContext,
LogSink,
ModuleLibraryMetadata,
Namespace,
NoTarget,
Expand Down Expand Up @@ -152,10 +149,10 @@ export async function compile(

// Emitter stage
for (const emitter of program.emitters) {
await emit(emitter, program, options);
await emit(emitter, program);

if (options.listFiles) {
logEmittedFilesPath(program.projectRoot);
logEmittedFilesPath(host.logSink);
}
}
return program;
Expand Down Expand Up @@ -891,16 +888,15 @@ function resolveOptions(options: CompilerOptions): CompilerOptions {
return { ...options };
}

async function emit(emitter: EmitterRef, program: Program, options: CompilerOptions = {}) {
async function emit(emitter: EmitterRef, program: Program) {
const emitterName = emitter.metadata.name ?? "";
const relativePathForEmittedFiles = options.listFiles
? `./${getRelativePathFromDirectory(program.projectRoot, emitter.emitterOutputDir, false)}/`
: "";
const relativePathForEmittedFiles =
transformPathForSink(program.host.logSink, emitter.emitterOutputDir) + "/";

const logger = createLogger({ sink: program.host.logSink });
await logger.trackAction(
`Running ${emitterName}...`,
`${emitterName}\t${relativePathForEmittedFiles}`,
`${emitterName} ${pc.dim(relativePathForEmittedFiles)}`,
() => runEmitter(emitter, program),
);
}
Expand All @@ -921,9 +917,12 @@ async function runEmitter(emitter: EmitterRef, program: Program) {
}
}

function logEmittedFilesPath(projectRoot: string) {
function logEmittedFilesPath(logSink: LogSink) {
flushEmittedFilesPaths().forEach((filePath) => {
// eslint-disable-next-line no-console
console.log(`\t./${getRelativePathFromDirectory(projectRoot, filePath, false)}`);
console.log(` ${pc.dim(transformPathForSink(logSink, filePath))}`);
});
}
function transformPathForSink(logSink: LogSink, path: string) {
return logSink.getPath ? logSink.getPath(path) : path;
}
6 changes: 6 additions & 0 deletions packages/compiler/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,10 @@ export interface RelatedSourceLocation {

export interface LogSink {
log(log: ProcessedLog): void;

/** @internal */
getPath?(path: string): string;

/**
* @internal
*/
Expand All @@ -2510,6 +2514,8 @@ export interface Logger {
warn(message: string): void;
error(message: string): void;
log(log: LogInfo): void;

/** @internal */
trackAction<T>(message: string, finalMessage: string, asyncAction: () => Promise<T>): Promise<T>;
}

Expand Down

0 comments on commit 50bd19c

Please sign in to comment.