Skip to content

Commit ae5c765

Browse files
committed
feat(windows): types generation
1 parent 32f33ff commit ae5c765

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

lib/commands/typings.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export class TypingsCommand implements ICommand {
3030
result = await this.handleAndroidTypings();
3131
} else if (this.$mobileHelper.isiOSPlatform(platform)) {
3232
result = await this.handleiOSTypings();
33+
} else if (this.$mobileHelper.isWindowsPlatform(platform)) {
34+
result = await this.handleWindowsTypings();
3335
}
3436
let typingsFolder = "./typings";
3537
if (this.$options.copyTo) {
@@ -210,6 +212,83 @@ export class TypingsCommand implements ICommand {
210212
);
211213
}
212214

215+
private async handleWindowsTypings() {
216+
if (!this.$hostInfo.isWindows) {
217+
this.$logger.warn("Windows typings can only be generated on Windows.");
218+
return false;
219+
}
220+
221+
const outDir = path.resolve(
222+
this.$projectData.projectDir,
223+
"typings",
224+
"windows",
225+
);
226+
this.$fs.ensureDirectoryExists(outDir);
227+
228+
const toolsDir = path.resolve(
229+
this.$projectData.projectDir,
230+
"platforms",
231+
"windows",
232+
"tools",
233+
);
234+
const arch = process.arch === "arm64" ? "arm64" : "x64";
235+
const resolveGenerator = () =>
236+
[
237+
path.join(toolsDir, `typings-generator-${arch}.exe`),
238+
path.join(toolsDir, "typings-generator.exe"),
239+
].find((p) => this.$fs.exists(p));
240+
241+
let generatorPath = resolveGenerator();
242+
if (!generatorPath) {
243+
this.$logger.warn("No platforms folder found, preparing project now...");
244+
await this.$childProcess.spawnFromEvent(
245+
this.$hostInfo.isWindows ? "ns.cmd" : "ns",
246+
["prepare", "windows"],
247+
"exit",
248+
{ stdio: "inherit", shell: this.$hostInfo.isWindows },
249+
);
250+
generatorPath = resolveGenerator();
251+
}
252+
253+
if (!generatorPath) {
254+
this.$logger.warn(
255+
[
256+
`Could not find the Windows typings generator under ${toolsDir}.`,
257+
"Ensure @nativescript/windows is installed and the project has been prepared.",
258+
].join("\n"),
259+
);
260+
return false;
261+
}
262+
263+
const asArray = (input: string | string[]) => {
264+
if (!input) {
265+
return [];
266+
}
267+
return typeof input === "string" ? [input] : input;
268+
};
269+
270+
const generatorArgs: string[] = ["--out-dir", outDir];
271+
if (this.$options.root) {
272+
generatorArgs.push("--root", this.$options.root);
273+
}
274+
if (this.$options.roots) {
275+
generatorArgs.push("--roots", this.$options.roots);
276+
}
277+
if (this.$options.input) {
278+
generatorArgs.push("--input", this.$options.input);
279+
}
280+
for (const lib of asArray(this.$options.lib)) {
281+
generatorArgs.push("--lib", lib);
282+
}
283+
if (this.$options.libs) {
284+
generatorArgs.push("--libs", this.$options.libs);
285+
}
286+
287+
await this.$childProcess.spawnFromEvent(generatorPath, generatorArgs, "exit", {
288+
stdio: "inherit",
289+
});
290+
}
291+
213292
private async handleiOSTypings() {
214293
if (this.$options.filter !== undefined) {
215294
this.$logger.warn("--filter flag is not supported yet.");

lib/declarations.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ interface ITypingsOptions {
594594
jar: string;
595595
aar: string;
596596
filter: string;
597+
root: string;
598+
roots: string;
599+
input: string;
600+
lib: string;
601+
libs: string;
597602
}
598603

599604
interface IOptions

lib/options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ export class Options {
249249
jar: { type: OptionType.String, hasSensitiveValue: true },
250250
aar: { type: OptionType.String, hasSensitiveValue: true },
251251
filter: { type: OptionType.String, hasSensitiveValue: true },
252+
root: { type: OptionType.String, hasSensitiveValue: true },
253+
roots: { type: OptionType.String, hasSensitiveValue: true },
254+
input: { type: OptionType.String, hasSensitiveValue: true },
255+
lib: { type: OptionType.String, hasSensitiveValue: true },
256+
libs: { type: OptionType.String, hasSensitiveValue: true },
252257
git: {
253258
type: OptionType.Boolean,
254259
hasSensitiveValue: false,

0 commit comments

Comments
 (0)