2020-10-30 09:25:23 +02:00

62 lines
1.6 KiB
TypeScript

import * as path from 'path';
import { execSync } from 'child_process';
import { output } from '@nrwl/workspace/src/utils/output';
export function showNxWarning(workspaceName: string) {
try {
const pathToRunNxCommand = path.resolve(process.cwd(), workspaceName);
execSync('nx --version', {
cwd: pathToRunNxCommand,
stdio: ['ignore', 'ignore', 'ignore'],
});
} catch (e) {
// no nx found
output.addVerticalSeparator();
output.note({
title: `Nx CLI is not installed globally.`,
bodyLines: [
`This means that you might have to use "yarn nx" or "npm nx" to execute commands in the workspace.`,
`Run "yarn global add nx" or "npm install -g nx" to be able to execute command directly.`,
],
});
}
}
export function determinePackageManager(preferredPackageManager?: string) {
return (
[
preferredPackageManager,
getPackageManagerFromAngularCLI(),
'yarn',
'pnpm',
].find((pm) => pm && isPackageManagerInstalled(pm)) || 'npm'
);
}
function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
try {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500,
})
.toString()
.trim();
} catch (e) {
return;
}
}
function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore'],
});
isInstalled = true;
} catch (e) {
/* do nothing */
}
return isInstalled;
}