Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@angular/cli): autodetect default package manager #12753

Merged
merged 1 commit into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/angular/cli/commands/add-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NodePackageDoesNotSupportSchematics } from '@angular-devkit/schematics/
import { Arguments } from '../models/interface';
import { SchematicCommand } from '../models/schematic-command';
import { NpmInstall } from '../tasks/npm-install';
import { getPackageManager } from '../utilities/config';
import { getPackageManager } from '../utilities/package-manager';
import { Schema as AddCommandSchema } from './add';

export class AddCommand extends SchematicCommand<AddCommandSchema> {
Expand All @@ -28,7 +28,7 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
return 1;
}

const packageManager = getPackageManager();
const packageManager = getPackageManager(this.workspace.root);

const npmInstall: NpmInstall = require('../tasks/npm-install').default;

Expand Down
4 changes: 2 additions & 2 deletions packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ import * as inquirer from 'inquirer';
import * as systemPath from 'path';
import { WorkspaceLoader } from '../models/workspace-loader';
import {
getPackageManager,
getProjectByCwd,
getSchematicDefaults,
getWorkspace,
getWorkspaceRaw,
} from '../utilities/config';
import { parseJsonSchemaToOptions } from '../utilities/json-schema';
import { getPackageManager } from '../utilities/package-manager';
import { BaseCommandOptions, Command } from './command';
import { Arguments, CommandContext, CommandDescription, Option } from './interface';
import { parseArguments, parseFreeFormArguments } from './parser';
Expand Down Expand Up @@ -255,7 +255,7 @@ export abstract class SchematicCommand<
{
force,
dryRun,
packageManager: getPackageManager(),
packageManager: getPackageManager(this.workspace.root),
root: normalize(this.workspace.root),
},
);
Expand Down
53 changes: 0 additions & 53 deletions packages/angular/cli/utilities/check-package-manager.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/angular/cli/utilities/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function getProjectByCwd(workspace: experimental.workspace.Workspace): st
}
}

export function getPackageManager(): string {
export function getConfiguredPackageManager(): string | null {
let workspace = getWorkspace('local');

if (workspace) {
Expand Down Expand Up @@ -186,7 +186,7 @@ export function getPackageManager(): string {
}
}

return 'npm';
return null;
}

export function migrateLegacyGlobalConfig(): boolean {
Expand Down
58 changes: 58 additions & 0 deletions packages/angular/cli/utilities/package-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';
import { getConfiguredPackageManager } from './config';


export function supportsYarn(): boolean {
try {
execSync('yarn --version');

return true;
} catch {
return false;
}
}

export function supportsNpm(): boolean {
try {
execSync('npm --version');

return true;
} catch {
return false;
}
}

export function getPackageManager(root: string): string {
let packageManager = getConfiguredPackageManager();
if (packageManager) {
return packageManager;
}

const hasYarn = supportsYarn();
const hasYarnLock = existsSync(join(root, 'yarn.lock'));
const hasNpm = supportsNpm();
const hasNpmLock = existsSync(join(root, 'package-lock.json'));

if (hasYarn && hasYarnLock && !hasNpmLock) {
packageManager = 'yarn';
} else if (hasNpm && hasNpmLock && !hasYarnLock) {
packageManager = 'npm';
} else if (hasYarn && !hasNpm) {
packageManager = 'yarn';
} else if (hasNpm && !hasYarn) {
packageManager = 'npm';
}

// TODO: This should eventually inform the user of ambiguous package manager usage.
// Potentially with a prompt to choose and optionally set as the default.
return packageManager || 'npm';
}