|
| 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); |
0 commit comments