Skip to content

Commit

Permalink
support check multiple dependency type
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyu3 committed Dec 29, 2024
1 parent 77452a0 commit 7561e24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
37 changes: 33 additions & 4 deletions packages/typespec-vscode/src/npm-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class NpmUtil {
public async calculateNpmPackageDependencyToUpgrade(
packageName: string,
version?: string,
dependencyType: npmDependencyType = npmDependencyType.dependencies,
dependencyTypes: npmDependencyType[] = [npmDependencyType.dependencies],
on?: spawnExecutionEvents,
): Promise<string[]> {
const dependenciesToInstall: string[] = [];
Expand All @@ -78,20 +78,33 @@ export class NpmUtil {
}

/* get dependencies. */
if (dependencyTypes.length === 0) {
logger.info("No dependency to check.");
return dependenciesToInstall;
}

try {
const dependenciesResult = await spawnExecution(
"npm",
["view", packageFullName, dependencyType, "--json"],
["view", packageFullName, ...dependencyTypes, "--json"],
this.cwd,
on,
);

if (dependenciesResult.exitCode === 0) {
const json = JSON.parse(dependenciesResult.stdout);
for (const [key, value] of Object.entries(json)) {
const jsonDependencies: any[] = [];
if (dependencyTypes.length > 1) {
jsonDependencies.push(...Object.values(json));
} else {
jsonDependencies.push(json);
}
const dependencies = parseDependency(jsonDependencies);
for (const [key, value] of Object.entries(dependencies)) {
// if (key === "@types/node") continue; //workaround, for testing, need to remove
const { installed, version: installedVersion } = await this.isPackageInstalled(key);
if (installed && installedVersion) {
if (!this.isValidVersion(installedVersion, value as string)) {
if (!this.isValidVersion(installedVersion, value.join("||"))) {
dependenciesToInstall.push(`${key}@latest`);
}
}
Expand All @@ -106,6 +119,22 @@ export class NpmUtil {
logger.error("Error getting dependencies.", [err]);
}

function parseDependency(jsonDependencies: any[]): Record<string, string[]> {
const dependencies: Record<string, string[]> = {};
for (const dependency of jsonDependencies) {
for (const [key, value] of Object.entries(dependency)) {
if (dependencies[key]) {
dependencies[key].push(value as string);
} else {
dependencies[key] = [value as string];
}
}
}
return dependencies;
}

logger.info("Finishing upgrade check.");

return dependenciesToInstall;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async function doEmit(context: vscode.ExtensionContext, mainTspFile: string, kin
const dependenciesToInstall = await npmUtil.calculateNpmPackageDependencyToUpgrade(
selectedEmitter.package,
version,
npmDependencyType.peerDependencies,
[npmDependencyType.dependencies, npmDependencyType.peerDependencies],
);
logger.info(`${dependenciesToInstall}`);
if (dependenciesToInstall.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getRegisterEmitterTypes(): ReadonlyArray<EmitterKind> {
}

const languageAlias: Record<string, string> = {
".net": "dotnet",
".NET": "dotnet",
};

/*return the alias of the language if it exists, otherwise return the original language. */
Expand Down

0 comments on commit 7561e24

Please sign in to comment.