fix(nx): make package manager detection a bit smarter

In @nrwl/tao this covers the cases where no lock files exist and in create-nx-workspace we add
support for detecting pnpm installation
This commit is contained in:
Jo Pearce 2019-09-10 09:11:09 +01:00 committed by Victor Savkin
parent 68498ab08e
commit a1df1e040a
2 changed files with 62 additions and 12 deletions

View File

@ -100,27 +100,48 @@ function showHelp() {
}
function determinePackageManager() {
let packageManager = getPackageManagerFromAngularCLI();
if (packageManager === 'npm' || isPackageManagerInstalled(packageManager)) {
return packageManager;
}
if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}
if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}
return 'npm';
}
function getPackageManagerFromAngularCLI(): string {
// If you have Angular CLI installed, read Angular CLI config.
// If it isn't not installed, default to 'yarn'.
let packageManager: string;
// If it isn't installed, default to 'yarn'.
try {
packageManager = execSync('ng config -g cli.packageManager', {
return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500
})
.toString()
.trim();
} catch (e) {
packageManager = 'yarn';
return 'yarn';
}
}
function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore']
});
isInstalled = true;
} catch (e) {
packageManager = 'npm';
/* do nothing */
}
return packageManager;
return isInstalled;
}
function determineWorkspaceName(parsedArgs: any) {

View File

@ -32,6 +32,7 @@ import { logger } from '../shared/logger';
import { commandName, printHelp } from '../shared/print-help';
import * as fs from 'fs';
import minimist = require('minimist');
import { execSync } from 'child_process';
interface GenerateOptions {
collectionName: string;
@ -161,20 +162,48 @@ async function detectPackageManager(
}
}
const yarnLockFileExists = await host.exists('yarn.lock' as any).toPromise();
if (yarnLockFileExists) {
if (await fileExists(host, 'yarn.lock')) {
return 'yarn';
}
const pnpmLockFileExists = await host
.exists('pnpm-lock.yaml' as any)
.toPromise();
if (pnpmLockFileExists) {
if (await fileExists(host, 'pnpm-lock.yaml')) {
return 'pnpm';
}
if (await fileExists(host, 'package-lock.json')) {
return 'npm';
}
// If we get here, there are no lock files, so lets check for package managers in our preferred order
if (isPackageManagerInstalled('yarn')) {
return 'yarn';
}
if (isPackageManagerInstalled('pnpm')) {
return 'pnpm';
}
return 'npm';
}
function fileExists(
host: virtualFs.Host<any>,
fileName: string
): Promise<boolean> {
return host.exists(fileName as any).toPromise();
}
function isPackageManagerInstalled(packageManager: string) {
try {
execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore']
});
return true;
} catch (e) {
return false;
}
}
async function createWorkflow(
fsHost: virtualFs.Host<fs.Stats>,
root: string,