Skip to content

Commit 085c04d

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

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-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

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

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)