Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
antSword
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
HuangJunbo
antSword
Commits
6431e477
Commit
6431e477
authored
Apr 30, 2016
by
antoor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reconstruction of optimized code
重构优化代码
parent
c03f6d26
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
474 additions
and
364 deletions
+474
-364
cache.js
modules/cache.js
+127
-109
config.js
modules/config.js
+47
-0
database.js
modules/database.js
+288
-244
menubar.js
modules/menubar.js
+12
-11
No files found.
modules/cache.js
View file @
6431e477
//
/**
// 缓存管理模块
* 缓存管理模块
//
* 更新:2016/04/28
* 作者:蚁逅 <https://github.com/antoor>
*/
'use strict'
;
'use strict'
;
const
fs
=
require
(
'fs'
);
const
fs
=
require
(
'fs'
),
const
path
=
require
(
'path'
);
path
=
require
(
'path'
),
const
log4js
=
require
(
'log4js'
);
CONF
=
require
(
'./config'
),
const
Datastore
=
require
(
'nedb'
);
logger
=
require
(
'log4js'
).
getLogger
(
'Cache'
),
Datastore
=
require
(
'nedb'
);
const
logger
=
log4js
.
getLogger
(
'Cache'
);
class
Cache
{
class
Cache
{
/**
* 初始化监听事件
* @param {Object} electron electron对象
* @return {[type]} [description]
*/
constructor
(
electron
)
{
constructor
(
electron
)
{
// 创建数据库
electron
.
ipcMain
// 获取用户保存目录(mac&&*unix=/home/path/,win=c:/path/appdata
.
on
(
'cache-add'
,
this
.
addCache
.
bind
(
this
))
let
dbPath
=
''
;
.
on
(
'cache-set'
,
this
.
setCache
.
bind
(
this
))
if
(
process
.
env
.
HOME
)
{
.
on
(
'cache-get'
,
this
.
getCache
.
bind
(
this
))
dbPath
=
path
.
join
(
process
.
env
.
HOME
,
'.antSword'
);
.
on
(
'cache-del'
,
this
.
delCache
.
bind
(
this
))
}
else
if
(
process
.
env
.
LOCALAPPPATH
)
{
.
on
(
'cache-clear'
,
this
.
clearCache
.
bind
(
this
))
dbPath
=
path
.
join
(
process
.
env
.
LOCALAPPPATH
,
'.antSword'
);
.
on
(
'cache-clearAll'
,
this
.
clearAllCache
.
bind
(
this
));
}
else
{
}
dbPath
=
'database'
;
};
/**
// 创建数据目录
* 创建nedb数据库文件
if
(
!
fs
.
existsSync
(
dbPath
))
{
* @param {String} id 数据存储文件名
fs
.
mkdirSync
(
dbPath
);
* @return {[type]} [description]
};
*/
// 创建缓存目录
createDB
(
id
=
String
(
+
new
Date
))
{
const
cachePath
=
path
.
join
(
dbPath
,
'cache'
);
return
new
Datastore
({
if
(
!
fs
.
existsSync
(
cachePath
))
{
filename
:
path
.
join
(
CONF
.
cachePath
,
id
),
fs
.
mkdirSync
(
cachePath
);
autoload
:
true
};
});
this
.
dbPath
=
dbPath
;
this
.
cachePath
=
cachePath
;
// 监听数据请求
this
.
listenHandle
(
electron
.
ipcMain
);
}
}
listenHandle
(
ipcMain
)
{
/**
logger
.
info
(
'listenHandle'
);
* 添加缓存数据
ipcMain
* @param {Object} event ipcMain对象
// 添加缓存
* @param {Object} opts 缓存配置(id,tag,cache
// arg={id="shellID",tag="存储标识",cache="存储内容"}
*/
.
on
(
'cache-add'
,
(
event
,
arg
)
=>
{
addCache
(
event
,
opts
)
{
logger
.
debug
(
'cache-add'
,
arg
);
logger
.
debug
(
'addCache'
,
opts
);
this
.
createDB
(
arg
[
'id'
]).
insert
({
this
.
createDB
(
opts
[
'id'
]).
insert
({
tag
:
arg
[
'tag'
],
tag
:
opts
[
'tag'
],
cache
:
arg
[
'cache'
]
cache
:
opts
[
'cache'
]
},
(
err
,
ret
)
=>
{
},
(
err
,
ret
)
=>
{
event
.
returnValue
=
err
||
ret
;
event
.
returnValue
=
err
||
ret
;
});
});
})
}
// 更新缓存
// arg = {id, tag, cache}
/**
.
on
(
'cache-set'
,
(
event
,
arg
)
=>
{
* 设置缓存数据
logger
.
debug
(
'cache-set'
,
arg
);
* @param {Object} event ipcMain对象
this
.
createDB
(
arg
[
'id'
]).
update
({
* @param {Object} opts 缓存配置(id,tag,cache
tag
:
arg
[
'tag'
]
*/
setCache
(
event
,
opts
)
{
logger
.
debug
(
'setCache'
,
opts
);
this
.
createDB
(
opts
[
'id'
]).
update
({
tag
:
opts
[
'tag'
]
},
{
},
{
$set
:
{
$set
:
{
cache
:
arg
[
'cache'
]
cache
:
opts
[
'cache'
]
}
}
},
(
err
,
ret
)
=>
{
},
(
err
,
ret
)
=>
{
event
.
returnValue
=
err
||
ret
;
event
.
returnValue
=
err
||
ret
;
});
});
})
}
// 查询缓存
// arg={id="shellID", tag="存储标识"}
/**
.
on
(
'cache-get'
,
(
event
,
arg
)
=>
{
* 获取缓存数据
logger
.
debug
(
'cache-get'
,
arg
);
* @param {Object} event ipcMain对象
this
.
createDB
(
arg
[
'id'
]).
findOne
({
* @param {Object} opts 缓存配置(id,tag)
tag
:
arg
[
'tag'
]
* @return {[type]} [description]
*/
getCache
(
event
,
opts
)
{
logger
.
debug
(
'getCache'
,
opts
);
this
.
createDB
(
opts
[
'id'
]).
findOne
({
tag
:
opts
[
'tag'
]
},
(
err
,
ret
)
=>
{
},
(
err
,
ret
)
=>
{
event
.
returnValue
=
err
||
ret
;
event
.
returnValue
=
err
||
ret
;
})
})
})
}
// 删除缓存
// arg = {id: 'SHELL-ID', tag: 'SAVE-TAG'}
/**
.
on
(
'cache-del'
,
(
event
,
arg
)
=>
{
* 删除缓存
logger
.
warn
(
'cache-del'
,
arg
);
* @param {Object} event ipcMain对象
this
.
createDB
(
arg
[
'id'
]).
remove
({
* @param {Object} opts 缓存配置(id,tag)
tag
:
arg
[
'tag'
]
* @return {[type]} [description]
*/
delCache
(
event
,
opts
)
{
logger
.
warn
(
'delCache'
,
opts
);
this
.
createDB
(
opts
[
'id'
]).
remove
({
tag
:
opts
[
'tag'
]
},
(
err
,
ret
)
=>
{
},
(
err
,
ret
)
=>
{
event
.
returnValue
=
err
||
ret
;
event
.
returnValue
=
err
||
ret
;
})
});
})
}
// 清空缓存
// arg = {id: 'SHELL-ID'}
/**
.
on
(
'cache-clear'
,
(
event
,
arg
)
=>
{
* 清空缓存数据
logger
.
fatal
(
'cache-clear'
,
arg
);
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id)
* @return {[type]} [description]
*/
clearCache
(
event
,
opts
)
{
logger
.
fatal
(
'clearCache'
,
opts
);
try
{
try
{
fs
.
unlinkSync
(
path
.
join
(
this
.
cachePath
,
arg
[
'id'
]));
fs
.
unlinkSync
(
path
.
join
(
CONF
.
cachePath
,
opts
[
'id'
]));
event
.
returnValue
=
true
;
event
.
returnValue
=
true
;
}
catch
(
e
)
{
}
catch
(
e
)
{
event
.
returnValue
=
e
;
event
.
returnValue
=
e
;
}
}
})
}
// 清空所有缓存
.
on
(
'cache-clearAll'
,
(
event
,
arg
)
=>
{
/**
logger
.
fatal
(
'cache-clearAll'
,
arg
);
* 清空所有缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(null)
* @return {[type]} [description]
*/
clearAllCache
(
event
,
opts
)
{
logger
.
fatal
(
'clearAllCache'
,
opts
);
try
{
try
{
fs
.
readdirSync
(
this
.
cachePath
).
map
((
_
)
=>
{
fs
.
readdirSync
(
CONF
.
cachePath
).
map
((
_
)
=>
{
fs
.
unlinkSync
(
path
.
join
(
this
.
cachePath
,
_
));
fs
.
unlinkSync
(
path
.
join
(
CONF
.
cachePath
,
_
));
});
});
event
.
returnValue
=
true
;
event
.
returnValue
=
true
;
}
catch
(
e
)
{
}
catch
(
e
)
{
event
.
returnValue
=
e
;
event
.
returnValue
=
e
;
}
}
})
}
}
createDB
(
id
)
{
// 创建数据库
return
new
Datastore
({
filename
:
path
.
join
(
this
.
cachePath
,
id
),
autoload
:
true
});
}
}
}
module
.
exports
=
Cache
;
module
.
exports
=
Cache
;
modules/config.js
0 → 100644
View file @
6431e477
/**
* 中国蚁剑::后端配置模块
* ? 用于进行一些通用的变量如初始化目录等设置
* 开写:2016/04/26
* 更新:2016/04/28
* 作者:蚁逅 <https://github.com/antoor>
*/
'use strict'
;
const
fs
=
require
(
'fs'
),
path
=
require
(
'path'
);
class
Conf
{
constructor
()
{
// 获取数据存储目录
this
.
basePath
=
path
.
join
(
process
.
env
.
HOME
||
process
.
env
.
LOCALAPPPATH
||
process
.
cwd
()
||
'.'
,
'.antSword'
);
// 创建.antSword目录
!
fs
.
existsSync
(
this
.
basePath
)
?
fs
.
mkdirSync
(
this
.
basePath
)
:
null
;
}
/**
* 获取数据存储路径
* @return {String} file-path
*/
get
dataPath
()
{
return
path
.
join
(
this
.
basePath
,
'shell.db'
);
}
/**
* 获取缓存目录
* @return {String} dir-path
*/
get
cachePath
()
{
let
_
=
path
.
join
(
this
.
basePath
,
'/cache/'
);
// 创建缓存目录
!
fs
.
existsSync
(
_
)
?
fs
.
mkdirSync
(
_
)
:
null
;
return
_
;
}
}
module
.
exports
=
new
Conf
();
modules/database.js
View file @
6431e477
This diff is collapsed.
Click to expand it.
modules/menubar.js
View file @
6431e477
...
@@ -4,20 +4,18 @@
...
@@ -4,20 +4,18 @@
'use strict'
;
'use strict'
;
// 读取package.json信息
const
info
=
require
(
'../package'
);
class
Menubar
{
class
Menubar
{
constructor
(
electron
,
app
,
mainWindow
)
{
constructor
(
electron
,
app
,
mainWindow
)
{
const
Menu
=
electron
.
Menu
;
const
Menu
=
electron
.
Menu
;
const
ipcMain
=
electron
.
ipcMain
;
// 清空菜单栏
// 清空菜单栏
Menu
.
setApplicationMenu
(
Menu
.
buildFromTemplate
([]));
Menu
.
setApplicationMenu
(
Menu
.
buildFromTemplate
([]));
// 监听重载菜单事件
// 监听重载菜单事件
ipcMain
.
on
(
'menubar'
,
this
.
reload
.
bind
(
this
));
electron
.
ipcMain
ipcMain
.
on
(
'quit'
,
app
.
quit
.
bind
(
app
));
.
on
(
'quit'
,
app
.
quit
.
bind
(
app
))
.
on
(
'menubar'
,
this
.
reload
.
bind
(
this
));
this
.
electron
=
electron
;
this
.
electron
=
electron
;
this
.
app
=
app
;
this
.
app
=
app
;
...
@@ -25,7 +23,12 @@ class Menubar {
...
@@ -25,7 +23,12 @@ class Menubar {
this
.
mainWindow
=
mainWindow
;
this
.
mainWindow
=
mainWindow
;
}
}
// 刷新菜单
/**
* 重新载入菜单
* @param {Object} event ipcMain对象
* @param {Object} LANG 语言模板
* @return {[type]} [description]
*/
reload
(
event
,
LANG
)
{
reload
(
event
,
LANG
)
{
// 菜单模板
// 菜单模板
const
template
=
[
const
template
=
[
...
@@ -36,9 +39,7 @@ class Menubar {
...
@@ -36,9 +39,7 @@ class Menubar {
{
{
label
:
LANG
[
'shell'
][
'add'
],
label
:
LANG
[
'shell'
][
'add'
],
accelerator
:
'Shift+A'
,
accelerator
:
'Shift+A'
,
click
:
()
=>
{
click
:
event
.
sender
.
send
.
bind
(
event
.
sender
,
'menubar'
,
'shell-add'
)
event
.
sender
.
send
(
'menubar'
,
'shell-add'
);
}
},
{
},
{
label
:
LANG
[
'shell'
][
'search'
],
label
:
LANG
[
'shell'
][
'search'
],
accelerator
:
'Shift+S'
,
accelerator
:
'Shift+S'
,
...
@@ -99,7 +100,7 @@ class Menubar {
...
@@ -99,7 +100,7 @@ class Menubar {
}
}
];
];
// 调试菜单
// 调试菜单
if
(
info
[
'
debug'
])
{
if
(
process
.
env
[
'npm_package_
debug'
])
{
template
.
push
({
template
.
push
({
label
:
LANG
[
'debug'
][
'title'
],
label
:
LANG
[
'debug'
][
'title'
],
submenu
:
[
submenu
:
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment