1
+ ///<reference path="../.d.ts"/>
2
+ "use strict" ;
3
+ import osenv = require( "osenv" ) ;
4
+ import path = require( "path" ) ;
5
+
6
+ export class PostInstallCommand implements ICommand {
7
+ constructor ( private $fs : IFileSystem ,
8
+ private $childProcess : IChildProcess ,
9
+ private $logger : ILogger ) { }
10
+
11
+ public disableAnalytics = true ;
12
+
13
+ public execute ( args : string [ ] ) : IFuture < void > {
14
+ return ( ( ) => {
15
+ var scriptsOk = true ;
16
+
17
+ try {
18
+ this . updateShellScript ( ".profile" ) . wait ( ) ;
19
+ this . updateShellScript ( ".bashrc" ) . wait ( ) ;
20
+
21
+ this . updateBashProfile ( ) . wait ( ) ;
22
+
23
+ // zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
24
+ this . updateShellScript ( ".zshrc" ) . wait ( ) ;
25
+ } catch ( err ) {
26
+ this . $logger . out ( "Failed to update all shell start-up scripts. Auto-completion may not work. " + err ) ;
27
+ scriptsOk = false ;
28
+ }
29
+
30
+ if ( scriptsOk ) {
31
+ this . $logger . out ( "Restart your shell to enable command auto-completion." ) ;
32
+ }
33
+ } ) . future < void > ( ) ( ) ;
34
+ }
35
+
36
+ private updateShellScript ( fileName : string ) : IFuture < void > {
37
+ return ( ( ) => {
38
+ var filePath = this . getHomePath ( fileName ) ;
39
+
40
+ var doUpdate = true ;
41
+ if ( this . $fs . exists ( filePath ) . wait ( ) ) {
42
+ var contents = this . $fs . readText ( filePath ) . wait ( ) ;
43
+ if ( contents . match ( / n a t i v e s c r i p t \s + c o m p l e t i o n \s + - - \s + / ) || contents . match ( / t n s \s + c o m p l e t i o n \s + - - \s + / ) ) {
44
+ doUpdate = false ;
45
+ }
46
+ }
47
+
48
+ if ( doUpdate ) {
49
+ this . updateShellScriptCore ( filePath ) . wait ( ) ;
50
+ }
51
+
52
+ } ) . future < void > ( ) ( ) ;
53
+ }
54
+
55
+ private updateShellScriptCore ( filePath : string ) : IFuture < void > {
56
+ return ( ( ) => {
57
+ this . $childProcess . exec ( "nativescript completion >> " + filePath ) . wait ( ) ;
58
+ this . $childProcess . exec ( "tns completion >> " + filePath ) . wait ( ) ;
59
+ } ) . future < void > ( ) ( ) ;
60
+ }
61
+
62
+ private getHomePath ( fileName : string ) : string {
63
+ return path . join ( osenv . home ( ) , fileName ) ;
64
+ }
65
+
66
+ private updateBashProfile ( ) : IFuture < void > {
67
+ return ( ( ) => {
68
+ var callProfileScript =
69
+ "if [ -f ~/.profile ]; then\n" +
70
+ " . ~/.profile\n" +
71
+ "fi\n" ;
72
+
73
+ var bashProfileFileName = this . getHomePath ( ".bash_profile" ) ;
74
+ if ( this . $fs . exists ( bashProfileFileName ) . wait ( ) ) {
75
+ var contens = this . $fs . readText ( bashProfileFileName ) . wait ( ) ;
76
+ if ( contens . indexOf ( callProfileScript ) < 0 ) {
77
+ this . updateShellScript ( ".bash_profile" ) ;
78
+ }
79
+ } else {
80
+ this . $fs . writeFile ( bashProfileFileName , callProfileScript ) . wait ( ) ;
81
+ this . updateShellScriptCore ( bashProfileFileName ) . wait ( ) ;
82
+ }
83
+ } ) . future < void > ( ) ( ) ;
84
+ }
85
+ }
86
+ $injector . registerCommand ( "dev-post-install" , PostInstallCommand ) ;
0 commit comments