forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-info.ts
More file actions
154 lines (139 loc) · 3.87 KB
/
version-info.ts
File metadata and controls
154 lines (139 loc) · 3.87 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
/**
* @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 { createRequire } from 'node:module';
import { CommandContext } from '../../command-builder/definitions';
import { isNodeVersionSupported } from '../../utilities/node-version';
import { VERSION } from '../../utilities/version';
/**
* A subset of `package.json` fields that are relevant for the version command.
*/
interface PartialPackageInfo {
name: string;
version: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
}
/**
* An object containing version information for a single package.
*/
export interface PackageVersionInfo {
requested: string;
installed: string;
}
/**
* An object containing all the version information that will be displayed by the command.
*/
export interface VersionInfo {
cli: {
version: string;
};
framework: {
version: string | undefined;
};
system: {
node: {
version: string;
unsupported: boolean;
};
os: {
platform: string;
architecture: string;
};
packageManager: {
name: string;
version: string | undefined;
};
};
packages: Record<string, PackageVersionInfo>;
}
/**
* A list of regular expression patterns that match package names that should be included in the
* version output.
*/
const PACKAGE_PATTERNS = [
/^@angular\/.*/,
/^@angular-devkit\/.*/,
/^@ngtools\/.*/,
/^@schematics\/.*/,
/^rxjs$/,
/^typescript$/,
/^ng-packagr$/,
/^vitest$/,
/^webpack$/,
/^zone\.js$/,
];
/**
* Gathers all the version information from the environment and workspace.
* @returns An object containing all the version information.
*/
export async function gatherVersionInfo(context: CommandContext): Promise<VersionInfo> {
// Trailing slash is used to allow the path to be treated as a directory
const workspaceRequire = createRequire(context.root + '/');
let workspacePackage: PartialPackageInfo | undefined;
try {
workspacePackage = workspaceRequire('./package.json');
} catch {}
const allDependencies = {
...workspacePackage?.dependencies,
...workspacePackage?.devDependencies,
};
const packageNames = new Set(Object.keys(allDependencies));
const packages: Record<string, PackageVersionInfo> = {};
for (const name of packageNames) {
if (PACKAGE_PATTERNS.some((p) => p.test(name))) {
packages[name] = {
requested: allDependencies[name] ?? 'error',
installed: getVersion(name, workspaceRequire),
};
}
}
const angularCoreVersion = packages['@angular/core'];
const packageManager = await context.packageManager;
return {
cli: {
version: VERSION.full,
},
framework: {
version: angularCoreVersion?.installed,
},
system: {
node: {
version: process.versions.node,
unsupported: !isNodeVersionSupported(),
},
os: {
platform: process.platform,
architecture: process.arch,
},
packageManager: {
name: packageManager.name,
version: await packageManager.getVersion(),
},
},
packages,
};
}
/**
* Gets the version of a package.
* @param moduleName The name of the package.
* @param workspaceRequire A `require` function for the workspace.
* @param localRequire A `require` function for the CLI.
* @returns The version of the package, or `<error>` if it could not be found.
*/
function getVersion(moduleName: string, workspaceRequire: NodeJS.Require): string {
let packageInfo: PartialPackageInfo | undefined;
// Try to find the package in the workspace
try {
packageInfo = workspaceRequire(`${moduleName}/package.json`);
} catch {}
// If found, attempt to get the version
if (packageInfo) {
return packageInfo.version;
}
return '<error>';
}