-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
179 lines (162 loc) · 3.84 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const fs = require('fs');
const electron = require('electron');
const Project = require('./src/models/Project');
const ProjectList = require('./src/utils/ProjectList');
const BrowserWindowFactory = require('./src/utils/BrowserWindowFactory');
const AppMenu = require('./src/utils/AppMenu');
const AppUpdater = require('./src/utils/AppUpdater');
const app = electron.app;
const dialog = electron.dialog;
let windowIcon = __dirname + '/build/icons/icon.svg';
let windowSize = 1024;
let debug = false;
let projectList = new ProjectList();
let mainWindow = null;
let projectWindow = null;
/**
* @type {Project}
*/
let activeProject = null;
let viewingPackage = null;
/**
* Main window for the app
*/
function createMainWindow() {
mainWindow = BrowserWindowFactory.createWindow(`file://${__dirname}/src/windows/index.html`, windowSize, windowIcon);
if (debug) mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null
})
}
/**
* Open a project in a new window
*
* @param folder
*/
function createProjectWindow(folder) {
try {
activeProject = new Project(folder);
projectWindow = BrowserWindowFactory.createWindow(`file://${__dirname}/src/windows/project/project.html`, windowSize, windowIcon);
if (debug) projectWindow.webContents.openDevTools();
projectWindow.on('show', () => {
if (mainWindow !== null) {
mainWindow.close();
}
});
projectWindow.on('closed', () => {
activeProject = null;
projectWindow = null;
if (mainWindow === null) {
createMainWindow();
}
});
} catch (e) {
console.error(e);
dialog.showMessageBox({
'type': 'error',
'buttons': [],
'message': 'There is was an error parsing the composer.json'
});
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
refreshProjectList();
AppMenu.setProjects(projectList.getList());
AppMenu.setMenu();
createMainWindow();
new AppUpdater();
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', function () {
if (mainWindow === null && projectWindow === null) {
createMainWindow()
}
});
/**
* Add a project to the ProjectsList from a chosen directory, then open that project
* @param folder
* @void
*/
const fromNewProject = function (folder) {
const composerJson = require(folder + '/composer.json');
projectList.addProject(folder, composerJson.name);
createProjectWindow(folder);
};
/**
* Open an existing project directory
* @void
*/
const openDirectory = function () {
let folder = dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
});
if (!folder) {
return;
}
fs.exists(folder + '/composer.json', function (exists) {
if (exists) {
const composerJson = require(folder + '/composer.json');
projectList.addProject(folder, composerJson.name);
createProjectWindow(folder[0]);
} else {
dialog.showMessageBox(mainWindow, {
'type': 'error',
'buttons': [],
'message': 'There is no composer.json in that directory'
});
}
});
};
/**
* Open a new window for a project
* @param path
* @void
*/
const openProject = function (path) {
createProjectWindow(path);
};
/**
* @returns {Project}
*/
const getActiveProject = () => {
return activeProject;
};
/**
* @void
*/
const refreshProjectList = () => {
try {
projectList.refreshList();
} catch (e) {
// This has issues when first run, or JSON missing.
}
};
/**
* @returns {ProjectList}
*/
const getProjectList = () => {
return projectList;
};
/**
* @returns {Package}
*/
const getViewingPackage = () => {
return viewingPackage;
};
module.exports = {
openDirectory,
openProject,
getActiveProject,
refreshProjectList,
getProjectList,
getViewingPackage,
fromNewProject
}