2
2
3
3
const path = nodeRequire ( 'path' ) ;
4
4
const fs = nodeRequire ( 'fs' ) ;
5
- const childProcess = nodeRequire ( 'child_process' ) ;
6
5
const del = nodeRequire ( 'del' ) ;
7
6
const gulp = nodeRequire ( 'gulp' ) ;
8
7
const extract = nodeRequire ( 'extract-zip' ) ;
9
8
const electron = nodeRequire ( 'electron' ) ;
10
9
const _ = nodeRequire ( 'lodash' ) ;
11
10
const async = nodeRequire ( 'async' ) ;
12
11
const remote = electron . remote ;
12
+ const ipc = electron . ipcRenderer ;
13
13
const shell = electron . shell ;
14
+ const dialog = electron . dialog ;
15
+ const BrowserWindow = remote . BrowserWindow ;
14
16
const dev = nodeRequire ( path . join ( __dirname , './src/_tasks/dev.js' ) ) ;
15
17
const dist = nodeRequire ( path . join ( __dirname , './src/_tasks/dist.js' ) ) ;
16
18
const zip = nodeRequire ( path . join ( __dirname , './src/_tasks/zip.js' ) ) ;
17
19
const ftp = nodeRequire ( path . join ( __dirname , './src/_tasks/ftp.js' ) ) ;
18
20
const Common = nodeRequire ( path . join ( __dirname , './src/common' ) ) ;
21
+ const packageJson = nodeRequire ( path . join ( __dirname , './package.json' ) ) ;
19
22
20
23
//变量声明
21
24
let $welcome = $ ( '#js-welcome' ) ;
@@ -43,6 +46,7 @@ let curConfigPath = Common.CONFIGPATH;
43
46
let config = nodeRequire ( curConfigPath ) ;
44
47
let FinderTitle = Common . PLATFORM === 'win32' ? '在 文件夹 中查看' : '在 Finder 中查看' ;
45
48
let bsObj = { } ;
49
+ let checkHandler = null ;
46
50
47
51
//初始化
48
52
init ( ) ;
@@ -51,6 +55,8 @@ init();
51
55
//其他则直接初始化数据
52
56
function init ( ) {
53
57
58
+ checkForUpdate ( ) ;
59
+
54
60
let storage = Common . getStorage ( ) ;
55
61
56
62
if ( ! storage ) {
@@ -62,16 +68,89 @@ function init() {
62
68
63
69
let workspace = path . join ( remote . app . getPath ( Common . DEFAULT_PATH ) , Common . WORKSPACE ) ;
64
70
65
- $formWorkspace . val ( workspace ) ;
71
+ fs . mkdir ( workspace , function ( err ) {
72
+
73
+ if ( err ) {
74
+ throw new Error ( err ) ;
75
+ }
76
+
77
+ $formWorkspace . val ( workspace ) ;
78
+
79
+ storage . workspace = workspace ;
80
+ Common . setStorage ( storage ) ;
66
81
67
- storage . workspace = workspace ;
68
- Common . setStorage ( storage )
82
+ console . log ( 'Create workspace success.' ) ;
83
+ } ) ;
69
84
} else {
85
+ checkLocalProjects ( ) ;
70
86
initData ( ) ;
71
87
}
72
88
73
89
}
74
90
91
+ //每次启动的时候检查本地项目是否还存在
92
+ function checkLocalProjects ( ) {
93
+ let storage = Common . getStorage ( ) ;
94
+
95
+ if ( storage ) {
96
+ if ( storage . workspace ) {
97
+
98
+ if ( ! Common . dirExist ( storage . workspace ) ) {
99
+ console . log ( '本地工作区已不存在' ) ;
100
+
101
+ //清空数据
102
+ storage . projects = { } ;
103
+ }
104
+
105
+ if ( storage . projects ) {
106
+
107
+ let projects = storage . projects ;
108
+
109
+ _ . forEach ( projects , function ( project , key ) {
110
+ if ( ! Common . dirExist ( project . path ) ) {
111
+ delete projects [ key ] ;
112
+ }
113
+ } ) ;
114
+
115
+ storage . projects = projects ;
116
+
117
+ }
118
+
119
+ Common . setStorage ( storage ) ;
120
+
121
+ }
122
+ }
123
+ }
124
+
125
+ //检查更新
126
+ function checkForUpdate ( action ) {
127
+ if ( action ) {
128
+ checkHandler = $ . ajax ( {
129
+ method : 'GET' ,
130
+ url : Common . CHECKURL ,
131
+ dataType : 'json' ,
132
+ cache : false ,
133
+ success : function ( data ) {
134
+ if ( data [ 0 ] . tag_name > packageJson . version ) {
135
+ ipc . send ( 'checkForUpdate' , 1 )
136
+ } else {
137
+ ipc . send ( 'checkForUpdate' , 0 ) ;
138
+ }
139
+ }
140
+ } ) ;
141
+ }
142
+ }
143
+
144
+ ipc . on ( 'checkForUpdateReply' , function ( event , index , status ) {
145
+ if ( status ) {
146
+ if ( index === 1 ) {
147
+ alert ( '哈哈哈, 你真的以为我等下会提醒你吗?' ) ;
148
+ } else {
149
+ shell . openExternal ( Common . DOWNLOADURL ) ;
150
+ }
151
+ }
152
+ } ) ;
153
+
75
154
//初始化数据
76
155
function initData ( ) {
77
156
let storage = Common . getStorage ( ) ;
@@ -118,7 +197,7 @@ $example.on('click', function () {
118
197
let projectName = Common . EXAMPLE_NAME ;
119
198
let projectPath = path . join ( storage [ 'workspace' ] , Common . EXAMPLE_NAME ) ;
120
199
121
- if ( storage . projects [ projectName ] ) {
200
+ if ( storage . projects && storage . projects [ projectName ] ) {
122
201
//已经打开,直接切换
123
202
} else {
124
203
@@ -142,6 +221,10 @@ $example.on('click', function () {
142
221
143
222
$projectHtml . trigger ( 'click' ) ;
144
223
224
+ if ( ! storage [ 'projects' ] ) {
225
+ storage [ 'projects' ] = { } ;
226
+ }
227
+
145
228
storage [ 'projects' ] [ projectName ] = { } ;
146
229
storage [ 'projects' ] [ projectName ] [ 'path' ] = projectPath ;
147
230
Common . setStorage ( storage ) ;
@@ -300,7 +383,7 @@ function delProject(cb) {
300
383
cb && cb ( ) ;
301
384
}
302
385
303
- function killBs ( ) {
386
+ function killBs ( ) {
304
387
var projectPath = $curProject . attr ( 'title' ) ;
305
388
if ( bsObj [ projectPath ] ) {
306
389
try {
@@ -356,38 +439,38 @@ function editName($project, $input) {
356
439
357
440
$input . keypress ( function ( event ) {
358
441
let $this = $ ( this ) ;
359
- text = $ . trim ( $this . text ( ) ) ;
442
+ text = $ . trim ( $this . text ( ) ) ;
360
443
361
- if ( event . which === 13 && ! hasText ) {
362
- keyboard = true ;
363
- if ( text !== '' ) {
364
- setProjectInfo ( $project , $this , text ) ;
365
- hasText = true ;
366
- keyboard = false ;
367
- } else {
368
- alert ( '请输入项目名' ) ;
369
-
370
- setTimeout ( function ( ) {
371
- $this . html ( '' ) ;
372
- this . focus ( ) ;
373
- } , 10 )
374
- }
444
+ if ( event . which === 13 && ! hasText ) {
445
+ keyboard = true ;
446
+ if ( text !== '' ) {
447
+ setProjectInfo ( $project , $this , text ) ;
448
+ hasText = true ;
449
+ keyboard = false ;
450
+ } else {
451
+ alert ( '请输入项目名' ) ;
452
+
453
+ setTimeout ( function ( ) {
454
+ $this . html ( '' ) ;
455
+ this . focus ( ) ;
456
+ } , 10 )
375
457
}
376
-
458
+ }
459
+
377
460
} )
378
461
. blur ( function ( ) {
379
462
let $this = $ ( this ) ;
380
463
text = $ . trim ( $this . text ( ) ) ;
381
464
382
- if ( text ) {
465
+ if ( text ) {
383
466
hasText = false ;
384
467
keyboard = false ;
385
468
}
386
469
387
470
if ( ! hasText && ! keyboard ) {
388
-
389
- setTimeout ( function ( ) {
390
-
471
+
472
+ setTimeout ( function ( ) {
473
+
391
474
if ( text !== '' ) {
392
475
setProjectInfo ( $project , $this , text ) ;
393
476
@@ -614,9 +697,11 @@ $setting.on('change', 'input', function () {
614
697
//windows 删除目录有bug
615
698
next ( ) ;
616
699
} else {
617
- del ( [ originWorkspace ] , { force : true } ) . then ( function ( ) {
618
- next ( ) ;
619
- } )
700
+ shell . moveItemToTrash ( originWorkspace ) ;
701
+ next ( ) ;
702
+ // del([originWorkspace], {force: true}).then(function () {
703
+ // next();
704
+ // })
620
705
}
621
706
} ,
622
707
function ( next ) {
@@ -705,7 +790,7 @@ function updateConfig($this) {
705
790
706
791
//写入configPath
707
792
changeTimer = setTimeout ( function ( ) {
708
- fs . writeFile ( curConfigPath , JSON . stringify ( config ) , function ( err ) {
793
+ fs . writeFile ( curConfigPath , JSON . stringify ( config , null , 4 ) , function ( err ) {
709
794
if ( err ) {
710
795
throw new Error ( err ) ;
711
796
}
@@ -813,7 +898,6 @@ $buildDevButton.hover(function () {
813
898
} ) ;
814
899
815
900
function showAbout ( ) {
816
- const BrowserWindow = remote . BrowserWindow ;
817
901
818
902
let win = new BrowserWindow ( {
819
903
width : 360 ,
@@ -849,3 +933,11 @@ $projectList.on('click', '[data-finder=true]', function () {
849
933
$cleanLog . on ( 'click' , function ( ) {
850
934
$logContent . html ( '' ) ;
851
935
} ) ;
936
+
937
+ function stopWatch ( ) {
938
+ _ . forEach ( bsObj , function ( item ) {
939
+ if ( item ) {
940
+ item . exit ( ) ;
941
+ }
942
+ } ) ;
943
+ }
0 commit comments