Skip to content

Commit

Permalink
only pop-up project selection window when there are more than one tsp…
Browse files Browse the repository at this point in the history
… projects
  • Loading branch information
chunyu3 committed Dec 25, 2024
1 parent 3b52923 commit 82b1a42
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
4 changes: 2 additions & 2 deletions packages/typespec-vscode/src/npm-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class NpmUtil {
}

/* identify the action to take for a package. install or skip or cancel or upgrade */
public async ensureNpmPackageInstallAction(
public async calculateNpmPackageInstallAction(
packageName: string,
version?: string,
): Promise<{ action: InstallationAction; version?: string }> {
Expand All @@ -65,7 +65,7 @@ export class NpmUtil {
}

/* identify the dependency packages need to be upgraded */
public async ensureNpmPackageDependencyToUpgrade(
public async calculateNpmPackageDependencyToUpgrade(
packageName: string,
version?: string,
dependencyType: npmDependencyType = npmDependencyType.dependencies,
Expand Down
15 changes: 0 additions & 15 deletions packages/typespec-vscode/src/typespec-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readdir } from "node:fs/promises";
import path from "path";
import vscode from "vscode";
import { StartFileName } from "./const.js";
Expand Down Expand Up @@ -27,20 +26,6 @@ export async function getEntrypointTspFile(tspPath: string): Promise<string | un
return mainTspFile;
}

if (isFilePath && tspPath.endsWith(".tsp")) {
return tspPath;
}

try {
const files = await readdir(baseDir);
if (files && files.length === 1 && files[0].endsWith(".tsp")) {
return path.resolve(baseDir, files[0]);
}
} catch (err) {
logger.error("Error reading directory", [err]);
return undefined;
}

return undefined;
}

Expand Down
63 changes: 33 additions & 30 deletions packages/typespec-vscode/src/vscode-cmd/emit-code/emit-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function doEmit(context: vscode.ExtensionContext, mainTspFile: string, kin

/* install emitter package. */
logger.info(`select ${selectedEmitter.package}`);
const { action, version } = await npmUtil.ensureNpmPackageInstallAction(
const { action, version } = await npmUtil.calculateNpmPackageInstallAction(
selectedEmitter.package,
selectedEmitter.version,
);
Expand Down Expand Up @@ -105,9 +105,9 @@ async function doEmit(context: vscode.ExtensionContext, mainTspFile: string, kin
if (version) {
packageFullName = `${selectedEmitter.package}@${version}`;
}
logger.info(`Installing ${packageFullName}`);
logger.info(`To install ${packageFullName}`);
/* verify dependency packages. */
const dependenciesToInstall = await npmUtil.ensureNpmPackageDependencyToUpgrade(
const dependenciesToInstall = await npmUtil.calculateNpmPackageDependencyToUpgrade(
selectedEmitter.package,
version,
npmDependencyType.peerDependencies,
Expand Down Expand Up @@ -228,43 +228,46 @@ export async function emitCode(context: vscode.ExtensionContext, uri: vscode.Uri
const targetPathes = await TraverseMainTspFileInWorkspace();
logger.info(`Found ${targetPathes.length} ${StartFileName} files`);
if (targetPathes.length === 0) {
logger.info("No main tsp file found. Generating Cancelled.", [], {
logger.info(`No entrypoint file (${StartFileName}) found. Generating Cancelled.`, [], {
showOutput: true,
showPopup: true,
});
return;
}
const toProjectPickItem = (filePath: string): any => {
return {
label: filePath,
path: filePath,
iconPath: {
light: Uri.file(context.asAbsolutePath(`./icons/tsp-file.light.svg`)),
dark: Uri.file(context.asAbsolutePath(`./icons/tsp-file.dark.svg`)),
},
} else if (targetPathes.length === 1) {
tspProjectFile = targetPathes[0];
} else {
const toProjectPickItem = (filePath: string): any => {
return {
label: filePath,
path: filePath,
iconPath: {
light: Uri.file(context.asAbsolutePath(`./icons/tsp-file.light.svg`)),
dark: Uri.file(context.asAbsolutePath(`./icons/tsp-file.dark.svg`)),
},
};
};
};
const typespecProjectQuickPickItems: any[] = targetPathes.map((filePath) =>
toProjectPickItem(filePath),
);
const selectedProjectFile = await vscode.window.showQuickPick(typespecProjectQuickPickItems, {
title: "Select a TypeSpec Project",
canPickMany: false,
placeHolder: "Pick a project",
ignoreFocusOut: true,
});
if (!selectedProjectFile) {
logger.info("No project selected. Generating Cancelled.", [], {
showOutput: true,
showPopup: true,
const typespecProjectQuickPickItems: any[] = targetPathes.map((filePath) =>
toProjectPickItem(filePath),
);
const selectedProjectFile = await vscode.window.showQuickPick(typespecProjectQuickPickItems, {
title: "Select a TypeSpec Project",
canPickMany: false,
placeHolder: "Pick a project",
ignoreFocusOut: true,
});
return;
if (!selectedProjectFile) {
logger.info("No project selected. Generating Cancelled.", [], {
showOutput: true,
showPopup: true,
});
return;
}
tspProjectFile = selectedProjectFile.path;
}
tspProjectFile = selectedProjectFile.path;
} else {
const tspStartFile = await getEntrypointTspFile(uri.fsPath);
if (!tspStartFile) {
logger.info("No main file. Invalid typespec project.", [], {
logger.info(`No entrypoint file (${StartFileName}). Invalid typespec project.`, [], {
showOutput: true,
showPopup: true,
});
Expand Down

0 comments on commit 82b1a42

Please sign in to comment.