diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 945e6c125c..2c04947936 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -117,5 +117,6 @@ $injector.require("messages", "./common/messages/messages"); $injector.require("xmlValidator", "./xml-validator"); $injector.requireCommand("devices", "./commands/devices"); +$injector.requireCommand("post-install-cli", "./commands/post-install"); $injector.require("iOSLogFilter", "./services/ios-log-filter"); diff --git a/lib/commands/post-install.ts b/lib/commands/post-install.ts new file mode 100644 index 0000000000..844f72c860 --- /dev/null +++ b/lib/commands/post-install.ts @@ -0,0 +1,86 @@ +import {PostInstallCommand} from "../common/commands/post-install"; +import * as emailValidator from "email-validator"; +import * as queryString from "querystring"; +import * as helpers from "../common/helpers"; + +export class PostInstallCliCommand extends PostInstallCommand { + + private logger: ILogger; + + constructor($fs: IFileSystem, + private $httpClient: Server.IHttpClient, + private $prompter: IPrompter, + private $userSettingsService: IUserSettingsService, + $staticConfig: Config.IStaticConfig, + $commandsService: ICommandsService, + $htmlHelpService: IHtmlHelpService, + $options: ICommonOptions, + $doctorService: IDoctorService, + $analyticsService: IAnalyticsService, + $logger: ILogger) { + super($fs, $staticConfig, $commandsService, $htmlHelpService, $options, $doctorService, $analyticsService, $logger); + this.logger = $logger; + } + + public execute(args: string[]): IFuture { + return (() => { + super.execute(args).wait(); + + if (this.shouldAskForEmail()) { + this.logger.out("Leave your e-mail address here to subscribe for NativeScript newsletter and product updates, tips and tricks:"); + let email = this.getEmail("(press Enter for blank)").wait(); + this.$userSettingsService.saveSetting("EMAIL_REGISTERED", true).wait(); + this.sendEmail(email); + } + + }).future()(); + } + + private shouldAskForEmail(): boolean { + return helpers.isInteractive() && process.env.CLI_NOPROMPT !== "1" && !this.$userSettingsService.getSettingValue("EMAIL_REGISTERED").wait(); + } + + private getEmail(prompt: string, options?: IPrompterOptions): IFuture { + return (() => { + let schema: IPromptSchema = { + message: prompt, + type: "input", + name: "inputEmail", + validate: (value: any) => { + if (value === "" || emailValidator.validate(value)) { + return true; + } + return "Please provide a valid e-mail or simply leave it blank."; + }, + default: options && options.defaultAction + }; + + let result = this.$prompter.get([schema]).wait(); + return result.inputString; + }).future()(); + } + + private sendEmail(email: string): void { + if (email) { + let postData = queryString.stringify({ + 'elqFormName': process.argv[2], + 'elqSiteID': '1325', + 'emailAddress': email, + 'elqCookieWrite': '0' + }); + + let options = { + url: 'https://s1325.t.eloqua.com/e/f2', + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Length': postData.length + }, + body: postData + }; + + this.$httpClient.httpRequest(options).wait(); + } + } +} +$injector.registerCommand("post-install-cli", PostInstallCliCommand); diff --git a/lib/definitions/email-validator.d.ts b/lib/definitions/email-validator.d.ts new file mode 100644 index 0000000000..299ebb19f6 --- /dev/null +++ b/lib/definitions/email-validator.d.ts @@ -0,0 +1,8 @@ +// Type definitions for email-validator 1.0.3 +// Project: https://github.com/Sembiance/email-validator +// Definitions by: Paul Lessing +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "email-validator" { + export function validate(email: String): boolean; +} diff --git a/package.json b/package.json index 9a43076510..93087d37fc 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "cli-table": "https://github.com/telerik/cli-table/tarball/v0.3.1.2", "clui": "0.3.1", "colors": "1.1.2", + "email-validator": "1.0.4", "esprima": "2.7.0", "ffi": "https://github.com/icenium/node-ffi/tarball/v2.0.0.3", "fibers": "https://github.com/icenium/node-fibers/tarball/v1.0.13.1", diff --git a/postinstall.js b/postinstall.js index 7d22659abb..29b596a95e 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,3 +1,3 @@ "use strict"; var child_process = require("child_process"); -child_process.spawn(process.argv[0], ["bin/nativescript.js", "dev-post-install"], {stdio: "inherit"}); +child_process.spawn(process.argv[0], ["bin/nativescript.js", "post-install-cli"], {stdio: "inherit"});