Skip to content

Commit 4703cef

Browse files
authored
feat: ns typings command (#5551)
* feat: ns typings dummy commands * feat: ns typings (wip) * feat: ns typings * chore: cleanup * revert: package-lock.json
1 parent 8f8dd83 commit 4703cef

File tree

6 files changed

+193
-1
lines changed

6 files changed

+193
-1
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<% if (isJekyll) { %>---
2+
title: ns typings
3+
position: 25
4+
---<% } %>
5+
6+
# ns typings
7+
8+
### Description
9+
10+
Generate iOS & Android typings by default (respecting platform support, so no ios typings generated on windows/linux machines)
11+
12+
### Commands
13+
14+
Usage | Synopsis
15+
---|---
16+
Generate typings to Android | `$ ns typings android [--jar <Jar1> --jar <Jar2>] [--aar <Aar1>] [--copy-to <Path>]`
17+
Generate typings to iOS | `$ ns typings ios [--filter <Filter>] [--copy-to <Path>]`
18+
19+
### Options
20+
21+
* `--jar` - Path to jar file to generate typings for (Supports multiple by passing them individually) (Android only)
22+
* `--aar` - Path to aar file to generate typings for (Currently unsupported) (Android only)
23+
* `--filter` - Regex to filter typings (Currently unsupported) (iOS only)
24+
25+
* `--copy-to` - Copy generated typings to the specified folder
26+
27+
<% if((isConsole && isMacOS) || isHtml) { %>### Arguments
28+
`<Platform>` is the target mobile platform for which you want to generate typings. You can set the following target platforms:
29+
* `android` - Generate typings for android.
30+
* `ios` - Generate typings for iOS.
31+
32+
33+
34+
### Related Commands
35+
36+
Command | Description
37+
----------|----------
38+
[appstore](../../publishing/appstore.html) | Lists applications registered in iTunes Connect.
39+
[appstore upload](../../publishing/appstore-upload.html) | Uploads project to iTunes Connect.
40+
[build android](build-android.html) | Builds the project for Android and produces an APK that you can manually deploy on device or in the native emulator.
41+
[build ios](build-ios.html) | Builds the project for iOS and produces an APP or IPA that you can manually deploy in the iOS Simulator or on device, respectively.
42+
[build](build.html) | Builds the project for the selected target platform and produces an application package that you can manually deploy on device or in the native emulator.
43+
[debug android](debug-android.html) | Debugs your project on a connected Android device or in a native emulator.
44+
[debug ios](debug-ios.html) | Debugs your project on a connected iOS device or in a native emulator.
45+
[debug](debug.html) | Debugs your project on a connected device or in a native emulator.
46+
[deploy](deploy.html) | Builds and deploys the project to a connected physical or virtual device.
47+
[run android](run-android.html) | Runs your project on a connected Android device or in a native Android emulator, if configured.
48+
[run ios](run-ios.html) | Runs your project on a connected iOS device or in the iOS Simulator, if configured.
49+
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
50+
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
51+
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
52+
<% } %>

docs/man_pages/start.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Command | Description
4141
[prepare `<Platform>`](project/configuration/prepare.html) | Copies relevant content from the app directory to the subdirectory for the selected target platform to let you build the project.
4242
[build `<Platform>`](project/testing/build.html) | Builds the project for the selected target platform and produces an application package or an emulator package.
4343
[deploy `<Platform>`](project/testing/deploy.html) | Deploys the project to a connected physical or virtual device.
44+
[typings `<Platform>`](project/testing/typings.html) | Generate iOS & Android typings
4445
[run](project/testing/run.html) | Runs your project on a connected device or in the native emulator, if configured.
4546
[run `<Platform>`](project/testing/run.html) | Runs your project on a connected device or in the native emulator for the specified platform, if configured.
4647
[debug `<Platform>`](project/testing/debug.html) | Debugs your project on a connected physical or virtual device.

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ injector.requireCommand("platform|update", "./commands/update-platform");
183183
injector.requireCommand("run|*all", "./commands/run");
184184
injector.requireCommand("run|ios", "./commands/run");
185185
injector.requireCommand("run|android", "./commands/run");
186+
injector.requireCommand("typings", "./commands/typings");
186187

187188
injector.requireCommand("preview", "./commands/preview");
188189

lib/commands/typings.ts

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { IOptions } from "../declarations";
2+
import { IChildProcess, IFileSystem } from "../common/declarations";
3+
import { ICommand, ICommandParameter } from "../common/definitions/commands";
4+
import { injector } from "../common/yok";
5+
import { IProjectData } from "../definitions/project";
6+
import * as path from "path";
7+
8+
export class TypingsCommand implements ICommand {
9+
public allowedParameters: ICommandParameter[] = [];
10+
constructor(
11+
private $logger: ILogger,
12+
private $options: IOptions,
13+
private $fs: IFileSystem,
14+
private $projectData: IProjectData,
15+
private $mobileHelper: Mobile.IMobileHelper,
16+
private $childProcess: IChildProcess
17+
) {}
18+
19+
public async execute(args: string[]): Promise<void> {
20+
const platform = args[0];
21+
22+
if (this.$mobileHelper.isAndroidPlatform(platform)) {
23+
await this.handleAndroidTypings();
24+
}
25+
26+
if (this.$mobileHelper.isiOSPlatform(platform)) {
27+
await this.handleiOSTypings();
28+
}
29+
let typingsFolder = "./typings";
30+
if (this.$options.copyTo) {
31+
this.$fs.copyFile(
32+
path.resolve(this.$projectData.projectDir, "typings"),
33+
this.$options.copyTo
34+
);
35+
typingsFolder = this.$options.copyTo;
36+
}
37+
this.$logger.info(
38+
"Typing have been generated in the following directory:",
39+
typingsFolder
40+
);
41+
}
42+
43+
public async canExecute(args: string[]): Promise<boolean> {
44+
const platform = args[0];
45+
this.$mobileHelper.validatePlatformName(platform);
46+
return true;
47+
}
48+
49+
private async handleAndroidTypings() {
50+
if (this.$options.aar) {
51+
return this.$logger.warn(`Open the .aar archive
52+
Extract the classes.jar and any dependencies it may have inside libs/
53+
Rename classes.jar if necessary
54+
55+
ns typings android --jar classes.jar --jar dependency-of-classes-jar.jar
56+
`);
57+
} else if (!this.$options.jar) {
58+
return this.$logger.warn(
59+
"No .jar file specified. Please specify a .jar file with --jar <Jar>."
60+
);
61+
}
62+
63+
this.$fs.ensureDirectoryExists(
64+
path.resolve(this.$projectData.projectDir, "typings", "android")
65+
);
66+
67+
const dtsGeneratorPath = path.resolve(
68+
this.$projectData.projectDir,
69+
"platforms",
70+
"android",
71+
"build-tools",
72+
"dts-generator.jar"
73+
);
74+
if (!this.$fs.exists(dtsGeneratorPath)) {
75+
this.$logger.warn("No platforms folder found, preparing project now...");
76+
await this.$childProcess.spawnFromEvent(
77+
"ns",
78+
["prepare", "android"],
79+
"exit",
80+
{ stdio: "inherit" }
81+
);
82+
}
83+
84+
if (this.$options.jar) {
85+
const jars: string[] =
86+
typeof this.$options.jar === "string"
87+
? [this.$options.jar]
88+
: this.$options.jar;
89+
await this.$childProcess.spawnFromEvent(
90+
"java",
91+
[
92+
"-jar",
93+
dtsGeneratorPath,
94+
"-input",
95+
...jars,
96+
"-output",
97+
path.resolve(this.$projectData.projectDir, "typings", "android"),
98+
],
99+
"exit",
100+
{ stdio: "inherit" }
101+
);
102+
}
103+
}
104+
105+
private async handleiOSTypings() {
106+
if (this.$options.filter !== undefined) {
107+
this.$logger.warn("--filter flag is not supported yet.");
108+
}
109+
110+
this.$fs.ensureDirectoryExists(
111+
path.resolve(this.$projectData.projectDir, "typings", "ios")
112+
);
113+
114+
await this.$childProcess.spawnFromEvent("ns", ["build", "ios"], "exit", {
115+
env: {
116+
...process.env,
117+
TNS_TYPESCRIPT_DECLARATIONS_PATH: path.resolve(
118+
this.$projectData.projectDir,
119+
"typings",
120+
"ios"
121+
),
122+
},
123+
stdio: "inherit",
124+
});
125+
}
126+
}
127+
128+
injector.registerCommand("typings", TypingsCommand);

lib/declarations.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ interface IAndroidBundleOptions {
579579
aab: boolean;
580580
}
581581

582+
interface ITypingsOptions {
583+
jar: string;
584+
aar: string;
585+
filter: string;
586+
}
587+
582588
interface IOptions
583589
extends IRelease,
584590
IDeviceIdentifier,
@@ -598,7 +604,8 @@ interface IOptions
598604
IPort,
599605
IEnvOptions,
600606
IPluginSeedOptions,
601-
IGenerateOptions {
607+
IGenerateOptions,
608+
ITypingsOptions {
602609
argv: IYargArgv;
603610
validateOptions(
604611
commandSpecificDashedOptions?: IDictionary<IDashedOption>,

lib/options.ts

+3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ export class Options {
221221
hasSensitiveValue: true,
222222
},
223223
appleSessionBase64: { type: OptionType.String, hasSensitiveValue: true },
224+
jar: { type: OptionType.String, hasSensitiveValue: true },
225+
aar: { type: OptionType.String, hasSensitiveValue: true },
226+
filter: { type: OptionType.String, hasSensitiveValue: true },
224227
};
225228
}
226229

0 commit comments

Comments
 (0)