Skip to content

Commit

Permalink
feat(nx): detect package manager in workspace-schematic
Browse files Browse the repository at this point in the history
workspace-schematic now tries to detect the right package manager using Angular CLI config and can
identify pnpm from lock file.
  • Loading branch information
mbinic authored and vsavkin committed Jun 15, 2019
1 parent 26903c0 commit 9e15f4a
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/workspace/src/command-line/workspace-schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import {
import * as appRoot from 'app-root-path';
import { execSync } from 'child_process';
import * as fs from 'fs';
import { readFileSync, statSync, writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { copySync, removeSync } from 'fs-extra';
import * as inquirer from 'inquirer';
import * as path from 'path';
import * as yargsParser from 'yargs-parser';
import { fileExists } from '../utils/fileutils';

const rootDirectory = appRoot.path;

Expand Down Expand Up @@ -115,12 +116,29 @@ function createWorkflow(dryRun: boolean) {
const root = normalize(rootDirectory);
const host = new virtualFs.ScopedHost(new NodeJsSyncHost(), root);
return new NodeWorkflow(host, {
packageManager: fileExists('yarn.lock') ? 'yarn' : 'npm',
packageManager: detectPackageManager(),
root,
dryRun
});
}

function detectPackageManager(): string {
try {
const packageManager = execSync(`ng config cli.packageManager`, {
stdio: ['ignore', 'pipe', 'ignore']
})
.toString()
.trim();
return packageManager;
} catch (e) {
return fileExists('yarn.lock')
? 'yarn'
: fileExists('pnpm-lock.yaml')
? 'pnpm'
: 'npm';
}
}

function listSchematics(collectionName: string, logger: logging.Logger) {
try {
const engineHost = new NodeModulesEngineHost();
Expand Down Expand Up @@ -276,11 +294,3 @@ function exists(file: string): boolean {
return false;
}
}

function fileExists(filePath: string): boolean {
try {
return statSync(filePath).isFile();
} catch (err) {
return false;
}
}

0 comments on commit 9e15f4a

Please sign in to comment.