UNPKG

1.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * @license
5 * Copyright Google Inc. All Rights Reserved.
6 *
7 * Use of this source code is governed by an MIT-style license that can be
8 * found in the LICENSE file at https://angular.io/license
9 */
10const child_process_1 = require("child_process");
11const fs_1 = require("fs");
12const path_1 = require("path");
13const schema_1 = require("../lib/config/schema");
14const config_1 = require("./config");
15function supports(name) {
16 try {
17 child_process_1.execSync(`${name} --version`, { stdio: 'ignore' });
18 return true;
19 }
20 catch (_a) {
21 return false;
22 }
23}
24function supportsYarn() {
25 return supports('yarn');
26}
27exports.supportsYarn = supportsYarn;
28function supportsNpm() {
29 return supports('npm');
30}
31exports.supportsNpm = supportsNpm;
32async function getPackageManager(root) {
33 let packageManager = await config_1.getConfiguredPackageManager();
34 if (packageManager) {
35 return packageManager;
36 }
37 const hasYarn = supportsYarn();
38 const hasYarnLock = fs_1.existsSync(path_1.join(root, 'yarn.lock'));
39 const hasNpm = supportsNpm();
40 const hasNpmLock = fs_1.existsSync(path_1.join(root, 'package-lock.json'));
41 if (hasYarn && hasYarnLock && !hasNpmLock) {
42 packageManager = schema_1.PackageManager.Yarn;
43 }
44 else if (hasNpm && hasNpmLock && !hasYarnLock) {
45 packageManager = schema_1.PackageManager.Npm;
46 }
47 else if (hasYarn && !hasNpm) {
48 packageManager = schema_1.PackageManager.Yarn;
49 }
50 else if (hasNpm && !hasYarn) {
51 packageManager = schema_1.PackageManager.Npm;
52 }
53 // TODO: This should eventually inform the user of ambiguous package manager usage.
54 // Potentially with a prompt to choose and optionally set as the default.
55 return packageManager || schema_1.PackageManager.Npm;
56}
57exports.getPackageManager = getPackageManager;