forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.ts
More file actions
155 lines (138 loc) · 5.14 KB
/
factory.ts
File metadata and controls
155 lines (138 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import { major } from 'semver';
import { discover } from './discovery';
import { Host, createNodeJsHost } from './host';
import { Logger } from './logger';
import { PackageManager } from './package-manager';
import { PackageManagerName, SUPPORTED_PACKAGE_MANAGERS } from './package-manager-descriptor';
/**
* The default package manager to use when none is discovered or configured.
*/
const DEFAULT_PACKAGE_MANAGER: PackageManagerName = 'npm';
/**
* Gets the version of yarn installed on the system.
* @param host A `Host` instance for running commands.
* @param cwd The absolute path to the working directory.
* @param logger An optional logger instance.
* @returns A promise that resolves to the yarn version string, or null if yarn is not installed.
*/
async function getYarnVersion(host: Host, cwd: string, logger?: Logger): Promise<string | null> {
logger?.debug(`Getting yarn version...`);
try {
const { stdout } = await host.runCommand('yarn', ['--version'], { cwd });
const version = stdout.trim();
logger?.debug(`Yarn version is '${version}'.`);
return version;
} catch (e) {
logger?.debug('Failed to get yarn version.');
return null;
}
}
/**
* Determines the package manager to use for a given project.
*
* This function will determine the package manager by checking for a configured
* package manager, discovering the package manager from lockfiles, or falling
* back to a default. It also handles differentiation between yarn classic and modern.
*
* @param host A `Host` instance for interacting with the file system and running commands.
* @param cwd The directory to start the search from.
* @param configured An optional, explicitly configured package manager.
* @param logger An optional logger instance.
* @returns A promise that resolves to an object containing the name and source of the package manager.
*/
async function determinePackageManager(
host: Host,
cwd: string,
configured?: PackageManagerName,
logger?: Logger,
dryRun?: boolean,
): Promise<{ name: PackageManagerName; source: 'configured' | 'discovered' | 'default' }> {
let name: PackageManagerName;
let source: 'configured' | 'discovered' | 'default';
if (configured) {
name = configured;
source = 'configured';
logger?.debug(`Using configured package manager: '${name}'.`);
} else {
const discovered = await discover(host, cwd, logger);
if (discovered) {
name = discovered;
source = 'discovered';
logger?.debug(`Discovered package manager: '${name}'.`);
} else {
name = DEFAULT_PACKAGE_MANAGER;
source = 'default';
logger?.debug(
`No lockfile found. Using default package manager: '${DEFAULT_PACKAGE_MANAGER}'.`,
);
}
}
if (name === 'yarn' && !dryRun) {
const version = await getYarnVersion(host, cwd, logger);
if (version && major(version) < 2) {
name = 'yarn-classic';
logger?.debug(`Detected yarn classic. Using 'yarn-classic'.`);
}
} else if (name === 'yarn') {
logger?.debug('Skipping yarn version check due to dry run. Assuming modern yarn.');
}
return { name, source };
}
/**
* Creates a new `PackageManager` instance for a given project.
*
* This function is the main entry point for the package manager abstraction.
* It will determine, verify, and instantiate the correct package manager.
*
* @param options An object containing the options for creating the package manager.
* @returns A promise that resolves to a new `PackageManager` instance.
*/
export async function createPackageManager(options: {
root: string;
cacheDirectory: string;
configuredPackageManager?: PackageManagerName;
logger?: Logger;
dryRun?: boolean;
}): Promise<PackageManager> {
const { root: cwd, cacheDirectory, configuredPackageManager, logger, dryRun } = options;
const host = createNodeJsHost(cwd, cacheDirectory);
const { name, source } = await determinePackageManager(
host,
cwd,
configuredPackageManager,
logger,
dryRun,
);
const descriptor = SUPPORTED_PACKAGE_MANAGERS[name];
if (!descriptor) {
throw new Error(`Unsupported package manager: "${name}"`);
}
const packageManager = new PackageManager(host, cwd, descriptor, { dryRun, logger });
// Do not verify if the package manager is installed during a dry run.
if (!dryRun) {
try {
await packageManager.getVersion();
} catch {
if (source === 'default') {
throw new Error(
`'${DEFAULT_PACKAGE_MANAGER}' was selected as the default package manager, but it is not installed or` +
` cannot be found in the PATH. Please install '${DEFAULT_PACKAGE_MANAGER}' to continue.`,
);
} else {
throw new Error(
`The project is configured to use '${name}', but it is not installed or cannot be` +
` found in the PATH. Please install '${name}' to continue.`,
);
}
}
}
logger?.debug(`Successfully created PackageManager for '${name}'.`);
return packageManager;
}