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() { 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 you have Angular CLI installed, read Angular CLI config.
// If it isn't not installed, default to 'yarn'. // If it isn't installed, default to 'yarn'.
let packageManager: string;
try { try {
packageManager = execSync('ng config -g cli.packageManager', { return execSync('ng config -g cli.packageManager', {
stdio: ['ignore', 'pipe', 'ignore'], stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500 timeout: 500
}) })
.toString() .toString()
.trim(); .trim();
} catch (e) { } catch (e) {
packageManager = 'yarn'; return 'yarn';
} }
}
function isPackageManagerInstalled(packageManager: string) {
let isInstalled = false;
try { try {
execSync(`${packageManager} --version`, { execSync(`${packageManager} --version`, {
stdio: ['ignore', 'ignore', 'ignore'] stdio: ['ignore', 'ignore', 'ignore']
}); });
isInstalled = true;
} catch (e) { } catch (e) {
packageManager = 'npm'; /* do nothing */
} }
return packageManager; return isInstalled;
} }
function determineWorkspaceName(parsedArgs: any) { function determineWorkspaceName(parsedArgs: any) {

View File

@ -32,6 +32,7 @@ import { logger } from '../shared/logger';
import { commandName, printHelp } from '../shared/print-help'; import { commandName, printHelp } from '../shared/print-help';
import * as fs from 'fs'; import * as fs from 'fs';
import minimist = require('minimist'); import minimist = require('minimist');
import { execSync } from 'child_process';
interface GenerateOptions { interface GenerateOptions {
collectionName: string; collectionName: string;
@ -161,20 +162,48 @@ async function detectPackageManager(
} }
} }
const yarnLockFileExists = await host.exists('yarn.lock' as any).toPromise(); if (await fileExists(host, 'yarn.lock')) {
if (yarnLockFileExists) {
return 'yarn'; return 'yarn';
} }
const pnpmLockFileExists = await host
.exists('pnpm-lock.yaml' as any) if (await fileExists(host, 'pnpm-lock.yaml')) {
.toPromise(); return 'pnpm';
if (pnpmLockFileExists) { }
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 'pnpm';
} }
return 'npm'; 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( async function createWorkflow(
fsHost: virtualFs.Host<fs.Stats>, fsHost: virtualFs.Host<fs.Stats>,
root: string, root: string,