forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypings.ts
241 lines (212 loc) · 5.87 KB
/
typings.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { glob } from "glob";
import { homedir } from "os";
import * as path from "path";
import { PromptObject } from "prompts";
import { color } from "../color";
import { IChildProcess, IFileSystem, IHostInfo } from "../common/declarations";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import { IOptions, IStaticConfig } from "../declarations";
import { IProjectData } from "../definitions/project";
export class TypingsCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(
private $logger: ILogger,
private $options: IOptions,
private $fs: IFileSystem,
private $projectData: IProjectData,
private $mobileHelper: Mobile.IMobileHelper,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $staticConfig: IStaticConfig,
private $prompter: IPrompter
) {}
public async execute(args: string[]): Promise<void> {
const platform = args[0];
let result;
if (this.$mobileHelper.isAndroidPlatform(platform)) {
result = await this.handleAndroidTypings();
} else if (this.$mobileHelper.isiOSPlatform(platform)) {
result = await this.handleiOSTypings();
}
let typingsFolder = "./typings";
if (this.$options.copyTo) {
this.$fs.copyFile(
path.resolve(this.$projectData.projectDir, "typings"),
this.$options.copyTo
);
typingsFolder = this.$options.copyTo;
}
if (result !== false) {
this.$logger.info(
"Typings have been generated in the following directory:",
typingsFolder
);
}
}
public async canExecute(args: string[]): Promise<boolean> {
const platform = args[0];
this.$mobileHelper.validatePlatformName(platform);
return true;
}
private async resolveGradleDependencies(target: string) {
const gradleHome = path.resolve(
process.env.GRADLE_USER_HOME ?? path.join(homedir(), `/.gradle`)
);
const gradleFiles = path.resolve(gradleHome, "caches/modules-2/files-2.1/");
if (!this.$fs.exists(gradleFiles)) {
this.$logger.warn("No gradle files found");
return;
}
const pattern = `${target.replaceAll(":", "/")}/**/*.{jar,aar}`;
const res = await glob(pattern, {
cwd: gradleFiles,
});
if (!res || res.length === 0) {
this.$logger.warn("No files found");
return [];
}
const items = res.map((item) => {
const [group, artifact, version, sha1, file] = item.split(path.sep);
return {
id: sha1 + version,
group,
artifact,
version,
sha1,
file,
path: path.resolve(gradleFiles, item),
};
});
this.$logger.clearScreen();
const choices = await this.$prompter.promptForChoice(
`Select dependencies to generate typings for (${color.greenBright(
target
)})`,
items
.sort((a, b) => {
if (a.artifact < b.artifact) return -1;
if (a.artifact > b.artifact) return 1;
return a.version.localeCompare(b.version, undefined, {
numeric: true,
sensitivity: "base",
});
})
.map((item) => {
return {
title: `${color.white(item.group)}:${color.greenBright(
item.artifact
)}:${color.yellow(item.version)} - ${color.cyanBright.bold(
item.file
)}`,
value: item.id,
};
}),
true,
{
optionsPerPage: process.stdout.rows - 6, // 6 lines are taken up by the instructions
} as Partial<PromptObject>
);
this.$logger.clearScreen();
return items
.filter((item) => choices.includes(item.id))
.map((item) => item.path);
}
private async handleAndroidTypings() {
const targets = this.$options.argv._.slice(2) ?? [];
const paths: string[] = [];
if (targets.length) {
for (const target of targets) {
try {
paths.push(...(await this.resolveGradleDependencies(target)));
} catch (err) {
this.$logger.trace(
`Failed to resolve gradle dependencies for target "${target}"`,
err
);
}
}
}
if (!paths.length && !(this.$options.jar || this.$options.aar)) {
this.$logger.warn(
[
"No .jar or .aar file specified. Please specify at least one of the following:",
" - path to .jar file with --jar <jar>",
" - path to .aar file with --aar <aar>",
].join("\n")
);
return false;
}
this.$fs.ensureDirectoryExists(
path.resolve(this.$projectData.projectDir, "typings", "android")
);
const dtsGeneratorPath = path.resolve(
this.$projectData.projectDir,
"platforms",
"android",
"build-tools",
"dts-generator.jar"
);
if (!this.$fs.exists(dtsGeneratorPath)) {
this.$logger.warn("No platforms folder found, preparing project now...");
await this.$childProcess.spawnFromEvent(
this.$hostInfo.isWindows ? "ns.cmd" : "ns",
["prepare", "android"],
"exit",
{ stdio: "inherit", shell: this.$hostInfo.isWindows }
);
}
const asArray = (input: string | string[]) => {
if (!input) {
return [];
}
if (typeof input === "string") {
return [input];
}
return input;
};
const inputs: string[] = [
...asArray(this.$options.jar),
...asArray(this.$options.aar),
...paths,
];
await this.$childProcess.spawnFromEvent(
"java",
[
"-jar",
dtsGeneratorPath,
"-input",
...inputs,
"-output",
path.resolve(this.$projectData.projectDir, "typings", "android"),
],
"exit",
{ stdio: "inherit" }
);
}
private async handleiOSTypings() {
if (this.$options.filter !== undefined) {
this.$logger.warn("--filter flag is not supported yet.");
}
this.$fs.ensureDirectoryExists(
path.resolve(this.$projectData.projectDir, "typings", "ios")
);
await this.$childProcess.spawnFromEvent(
"node",
[this.$staticConfig.cliBinPath, "build", "ios"],
"exit",
{
env: {
...process.env,
TNS_TYPESCRIPT_DECLARATIONS_PATH: path.resolve(
this.$projectData.projectDir,
"typings",
"ios"
),
},
stdio: "inherit",
}
);
}
}
injector.registerCommand("typings", TypingsCommand);