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
+
10
+ public disableAnalytics = true ;
11
+
12
+ public execute ( args : string [ ] ) : IFuture < void > {
13
+ return ( ( ) => {
14
+ var scriptsOk = true ;
15
+
16
+ try {
17
+ this . updateShellScript ( ".profile" ) . wait ( ) ;
18
+ this . updateShellScript ( ".bashrc" ) . wait ( ) ;
19
+
20
+ this . updateBashProfile ( ) . wait ( ) ;
21
+
22
+ // zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
23
+ this . updateShellScript ( ".zshrc" ) . wait ( ) ;
24
+ } catch ( err ) {
25
+ console . log ( "Failed to update all shell start-up scripts. Auto-completion may not work. " + err ) ;
26
+ scriptsOk = false ;
27
+ }
28
+
29
+ if ( scriptsOk ) {
30
+ console . log ( "Restart your shell to enable command auto-completion." ) ;
31
+ }
32
+ } ) . future < void > ( ) ( ) ;
33
+ }
34
+
35
+ private updateShellScript ( fileName : string ) : IFuture < void > {
36
+ return ( ( ) => {
37
+ var filePath = this . getHomePath ( fileName ) ;
38
+
39
+ var doUpdate = true ;
40
+ if ( this . $fs . exists ( filePath ) . wait ( ) ) {
41
+ var contents = this . $fs . readFile ( filePath ) . wait ( ) . toString ( ) ;
42
+ 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 + / ) ) {
43
+ doUpdate = false ;
44
+ }
45
+ }
46
+
47
+ if ( doUpdate ) {
48
+ this . updateShellScriptCore ( filePath ) . wait ( ) ;
49
+ }
50
+
51
+ } ) . future < void > ( ) ( ) ;
52
+ }
53
+
54
+ private updateShellScriptCore ( filePath : string ) : IFuture < void > {
55
+ return ( ( ) => {
56
+ this . $childProcess . exec ( "nativescript completion >> " + filePath ) . wait ( ) ;
57
+ this . $childProcess . exec ( "tns completion >> " + filePath ) . wait ( ) ;
58
+ } ) . future < void > ( ) ( ) ;
59
+ }
60
+
61
+ private getHomePath ( fileName : string ) : string {
62
+ return path . join ( osenv . home ( ) , fileName ) ;
63
+ }
64
+
65
+ private updateBashProfile ( ) : IFuture < void > {
66
+ return ( ( ) => {
67
+ var callProfileScript =
68
+ "if [ -f ~/.profile ]; then\n" +
69
+ " . ~/.profile\n" +
70
+ "fi\n" ;
71
+
72
+ var bashProfileFileName = this . getHomePath ( ".bash_profile" ) ;
73
+ if ( this . $fs . exists ( bashProfileFileName ) . wait ( ) ) {
74
+ var contens = this . $fs . readFile ( bashProfileFileName ) . wait ( ) . toString ( ) ;
75
+ if ( contens . indexOf ( callProfileScript ) < 0 ) {
76
+ this . updateShellScript ( ".bash_profile" ) ;
77
+ }
78
+ } else {
79
+ this . $fs . writeFile ( bashProfileFileName , callProfileScript ) . wait ( ) ;
80
+ this . updateShellScriptCore ( bashProfileFileName ) . wait ( ) ;
81
+ }
82
+ } ) . future < void > ( ) ( ) ;
83
+ }
84
+ }
85
+ $injector . registerCommand ( "dev-post-install" , PostInstallCommand ) ;
0 commit comments