|
| 1 | +import { ICommand, ICommandParameter } from "../../common/definitions/commands"; |
| 2 | +import { injector } from "../../common/yok"; |
| 3 | +import { PrepareCommand } from "../prepare"; |
| 4 | +import { PrepareController } from "../../controllers/prepare-controller"; |
| 5 | +import { IOptions, IPlatformValidationService } from "../../declarations"; |
| 6 | +import { IProjectConfigService, IProjectData } from "../../definitions/project"; |
| 7 | +import { IPlatformsDataService } from "../../definitions/platform"; |
| 8 | +import { PrepareDataService } from "../../services/prepare-data-service"; |
| 9 | +import { IMigrateController } from "../../definitions/migrate"; |
| 10 | +import { resolve } from "path"; |
| 11 | +import { IFileSystem } from "../../common/declarations"; |
| 12 | +import { color } from "../../color"; |
| 13 | + |
| 14 | +export class EmbedCommand extends PrepareCommand implements ICommand { |
| 15 | + constructor( |
| 16 | + public $options: IOptions, |
| 17 | + public $prepareController: PrepareController, |
| 18 | + public $platformValidationService: IPlatformValidationService, |
| 19 | + public $projectData: IProjectData, |
| 20 | + public $platformCommandParameter: ICommandParameter, |
| 21 | + public $platformsDataService: IPlatformsDataService, |
| 22 | + public $prepareDataService: PrepareDataService, |
| 23 | + public $migrateController: IMigrateController, |
| 24 | + |
| 25 | + private $logger: ILogger, |
| 26 | + private $fs: IFileSystem, |
| 27 | + private $projectConfigService: IProjectConfigService |
| 28 | + ) { |
| 29 | + super( |
| 30 | + $options, |
| 31 | + $prepareController, |
| 32 | + $platformValidationService, |
| 33 | + $projectData, |
| 34 | + $platformCommandParameter, |
| 35 | + $platformsDataService, |
| 36 | + $prepareDataService, |
| 37 | + $migrateController |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + private resolveHostProjectPath(hostProjectPath: string): string { |
| 42 | + if (hostProjectPath.charAt(0) === ".") { |
| 43 | + // resolve relative to the project dir |
| 44 | + const projectDir = this.$projectData.projectDir; |
| 45 | + return resolve(projectDir, hostProjectPath); |
| 46 | + } |
| 47 | + |
| 48 | + return resolve(hostProjectPath); |
| 49 | + } |
| 50 | + |
| 51 | + public async execute(args: string[]): Promise<void> { |
| 52 | + const hostProjectPath = args[1]; |
| 53 | + const resolvedHostProjectPath = |
| 54 | + this.resolveHostProjectPath(hostProjectPath); |
| 55 | + |
| 56 | + if (!this.$fs.exists(resolvedHostProjectPath)) { |
| 57 | + this.$logger.error( |
| 58 | + `The host project path ${color.yellow( |
| 59 | + hostProjectPath |
| 60 | + )} (resolved to: ${color.yellow.dim( |
| 61 | + resolvedHostProjectPath |
| 62 | + )}) does not exist.` |
| 63 | + ); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + this.$options["hostProjectPath"] = resolvedHostProjectPath; |
| 68 | + if (args.length > 2) { |
| 69 | + this.$options["hostProjectModuleName"] = args[2]; |
| 70 | + } |
| 71 | + |
| 72 | + return super.execute(args); |
| 73 | + } |
| 74 | + |
| 75 | + public async canExecute(args: string[]): Promise<boolean> { |
| 76 | + const canSuperExecute = await super.canExecute(args); |
| 77 | + |
| 78 | + if (!canSuperExecute) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // args[0] is the platform |
| 83 | + // args[1] is the path to the host project |
| 84 | + // args[2] is the host project module name |
| 85 | + |
| 86 | + const platform = args[0].toLowerCase(); |
| 87 | + |
| 88 | + // also allow these to be set in the nativescript.config.ts |
| 89 | + if (!args[1]) { |
| 90 | + const hostProjectPath = this.getEmbedConfigForKey( |
| 91 | + "hostProjectPath", |
| 92 | + platform |
| 93 | + ); |
| 94 | + if (hostProjectPath) { |
| 95 | + args[1] = hostProjectPath; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (!args[2]) { |
| 100 | + const hostProjectModuleName = this.getEmbedConfigForKey( |
| 101 | + "hostProjectModuleName", |
| 102 | + platform |
| 103 | + ); |
| 104 | + if (hostProjectModuleName) { |
| 105 | + args[2] = hostProjectModuleName; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + console.log(args); |
| 110 | + |
| 111 | + if (args.length < 2) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + private getEmbedConfigForKey(key: string, platform: string) { |
| 119 | + // get the embed.<platform>.<key> value, or fallback to embed.<key> value |
| 120 | + return this.$projectConfigService.getValue( |
| 121 | + `embed.${platform}.${key}`, |
| 122 | + this.$projectConfigService.getValue(`embed.${key}`) |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +injector.registerCommand("embed", EmbedCommand); |
0 commit comments