Skip to content

Commit

Permalink
fix: add dependency works in yarn/npm/pnpm workspaces (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Dec 9, 2022
1 parent b44a62c commit ed78721
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions libs/vscode/add-dependency/src/lib/vscode-add-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@ import {
detectPackageManager,
getPackageManagerCommand,
PackageManager,
readJsonFile,
} from '@nrwl/devkit';
import { getGenerators } from '@nx-console/shared/collections';
import { getNxWorkspace } from '@nx-console/vscode/nx-workspace';
import { getGeneratorOptions, selectFlags } from '@nx-console/vscode/tasks';
import {
getShellExecutionForConfig,
getTelemetry,
getWorkspacePath,
} from '@nx-console/vscode/utils';
import { existsSync } from 'fs';
import { join } from 'path';
import { xhr, XHRResponse } from 'request-light';
import {
commands,
ExtensionContext,
QuickInput,
QuickPickItem,
QuickPickItemKind,
ShellExecution,
Task,
tasks,
TaskScope,
window,
} from 'vscode';
import { gte, major, rcompare } from 'semver';
import { resolveDependencyVersioning } from './dependency-versioning';
import { getNxWorkspace } from '@nx-console/vscode/nx-workspace';

export const ADD_DEPENDENCY_COMMAND = 'nxConsole.addDependency';
export const ADD_DEV_DEPENDENCY_COMMAND = 'nxConsole.addDevDependency';
Expand Down Expand Up @@ -66,7 +65,7 @@ function vscodeAddDependencyCommand(installAsDevDependency: boolean) {
if (dep) {
const quickInput = showLoadingQuickInput(dep);
getTelemetry().featureUsed('add-dependency');
addDependency(dep, version, installAsDevDependency);
addDependency(dep, version, installAsDevDependency, workspacePath);
const disposable = tasks.onDidEndTaskProcess((taskEndEvent) => {
if (
taskEndEvent.execution.task.definition.type === 'nxconsole-add-dep'
Expand Down Expand Up @@ -129,12 +128,18 @@ function showLoadingQuickInput(dependency: string): QuickInput {
function addDependency(
dependency: string,
version: string,
installAsDevDependency: boolean
installAsDevDependency: boolean,
workspacePath: string
) {
const pkgManagerCommands = getPackageManagerCommand(pkgManager);
const pkgManagerWorkspaceFlag = getWorkspaceAddFlag(
pkgManager,
workspacePath
);
const command = `${
installAsDevDependency ? pkgManagerCommands.addDev : pkgManagerCommands.add
} ${dependency}@${version}`;
} ${pkgManagerWorkspaceFlag} ${dependency}@${version}`;

const task = new Task(
{
type: 'nxconsole-add-dep',
Expand All @@ -146,6 +151,7 @@ function addDependency(
);
tasks.executeTask(task);
}

async function executeInitGenerator(
dependency: string,
workspacePath: string,
Expand Down Expand Up @@ -241,3 +247,29 @@ function getDependencySuggestions(): Promise<
}
);
}

function getWorkspaceAddFlag(
pkgManager: string,
workspacePath: string
): string {
const pkgJson = readJsonFile<{
workspaces?: string[];
private?: boolean;
}>(join(workspacePath, 'package.json'));
if (pkgManager === 'yarn') {
const isWorkspace =
!!pkgJson.private &&
!!pkgJson.workspaces &&
pkgJson.workspaces?.length > 0;
return isWorkspace ? '-W' : '';
}
if (pkgManager === 'npm') {
const isWorkspace = !!pkgJson.workspaces && pkgJson.workspaces?.length > 0;
return isWorkspace ? '--workspaces false' : '';
}
let pnpmYml = existsSync(join(workspacePath, 'pnpm-workspace.yaml'));
if (!pnpmYml) {
pnpmYml = existsSync(join(workspacePath, 'pnpm-workspace.yml'));
}
return pnpmYml ? '-w' : '';
}

0 comments on commit ed78721

Please sign in to comment.