1
+ exports . name = 'npm' ;
2
+ exports . desc = 'description of npm' ;
3
+ exports . options = {
4
+ '-h, --help' : 'print this help message' ,
5
+ '--files' : 'some options.'
6
+ } ;
7
+
8
+ exports . run = function ( argv , cli ) {
9
+ // 如果输入为 fis3 npm -h
10
+ // 或者 fis3 npm --help
11
+ // 则输出帮助信息。
12
+ if ( argv . h || argv . help ) {
13
+ return cli . help ( exports . name , exports . options ) ;
14
+ }
15
+
16
+
17
+ var exec = require ( 'child_process' ) . exec ;
18
+
19
+ var fisReleaseDir = fis . project . getTempPath ( '/www' ) ;
20
+ //console.log('>>>app run dir:' + fisReleaseDir);
21
+
22
+ var cmdRun = function ( cmdRunStr ) {
23
+ exec ( cmdRunStr , { cwd : fisReleaseDir } , function ( err , stdout , stderr ) {
24
+ if ( err ) {
25
+ console . log ( '>>>' + cmdRunStr + ' error:' + err ) ;
26
+ } else {
27
+ console . log ( '>>>' + cmdRunStr + ' success' ) ;
28
+ }
29
+ } ) ;
30
+ } ;
31
+
32
+
33
+ var safeRun = function ( cmdRunStr ) {
34
+ if ( cmdRunStr . endsWith ( '.js' ) ) {
35
+ var command = cmdRunStr . split ( ' ' ) [ 1 ] ;
36
+ has ( fisReleaseDir + '/' + command ) . then ( function ( isExist ) {
37
+ if ( isExist ) {
38
+ cmdRun ( cmdRunStr ) ;
39
+ } else {
40
+ console . log ( '>>>invalid file:' + command ) ;
41
+ }
42
+ } ) ;
43
+ } else {
44
+ cmdRun ( cmdRunStr ) ;
45
+ }
46
+ }
47
+
48
+ var has = function ( path ) {
49
+ return new Promise ( function ( resolve , reject ) {
50
+ fs . stat ( path , function ( err , stat ) {
51
+ if ( err == null && stat . isDirectory ( ) ) {
52
+ resolve ( true ) ;
53
+ } else if ( err == null && stat . isFile ( ) ) {
54
+ resolve ( true ) ;
55
+ } else {
56
+ resolve ( false ) ;
57
+ }
58
+ } ) ;
59
+ } ) ;
60
+ }
61
+
62
+ var fs = require ( 'fs' ) ;
63
+
64
+ var params = argv ? argv [ '_' ] || [ ] : [ ] ;
65
+
66
+ if ( params . length >= 2 ) {
67
+ var command = params [ 1 ] ;
68
+ if ( command == 'start' ) {
69
+ safeRun ( 'npm start' ) ;
70
+ } else if ( command == 'install' ) {
71
+ safeRun ( 'npm install' ) ;
72
+ } else if ( command . endsWith ( '.js' ) ) {
73
+ safeRun ( 'node ' + command )
74
+ } else {
75
+ console . log ( '>>>npm install invalid command:' + command ) ;
76
+ }
77
+ } else {
78
+
79
+ has ( fisReleaseDir + '/node_modules' ) . then ( function ( installed ) {
80
+ //console.log('>>>' + installed);
81
+ if ( installed ) {
82
+ safeRun ( 'node app.js' )
83
+ } else {
84
+ safeRun ( 'npm install' ) ;
85
+ safeRun ( 'node app.js' )
86
+ }
87
+ } ) ;
88
+ }
89
+
90
+
91
+ } ;
0 commit comments