Skip to content

Commit d25f412

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

File tree

4 files changed

+94
-1
lines changed

4 files changed

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

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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);

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)