|
| 1 | +import {PostInstallCommand} from "../common/commands/post-install"; |
| 2 | +let emailValidatorModule = require('email-validator'); |
| 3 | +let queryString = require('querystring'); |
| 4 | +import * as helpers from "../common/helpers"; |
| 5 | + |
| 6 | +export class PostInstallCliCommand extends PostInstallCommand { |
| 7 | + |
| 8 | + private fileSystem: IFileSystem; |
| 9 | + private logger: ILogger; |
| 10 | + |
| 11 | + constructor($fs: IFileSystem, |
| 12 | + private $httpClient: Server.IHttpClient, |
| 13 | + private $prompter: IPrompter, |
| 14 | + private $userSettingsService: IUserSettingsService, |
| 15 | + $staticConfig: Config.IStaticConfig, |
| 16 | + $commandsService: ICommandsService, |
| 17 | + $htmlHelpService: IHtmlHelpService, |
| 18 | + $options: ICommonOptions, |
| 19 | + $doctorService: IDoctorService, |
| 20 | + $analyticsService: IAnalyticsService, |
| 21 | + $logger: ILogger) { |
| 22 | + super($fs, $staticConfig, $commandsService, $htmlHelpService, $options, $doctorService, $analyticsService, $logger); |
| 23 | + this.fileSystem = $fs; |
| 24 | + this.logger = $logger; |
| 25 | + } |
| 26 | + |
| 27 | + public execute(args: string[]): IFuture<void> { |
| 28 | + return (() => { |
| 29 | + super.execute(args).wait(); |
| 30 | + |
| 31 | + if (this.shouldAskForEmail()) { |
| 32 | + this.logger.out("Leave your e-mail address here to subscribe for NativeScript newsletter and product updates, tips and tricks:"); |
| 33 | + let email = this.getEmail("(press Enter for blank)").wait(); |
| 34 | + this.sendEmail(email); |
| 35 | + this.$userSettingsService.saveSetting("EMAIL_REGISTERED", true).wait(); |
| 36 | + } |
| 37 | + |
| 38 | + }).future<void>()(); |
| 39 | + } |
| 40 | + |
| 41 | + private shouldAskForEmail(): boolean { |
| 42 | + if (!helpers.isInteractive() || process.env.CLI_NOPROMPT === "1" || this.$userSettingsService.getSettingValue("EMAIL_REGISTERED").wait()) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + private getEmail(prompt: string, options?: IPrompterOptions): IFuture<string> { |
| 49 | + return (() => { |
| 50 | + let schema: IPromptSchema = { |
| 51 | + message: prompt, |
| 52 | + type: "input", |
| 53 | + name: "inputEmail", |
| 54 | + validate: (value: any) => { |
| 55 | + if (value === "" || emailValidatorModule.validate(value)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + return "Please provide a valid e-mail or simply leave it blank."; |
| 59 | + }, |
| 60 | + default: options && options.defaultAction |
| 61 | + }; |
| 62 | + |
| 63 | + let result = this.$prompter.get([schema]).wait(); |
| 64 | + return result.inputString; |
| 65 | + }).future<string>()(); |
| 66 | + } |
| 67 | + |
| 68 | + private sendEmail(email: string): void { |
| 69 | + if (email) { |
| 70 | + let postData = queryString.stringify({ |
| 71 | + 'elqFormName': process.argv[2], |
| 72 | + 'elqSiteID': '1325', |
| 73 | + 'emailAddress': email, |
| 74 | + 'elqCookieWrite': '0' |
| 75 | + }); |
| 76 | + |
| 77 | + let options = { |
| 78 | + url: 'https://s1325.t.eloqua.com/e/f2', |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 82 | + 'Content-Length': postData.length |
| 83 | + }, |
| 84 | + body: postData |
| 85 | + }; |
| 86 | + |
| 87 | + this.$httpClient.httpRequest(options).wait(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +$injector.registerCommand("post-install-cli", PostInstallCliCommand); |
0 commit comments