Skip to content

Commit

Permalink
feat(common-cli): add support for inferred tasks when adding the plug…
Browse files Browse the repository at this point in the history
…in to the workspace
  • Loading branch information
tinesoft committed Dec 31, 2024
1 parent 237e7b4 commit 788d8be
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/common-cli/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NxJsonConfiguration, PackageManager } from '@nx/devkit';
import { type NxJsonConfiguration, type PackageManager } from '@nx/devkit';
import { execSync, ExecSyncOptions } from 'child_process';
import {
ensureDirSync,
Expand Down Expand Up @@ -45,13 +45,22 @@ export function createWorkspaceWithNxWrapper(

const nxJsonPath = resolve(directory, 'nx.json');
const nxJson: NxJsonConfiguration = readJSONSync(nxJsonPath);
nxJson.installation = nxJson.installation ?? {
version: 'latest',
plugins: {},
};
nxJson.installation.plugins = nxJson.installation.plugins ?? {};
nxJson.installation.plugins[pkgName] = presetVersion;
writeJsonSync(nxJsonPath, nxJson, { spaces: 2 });

if (isNxCrystalEnabled(nxJson)) {
runNxWrapperSync(`add ${pkgName}@${presetVersion}`, {
cwd: directory,
stdio: 'inherit',
env: process.env,
});
} else {
nxJson.installation = nxJson.installation ?? {
version: 'latest',
plugins: {},
};
nxJson.installation.plugins = nxJson.installation.plugins ?? {};
nxJson.installation.plugins[pkgName] = presetVersion;
writeJsonSync(nxJsonPath, nxJson, { spaces: 2 });
}

runNxWrapperSync(`g ${pkgName}:preset ${extraArgs}`, {
cwd: directory,
Expand Down Expand Up @@ -96,11 +105,10 @@ export function getNxCommand(
) {
if (!useNxWrapper) {
switch (pkgManager) {
case 'yarn':
case 'pnpm':
return `$(pkgManager} nx`;
default:
case 'npm':
return 'npx nx';
default:
return `$(pkgManager} nx`;
}
}
return process.platform === 'win32' ? '.\\nx.bat' : './nx';
Expand All @@ -115,6 +123,13 @@ export function isNxWrapperInstalled(cwd: string) {
);
}

export function isNxCrystalEnabled(nxJson: NxJsonConfiguration) {
return (
process.env['NX_ADD_PLUGINS'] !== 'false' &&
nxJson.useInferencePlugins !== false
);
}

// Copied from https://github.com/nrwl/nx/blob/master/packages/nx/src/utils/workspace-root.ts
// Mainly because workspaceRootInner is not exported by @nx/devkit
function workspaceRootInner(
Expand Down

0 comments on commit 788d8be

Please sign in to comment.