Skip to content

Commit 06aebfd

Browse files
author
Tsvetan Raikov
committed
Added email registration for NativeScript mailing lists as post install step
1 parent 791f6ab commit 06aebfd

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,6 @@ $injector.require("messages", "./common/messages/messages");
117117
$injector.require("xmlValidator", "./xml-validator");
118118

119119
$injector.requireCommand("devices", "./commands/devices");
120+
$injector.requireCommand("post-install-cli", "./commands/post-install");
120121

121122
$injector.require("iOSLogFilter", "./services/ios-log-filter");

lib/commands/post-install.ts

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

lib/definitions/email-validator.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Type definitions for email-validator 1.0.3
2+
// Project: https://github.com/Sembiance/email-validator
3+
// Definitions by: Paul Lessing <https://github.com/paullessing>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
declare module "email-validator" {
7+
export function validate(email: String): boolean;
8+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"cli-table": "https://github.com/telerik/cli-table/tarball/v0.3.1.2",
3737
"clui": "0.3.1",
3838
"colors": "1.1.2",
39+
"email-validator": "1.0.4",
3940
"esprima": "2.7.0",
4041
"ffi": "https://github.com/icenium/node-ffi/tarball/v2.0.0.3",
4142
"fibers": "https://github.com/icenium/node-fibers/tarball/v1.0.13.1",

postinstall.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"use strict";
22
var child_process = require("child_process");
3-
child_process.spawn(process.argv[0], ["bin/nativescript.js", "dev-post-install"], {stdio: "inherit"});
3+
child_process.spawn(process.argv[0], ["bin/nativescript.js", "post-install-cli"], {stdio: "inherit"});

0 commit comments

Comments
 (0)