Skip to content

Commit

Permalink
[tsp-client] Print example command (#9217)
Browse files Browse the repository at this point in the history
* print example command

* debug level

* account for object options

* print command later in program execution

* prep for release

* fix test

---------

Co-authored-by: Catalina Peralta <[email protected]>
  • Loading branch information
catalinaperalta and cperaltah authored Nov 8, 2024
1 parent 3b44d07 commit 9c36aa6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tools/tsp-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release

## 2024-11-08 - 0.14.1

- Print an example `tsp compile` call when the `--debug` flag is passed to a `tsp-client` command.

## 2024-11-07 - 0.14.0

- Fix `init` command when using a local spec:
Expand Down
4 changes: 2 additions & 2 deletions tools/tsp-client/package-lock.json

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

2 changes: 1 addition & 1 deletion tools/tsp-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-client-generator-cli",
"version": "0.14.0",
"version": "0.14.1",
"description": "A tool to generate Azure SDKs from TypeSpec",
"main": "dist/index.js",
"homepage": "https://github.com/Azure/azure-sdk-tools/tree/main/tools/tsp-client#readme",
Expand Down
9 changes: 8 additions & 1 deletion tools/tsp-client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,21 @@ export async function generateCommand(argv: any) {
args.push("--force");
}
await npmCommand(srcDir, args);
const success = await compileTsp({

const [success, exampleCmd] = await compileTsp({
emitterPackage: emitter,
outputPath: outputDir,
resolvedMainFilePath,
saveInputs: saveInputs,
additionalEmitterOptions: emitterOptions,
});

if (argv["debug"]) {
Logger.warn(`Example of how to compile using the tsp commandline. NOTE: tsp-client does NOT directly run this command, results may vary:
${exampleCmd}
`);
}

if (saveInputs) {
Logger.debug(`Skipping cleanup of temp directory: ${tempRoot}`);
} else {
Expand Down
20 changes: 16 additions & 4 deletions tools/tsp-client/src/typespec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function compileTsp({
resolvedMainFilePath: string;
additionalEmitterOptions?: string;
saveInputs?: boolean;
}): Promise<boolean> {
}): Promise<[boolean, string]> {
const parsedEntrypoint = getDirectoryPath(resolvedMainFilePath);
const { compile, NodeHost, resolveCompilerOptions, formatDiagnostic } =
await importTsp(parsedEntrypoint);
Expand Down Expand Up @@ -103,6 +103,18 @@ export async function compileTsp({
overrides,
});
Logger.debug(`Compiler options: ${JSON.stringify(options)}`);

const cliOptions = Object.entries(options.options?.[emitterPackage] ?? {})
.map(([key, value]) => {
if (typeof value === "object") {
value = JSON.stringify(value);
}
return `--option ${key}=${value}`;
})
.join(" ");

const exampleCmd = `npx tsp compile ${resolvedMainFilePath} --emit ${emitterPackage} ${cliOptions}`;

if (diagnostics.length > 0) {
let errorDiagnostic = false;
// This should not happen, but if it does, we should log it.
Expand All @@ -118,7 +130,7 @@ export async function compileTsp({
}
}
if (errorDiagnostic) {
return false;
return [false, exampleCmd];
}
}

Expand All @@ -138,11 +150,11 @@ export async function compileTsp({
}
}
if (errorDiagnostic) {
return false;
return [false, exampleCmd];
}
}
Logger.success("generation complete");
return true;
return [true, exampleCmd];
}

export async function importTsp(baseDir: string): Promise<typeof import("@typespec/compiler")> {
Expand Down
2 changes: 1 addition & 1 deletion tools/tsp-client/test/typespec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Check diagnostic reporting", function () {
resolvePath(process.cwd(), "test", "examples", "specification", "diagnostics"),
);
try {
const succeeded = await compileTsp({
const [succeeded, _] = await compileTsp({
emitterPackage: "@azure-tools/typespec-ts",
outputPath: joinPaths(process.cwd(), "examples"),
resolvedMainFilePath: mainFile,
Expand Down

0 comments on commit 9c36aa6

Please sign in to comment.