1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.ensureCompatibleNpm = exports.getPackageManager = exports.supportsNpm = exports.supportsYarn = void 0;
|
---|
11 | const child_process_1 = require("child_process");
|
---|
12 | const fs_1 = require("fs");
|
---|
13 | const path_1 = require("path");
|
---|
14 | const semver_1 = require("semver");
|
---|
15 | const workspace_schema_1 = require("../lib/config/workspace-schema");
|
---|
16 | const config_1 = require("./config");
|
---|
17 | function supports(name) {
|
---|
18 | try {
|
---|
19 | child_process_1.execSync(`${name} --version`, { stdio: 'ignore' });
|
---|
20 | return true;
|
---|
21 | }
|
---|
22 | catch {
|
---|
23 | return false;
|
---|
24 | }
|
---|
25 | }
|
---|
26 | function supportsYarn() {
|
---|
27 | return supports('yarn');
|
---|
28 | }
|
---|
29 | exports.supportsYarn = supportsYarn;
|
---|
30 | function supportsNpm() {
|
---|
31 | return supports('npm');
|
---|
32 | }
|
---|
33 | exports.supportsNpm = supportsNpm;
|
---|
34 | async function getPackageManager(root) {
|
---|
35 | let packageManager = (await config_1.getConfiguredPackageManager());
|
---|
36 | if (packageManager) {
|
---|
37 | return packageManager;
|
---|
38 | }
|
---|
39 | const hasYarn = supportsYarn();
|
---|
40 | const hasYarnLock = fs_1.existsSync(path_1.join(root, 'yarn.lock'));
|
---|
41 | const hasNpm = supportsNpm();
|
---|
42 | const hasNpmLock = fs_1.existsSync(path_1.join(root, 'package-lock.json'));
|
---|
43 | if (hasYarn && hasYarnLock && !hasNpmLock) {
|
---|
44 | packageManager = workspace_schema_1.PackageManager.Yarn;
|
---|
45 | }
|
---|
46 | else if (hasNpm && hasNpmLock && !hasYarnLock) {
|
---|
47 | packageManager = workspace_schema_1.PackageManager.Npm;
|
---|
48 | }
|
---|
49 | else if (hasYarn && !hasNpm) {
|
---|
50 | packageManager = workspace_schema_1.PackageManager.Yarn;
|
---|
51 | }
|
---|
52 | else if (hasNpm && !hasYarn) {
|
---|
53 | packageManager = workspace_schema_1.PackageManager.Npm;
|
---|
54 | }
|
---|
55 | // TODO: This should eventually inform the user of ambiguous package manager usage.
|
---|
56 | // Potentially with a prompt to choose and optionally set as the default.
|
---|
57 | return packageManager || workspace_schema_1.PackageManager.Npm;
|
---|
58 | }
|
---|
59 | exports.getPackageManager = getPackageManager;
|
---|
60 | /**
|
---|
61 | * Checks if the npm version is a supported 7.x version. If not, display a warning.
|
---|
62 | */
|
---|
63 | async function ensureCompatibleNpm(root) {
|
---|
64 | if ((await getPackageManager(root)) !== workspace_schema_1.PackageManager.Npm) {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | try {
|
---|
68 | const versionText = child_process_1.execSync('npm --version', { encoding: 'utf8', stdio: 'pipe' }).trim();
|
---|
69 | const version = semver_1.valid(versionText);
|
---|
70 | if (!version) {
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | if (semver_1.satisfies(version, '>=7 <7.5.6')) {
|
---|
74 | // eslint-disable-next-line no-console
|
---|
75 | console.warn(`npm version ${version} detected.` +
|
---|
76 | ' When using npm 7 with the Angular CLI, npm version 7.5.6 or higher is recommended.');
|
---|
77 | }
|
---|
78 | }
|
---|
79 | catch {
|
---|
80 | // npm is not installed
|
---|
81 | }
|
---|
82 | }
|
---|
83 | exports.ensureCompatibleNpm = ensureCompatibleNpm;
|
---|