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 ) ;
0 commit comments