-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.js
119 lines (108 loc) · 2.37 KB
/
Util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const {
app,
dialog,
shell,
} = require('electron')
const prompt = require('electron-prompt')
const I18n = require('./I18n')
const fs = require('fs')
const path = require('path')
class Util {
/**
* Message box options
* @typedef {Object} MsgBoxParas
* @property {String} type type
* @property {Array} buttons buttons
* @property {Number} id default id
* @property {String} message message
* @property {String} detail detail
* @property {Number} only Only execute callback when this and res are the same
*/
/**
* Show message box
* @param {MsgBoxParas} params Options
* @returns {Promise}
*/
static msgbox(params) {
return new Promise(resolve => {
dialog.showMessageBox({
type: params.type,
buttons: params.buttons,
defaultId: params.id || 0,
title: 'LiveSupport',
message: params.message,
detail: params.detail || '',
cancelId: -1,
}, res => {
if (params.only | res === res) resolve(res)
})
})
}
/**
* Show prompt
* @param {String} message message
* @returns {Promise}
*/
static prompt(message) {
return new Promise(resolve => {
prompt({ title: 'LiveSupport', label: message })
.then(resolve)
.catch(this.showError)
})
}
/**
* Show error box
* @param {String} [err] message
*/
static showError(err) {
if (err) {
console.log(err)
dialog.showErrorBox('LiveSupport', err)
app.quit()
}
}
/**
* Open URL
* @param {String} url URL
*/
static open(url) {
shell.openExternal(url)
}
static _(id) {
if (!this.i18n) this.i18n = new I18n(this, app.getLocale())
return this.i18n._(id)
}
static get settings() {
if (!this.setting) {
const filepath = this.join(__dirname, 'settings.json')
this.setting = this.readJSON(filepath)
}
return this.setting
}
/**
* Read JSON file
* @param {String} path
* @return {Object}
*/
static readJSON(path) {
const file = fs.readFileSync(path)
const json = JSON.parse(file)
return json
}
static isExist(file) {
try {
fs.statSync(file)
return true
} catch (err) {
if (err.code === 'ENOENT') return false
}
}
/**
* Return path.join
* @return {String}
*/
static join() {
return path.join(...arguments)
}
}
module.exports = Util