6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { JsonParseMode , isJsonObject , parseJson } from '@angular-devkit/core' ;
9
- import * as child_process from 'child_process' ;
10
9
import * as fs from 'fs' ;
11
10
import * as path from 'path' ;
12
11
import { Command } from '../models/command' ;
13
12
import { colors } from '../utilities/color' ;
14
- import { findUp } from '../utilities/find-up' ;
15
13
import { Schema as VersionCommandSchema } from './version' ;
16
14
15
+ interface PartialPackageInfo {
16
+ name : string ;
17
+ version : string ;
18
+ dependencies ?: Record < string , string > ;
19
+ devDependencies ?: Record < string , string > ;
20
+ }
21
+
17
22
export class VersionCommand extends Command < VersionCommandSchema > {
18
23
public static aliases = [ 'v' ] ;
19
24
20
25
async run ( ) {
21
- const pkg = require ( path . resolve ( __dirname , '..' , ' package.json') ) ;
22
- let projPkg ;
26
+ const cliPackage : PartialPackageInfo = require ( '../ package.json' ) ;
27
+ let workspacePackage : PartialPackageInfo | undefined ;
23
28
try {
24
- projPkg = require ( path . resolve ( this . workspace . root , 'package.json' ) ) ;
25
- } catch {
26
- projPkg = undefined ;
27
- }
29
+ workspacePackage = require ( path . resolve ( this . workspace . root , 'package.json' ) ) ;
30
+ } catch { }
28
31
29
32
const patterns = [
30
33
/ ^ @ a n g u l a r \/ .* / ,
@@ -39,33 +42,13 @@ export class VersionCommand extends Command<VersionCommandSchema> {
39
42
/ ^ w e b p a c k $ / ,
40
43
] ;
41
44
42
- const maybeNodeModules = findUp ( 'node_modules' , __dirname ) ;
43
- const packageRoot = projPkg
44
- ? path . resolve ( this . workspace . root , 'node_modules' )
45
- : maybeNodeModules ;
46
-
47
45
const packageNames = [
48
- ...Object . keys ( ( pkg && pkg [ ' dependencies' ] ) || { } ) ,
49
- ...Object . keys ( ( pkg && pkg [ ' devDependencies' ] ) || { } ) ,
50
- ...Object . keys ( ( projPkg && projPkg [ ' dependencies' ] ) || { } ) ,
51
- ...Object . keys ( ( projPkg && projPkg [ ' devDependencies' ] ) || { } ) ,
46
+ ...Object . keys ( cliPackage . dependencies || { } ) ,
47
+ ...Object . keys ( cliPackage . devDependencies || { } ) ,
48
+ ...Object . keys ( workspacePackage ?. dependencies || { } ) ,
49
+ ...Object . keys ( workspacePackage ?. devDependencies || { } ) ,
52
50
] ;
53
51
54
- if ( packageRoot != null ) {
55
- // Add all node_modules and node_modules/@*/*
56
- const nodePackageNames = fs . readdirSync ( packageRoot ) . reduce < string [ ] > ( ( acc , name ) => {
57
- if ( name . startsWith ( '@' ) ) {
58
- return acc . concat (
59
- fs . readdirSync ( path . resolve ( packageRoot , name ) ) . map ( subName => name + '/' + subName ) ,
60
- ) ;
61
- } else {
62
- return acc . concat ( name ) ;
63
- }
64
- } , [ ] ) ;
65
-
66
- packageNames . push ( ...nodePackageNames ) ;
67
- }
68
-
69
52
const versions = packageNames
70
53
. filter ( x => patterns . some ( p => p . test ( x ) ) )
71
54
. reduce (
@@ -74,31 +57,18 @@ export class VersionCommand extends Command<VersionCommandSchema> {
74
57
return acc ;
75
58
}
76
59
77
- acc [ name ] = this . getVersion ( name , packageRoot , maybeNodeModules ) ;
60
+ acc [ name ] = this . getVersion ( name ) ;
78
61
79
62
return acc ;
80
63
} ,
81
64
{ } as { [ module : string ] : string } ,
82
65
) ;
83
66
84
- let ngCliVersion = pkg . version ;
85
- if ( ! __dirname . match ( / n o d e _ m o d u l e s / ) ) {
86
- let gitBranch = '??' ;
87
- try {
88
- const gitRefName = child_process . execSync ( 'git rev-parse --abbrev-ref HEAD' , {
89
- cwd : __dirname ,
90
- encoding : 'utf8' ,
91
- stdio : 'pipe' ,
92
- } ) ;
93
- gitBranch = gitRefName . replace ( '\n' , '' ) ;
94
- } catch { }
95
-
96
- ngCliVersion = `local (v${ pkg . version } , branch: ${ gitBranch } )` ;
97
- }
67
+ const ngCliVersion = cliPackage . version ;
98
68
let angularCoreVersion = '' ;
99
69
const angularSameAsCore : string [ ] = [ ] ;
100
70
101
- if ( projPkg ) {
71
+ if ( workspacePackage ) {
102
72
// Filter all angular versions that are the same as core.
103
73
angularCoreVersion = versions [ '@angular/core' ] ;
104
74
if ( angularCoreVersion ) {
@@ -156,7 +126,7 @@ export class VersionCommand extends Command<VersionCommandSchema> {
156
126
return acc ;
157
127
} , [ ] )
158
128
. join ( '\n... ' ) }
159
- Ivy Workspace: ${ projPkg ? this . getIvyWorkspace ( ) : '' }
129
+ Ivy Workspace: ${ workspacePackage ? this . getIvyWorkspace ( ) : '' }
160
130
161
131
Package${ namePad . slice ( 7 ) } Version
162
132
-------${ namePad . replace ( / / g, '-' ) } ------------------
@@ -168,28 +138,33 @@ export class VersionCommand extends Command<VersionCommandSchema> {
168
138
) ;
169
139
}
170
140
171
- private getVersion (
172
- moduleName : string ,
173
- projectNodeModules : string | null ,
174
- cliNodeModules : string | null ,
175
- ) : string {
176
- try {
177
- if ( projectNodeModules ) {
178
- const modulePkg = require ( path . resolve ( projectNodeModules , moduleName , 'package.json' ) ) ;
141
+ private getVersion ( moduleName : string ) : string {
142
+ let packagePath ;
143
+ let cliOnly = false ;
179
144
180
- return modulePkg . version ;
181
- }
145
+ // Try to find the package in the workspace
146
+ try {
147
+ packagePath = require . resolve ( `${ moduleName } /package.json` , { paths : [ this . workspace . root ] } ) ;
182
148
} catch { }
183
149
184
- try {
185
- if ( cliNodeModules ) {
186
- const modulePkg = require ( path . resolve ( cliNodeModules , moduleName , 'package.json' ) ) ;
150
+ // If not found, try to find within the CLI
151
+ if ( ! packagePath ) {
152
+ try {
153
+ packagePath = require . resolve ( `${ moduleName } /package.json` ) ;
154
+ cliOnly = true ;
155
+ } catch { }
156
+ }
187
157
188
- return modulePkg . version + ' (cli-only)' ;
189
- }
190
- } catch { }
158
+ let version : string | undefined ;
159
+
160
+ // If found, attempt to get the version
161
+ if ( packagePath ) {
162
+ try {
163
+ version = require ( packagePath ) . version + ( cliOnly ? ' (cli-only)' : '' ) ;
164
+ } catch { }
165
+ }
191
166
192
- return '<error>' ;
167
+ return version || '<error>' ;
193
168
}
194
169
195
170
private getIvyWorkspace ( ) : string {
0 commit comments