1
- import { IOptions , IStaticConfig } from "../declarations" ;
1
+ import { glob } from "glob" ;
2
+ import { homedir } from "os" ;
3
+ import * as path from "path" ;
4
+ import { PromptObject } from "prompts" ;
5
+ import { color } from "../color" ;
2
6
import { IChildProcess , IFileSystem , IHostInfo } from "../common/declarations" ;
3
7
import { ICommand , ICommandParameter } from "../common/definitions/commands" ;
4
8
import { injector } from "../common/yok" ;
9
+ import { IOptions , IStaticConfig } from "../declarations" ;
5
10
import { IProjectData } from "../definitions/project" ;
6
- import * as path from "path" ;
7
11
8
12
export class TypingsCommand implements ICommand {
9
13
public allowedParameters : ICommandParameter [ ] = [ ] ;
@@ -15,7 +19,8 @@ export class TypingsCommand implements ICommand {
15
19
private $mobileHelper : Mobile . IMobileHelper ,
16
20
private $childProcess : IChildProcess ,
17
21
private $hostInfo : IHostInfo ,
18
- private $staticConfig : IStaticConfig
22
+ private $staticConfig : IStaticConfig ,
23
+ private $prompter : IPrompter
19
24
) { }
20
25
21
26
public async execute ( args : string [ ] ) : Promise < void > {
@@ -49,8 +54,98 @@ export class TypingsCommand implements ICommand {
49
54
return true ;
50
55
}
51
56
57
+ private async resolveGradleDependencies ( target : string ) {
58
+ const gradleHome = path . resolve (
59
+ process . env . GRADLE_USER_HOME ?? path . join ( homedir ( ) , `/.gradle` )
60
+ ) ;
61
+ const gradleFiles = path . resolve ( gradleHome , "caches/modules-2/files-2.1/" ) ;
62
+
63
+ if ( ! this . $fs . exists ( gradleFiles ) ) {
64
+ this . $logger . warn ( "No gradle files found" ) ;
65
+ return ;
66
+ }
67
+
68
+ const pattern = `${ target . replaceAll ( ":" , "/" ) } /**/*.{jar,aar}` ;
69
+
70
+ const res = await glob ( pattern , {
71
+ cwd : gradleFiles ,
72
+ } ) ;
73
+
74
+ if ( ! res || res . length === 0 ) {
75
+ this . $logger . warn ( "No files found" ) ;
76
+ return [ ] ;
77
+ }
78
+
79
+ const items = res . map ( ( item ) => {
80
+ const [ group , artifact , version , sha1 , file ] = item . split ( "/" ) ;
81
+ return {
82
+ id : sha1 + version ,
83
+ group,
84
+ artifact,
85
+ version,
86
+ sha1,
87
+ file,
88
+ path : path . resolve ( gradleFiles , item ) ,
89
+ } ;
90
+ } ) ;
91
+
92
+ this . $logger . clearScreen ( ) ;
93
+
94
+ const choices = await this . $prompter . promptForChoice (
95
+ `Select dependencies to generate typings for (${ color . greenBright (
96
+ target
97
+ ) } )`,
98
+ items
99
+ . sort ( ( a , b ) => {
100
+ if ( a . artifact < b . artifact ) return - 1 ;
101
+ if ( a . artifact > b . artifact ) return 1 ;
102
+
103
+ return a . version . localeCompare ( b . version , undefined , {
104
+ numeric : true ,
105
+ sensitivity : "base" ,
106
+ } ) ;
107
+ } )
108
+ . map ( ( item ) => {
109
+ return {
110
+ title : `${ color . white ( item . group ) } :${ color . greenBright (
111
+ item . artifact
112
+ ) } :${ color . yellow ( item . version ) } - ${ color . cyanBright . bold (
113
+ item . file
114
+ ) } `,
115
+ value : item . id ,
116
+ } ;
117
+ } ) ,
118
+ true ,
119
+ {
120
+ optionsPerPage : process . stdout . rows - 6 , // 6 lines are taken up by the instructions
121
+ } as Partial < PromptObject >
122
+ ) ;
123
+
124
+ this . $logger . clearScreen ( ) ;
125
+
126
+ return items
127
+ . filter ( ( item ) => choices . includes ( item . id ) )
128
+ . map ( ( item ) => item . path ) ;
129
+ }
130
+
52
131
private async handleAndroidTypings ( ) {
53
- if ( ! ( this . $options . jar || this . $options . aar ) ) {
132
+ const targets = this . $options . argv . _ . slice ( 2 ) ?? [ ] ;
133
+ const paths : string [ ] = [ ] ;
134
+
135
+ if ( targets . length ) {
136
+ for ( const target of targets ) {
137
+ try {
138
+ paths . push ( ...( await this . resolveGradleDependencies ( target ) ) ) ;
139
+ } catch ( err ) {
140
+ this . $logger . trace (
141
+ `Failed to resolve gradle dependencies for target "${ target } "` ,
142
+ err
143
+ ) ;
144
+ }
145
+ }
146
+ }
147
+
148
+ if ( ! paths . length && ! ( this . $options . jar || this . $options . aar ) ) {
54
149
this . $logger . warn (
55
150
[
56
151
"No .jar or .aar file specified. Please specify at least one of the following:" ,
@@ -97,6 +192,7 @@ export class TypingsCommand implements ICommand {
97
192
const inputs : string [ ] = [
98
193
...asArray ( this . $options . jar ) ,
99
194
...asArray ( this . $options . aar ) ,
195
+ ...paths ,
100
196
] ;
101
197
102
198
await this . $childProcess . spawnFromEvent (
0 commit comments