-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtypings.ts
145 lines (129 loc) · 3.59 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
import { IOptions, IStaticConfig } from "../declarations";
import { IChildProcess, IFileSystem, IHostInfo } from "../common/declarations";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import { IProjectData } from "../definitions/project";
import * as path from "path";
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
) {}
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 handleAndroidTypings() {
if (!(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,
this.$projectData.getBuildRelativeDirectoryPath(),
"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" }
);
}
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),
];
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);