Skip to content

Commit 087484e

Browse files
committed
1.2.0
1. 启动时检测本地文件是否被删除 2. 退出时停止所有进程 3. 检查版本更新 4. 修改更新工作区路径时移动到回收站的逻辑,保证数据安全 5. 加入腾讯分析
1 parent 083470a commit 087484e

File tree

6 files changed

+167
-41
lines changed

6 files changed

+167
-41
lines changed

app.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<title>WeFlow</title>
66
<link rel="stylesheet" href="assets/css/reset.css">
77
<link rel="stylesheet" href="assets/css/app.css">
8+
9+
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=56909571" charset="UTF-8"></script>
10+
811
</head>
912
<body>
1013
<div class="wraper">

main.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const electron = require('electron');
44
const app = electron.app;
5+
const dialog = electron.dialog;
6+
const ipc = electron.ipcMain;
57
const BrowserWindow = electron.BrowserWindow;
68
const path = require('path');
79

@@ -29,11 +31,12 @@ function createWindow() {
2931
// mainWindow.webContents.openDevTools();
3032

3133
mainWindow.on('close', function (event) {
32-
if(process.platform !== 'win32' && !willClose){
34+
if (process.platform !== 'win32' && !willClose) {
3335
app.hide();
3436
event.preventDefault();
3537
}
36-
});``
38+
});
39+
``
3740

3841
// Emitted when the window is closed.
3942
mainWindow.on('closed', function () {
@@ -59,7 +62,7 @@ app.on('window-all-closed', function () {
5962
}
6063
});
6164

62-
app.on('before-quit', function(){
65+
app.on('before-quit', function () {
6366
willClose = true;
6467
});
6568

@@ -73,3 +76,28 @@ app.on('activate', function () {
7376

7477
app.show();
7578
});
79+
80+
//检查更新
81+
ipc.on('checkForUpdate', function (event, status) {
82+
let options = {};
83+
84+
if(status){
85+
options = {
86+
type: 'info',
87+
title: '检查更新...',
88+
message: "当前已有新版本, 请更新",
89+
buttons: ['点击下载最新版本', '稍后提醒我']
90+
}
91+
}else{
92+
options = {
93+
type: 'info',
94+
title: '检查更新...',
95+
message: "当前为最新版本, 不需要更新",
96+
buttons: ['确定']
97+
}
98+
}
99+
100+
dialog.showMessageBox(options, function (index) {
101+
event.sender.send('checkForUpdateReply', index, status);
102+
});
103+
});

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "WeFlow",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "A minimal Electron application",
55
"main": "main.js",
66
"scripts": {
77
"start": "electron main.js",
8-
"build:mac": "electron-packager ./ WeFlow --platform=darwin --arch=x64 --icon=./assets/img/WeFlow.icns --overwrite --out ./dist/$npm_package_version --version=0.37.8",
8+
"build:mac": "rimraf dist && electron-packager ./ WeFlow --platform=darwin --arch=x64 --icon=./assets/img/WeFlow.icns --overwrite --out ./dist/$npm_package_version --version=0.37.8",
99
"build:win64": "rimraf dist-win && electron-packager ./ WeFlow --platform=win32 --arch=x64 --icon=./assets/img/WeFlow.png --overwrite --out ./dist-win --version=0.37.8",
1010
"pack": "build --target dir",
1111
"dist": "rimraf dist && build"
@@ -76,4 +76,4 @@
7676
"tmt-ejs-helper": "^0.0.1",
7777
"vinyl-fs": "^2.4.3"
7878
}
79-
}
79+
}

src/app.js

Lines changed: 123 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
const path = nodeRequire('path');
44
const fs = nodeRequire('fs');
5-
const childProcess = nodeRequire('child_process');
65
const del = nodeRequire('del');
76
const gulp = nodeRequire('gulp');
87
const extract = nodeRequire('extract-zip');
98
const electron = nodeRequire('electron');
109
const _ = nodeRequire('lodash');
1110
const async = nodeRequire('async');
1211
const remote = electron.remote;
12+
const ipc = electron.ipcRenderer;
1313
const shell = electron.shell;
14+
const dialog = electron.dialog;
15+
const BrowserWindow = remote.BrowserWindow;
1416
const dev = nodeRequire(path.join(__dirname, './src/_tasks/dev.js'));
1517
const dist = nodeRequire(path.join(__dirname, './src/_tasks/dist.js'));
1618
const zip = nodeRequire(path.join(__dirname, './src/_tasks/zip.js'));
1719
const ftp = nodeRequire(path.join(__dirname, './src/_tasks/ftp.js'));
1820
const Common = nodeRequire(path.join(__dirname, './src/common'));
21+
const packageJson = nodeRequire(path.join(__dirname, './package.json'));
1922

2023
//变量声明
2124
let $welcome = $('#js-welcome');
@@ -43,6 +46,7 @@ let curConfigPath = Common.CONFIGPATH;
4346
let config = nodeRequire(curConfigPath);
4447
let FinderTitle = Common.PLATFORM === 'win32' ? '在 文件夹 中查看' : '在 Finder 中查看';
4548
let bsObj = {};
49+
let checkHandler = null;
4650

4751
//初始化
4852
init();
@@ -51,6 +55,8 @@ init();
5155
//其他则直接初始化数据
5256
function init() {
5357

58+
checkForUpdate();
59+
5460
let storage = Common.getStorage();
5561

5662
if (!storage) {
@@ -62,16 +68,89 @@ function init() {
6268

6369
let workspace = path.join(remote.app.getPath(Common.DEFAULT_PATH), Common.WORKSPACE);
6470

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);
6681

67-
storage.workspace = workspace;
68-
Common.setStorage(storage)
82+
console.log('Create workspace success.');
83+
});
6984
} else {
85+
checkLocalProjects();
7086
initData();
7187
}
7288

7389
}
7490

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+
75154
//初始化数据
76155
function initData() {
77156
let storage = Common.getStorage();
@@ -118,7 +197,7 @@ $example.on('click', function () {
118197
let projectName = Common.EXAMPLE_NAME;
119198
let projectPath = path.join(storage['workspace'], Common.EXAMPLE_NAME);
120199

121-
if (storage.projects[projectName]) {
200+
if (storage.projects && storage.projects[projectName]) {
122201
//已经打开,直接切换
123202
} else {
124203

@@ -142,6 +221,10 @@ $example.on('click', function () {
142221

143222
$projectHtml.trigger('click');
144223

224+
if (!storage['projects']) {
225+
storage['projects'] = {};
226+
}
227+
145228
storage['projects'][projectName] = {};
146229
storage['projects'][projectName]['path'] = projectPath;
147230
Common.setStorage(storage);
@@ -300,7 +383,7 @@ function delProject(cb) {
300383
cb && cb();
301384
}
302385

303-
function killBs(){
386+
function killBs() {
304387
var projectPath = $curProject.attr('title');
305388
if (bsObj[projectPath]) {
306389
try {
@@ -356,38 +439,38 @@ function editName($project, $input) {
356439

357440
$input.keypress(function (event) {
358441
let $this = $(this);
359-
text = $.trim($this.text());
442+
text = $.trim($this.text());
360443

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)
375457
}
376-
458+
}
459+
377460
})
378461
.blur(function () {
379462
let $this = $(this);
380463
text = $.trim($this.text());
381464

382-
if(text){
465+
if (text) {
383466
hasText = false;
384467
keyboard = false;
385468
}
386469

387470
if (!hasText && !keyboard) {
388-
389-
setTimeout(function(){
390-
471+
472+
setTimeout(function () {
473+
391474
if (text !== '') {
392475
setProjectInfo($project, $this, text);
393476

@@ -614,9 +697,11 @@ $setting.on('change', 'input', function () {
614697
//windows 删除目录有bug
615698
next();
616699
} 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+
// })
620705
}
621706
},
622707
function (next) {
@@ -705,7 +790,7 @@ function updateConfig($this) {
705790

706791
//写入configPath
707792
changeTimer = setTimeout(function () {
708-
fs.writeFile(curConfigPath, JSON.stringify(config), function (err) {
793+
fs.writeFile(curConfigPath, JSON.stringify(config, null, 4), function (err) {
709794
if (err) {
710795
throw new Error(err);
711796
}
@@ -813,7 +898,6 @@ $buildDevButton.hover(function () {
813898
});
814899

815900
function showAbout() {
816-
const BrowserWindow = remote.BrowserWindow;
817901

818902
let win = new BrowserWindow({
819903
width: 360,
@@ -849,3 +933,11 @@ $projectList.on('click', '[data-finder=true]', function () {
849933
$cleanLog.on('click', function () {
850934
$logContent.html('');
851935
});
936+
937+
function stopWatch() {
938+
_.forEach(bsObj, function (item) {
939+
if(item){
940+
item.exit();
941+
}
942+
});
943+
}

src/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Common.DEFAULT_PATH = Common.PLATFORM === 'win32' ? 'desktop' : 'home';
1616
Common.TEMPLAGE_PROJECT = path.resolve(path.join(__dirname, '../templates/project.zip'));
1717
Common.TEMPLAGE_EXAMPLE = path.resolve(path.join(__dirname, '../templates/example.zip'));
1818
Common.EXAMPLE_NAME = 'WeFlow-example';
19+
Common.CHECKURL = 'https://api.github.com/repos/weixin/WeFlow/releases';
20+
Common.DOWNLOADURL = 'https://github.com/weixin/WeFlow/releases';
1921

2022
Common.requireUncached = function (module) {
2123
delete require.cache[require.resolve(module)];

src/menu.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ if (process.platform == 'darwin') {
138138
{
139139
label: '偏好设置',
140140
accelerator: 'CmdOrCtrl+,',
141-
click: function (item, focusedWindow) {
141+
click: function () {
142142
settingFn();
143143
}
144144
},
145145
{
146-
label: '检查版本更新…',
146+
label: '检查更新…',
147147
accelerator: '',
148-
click: function (item, focusedWindow) {
149-
alert('功能实现中...')
148+
click: function () {
149+
checkForUpdate(true);
150150
}
151151
},
152152
{
@@ -181,6 +181,7 @@ if (process.platform == 'darwin') {
181181
label: '退出',
182182
accelerator: 'Command+Q',
183183
click: function () {
184+
stopWatch();
184185
remote.app.quit();
185186
}
186187
}

0 commit comments

Comments
 (0)