forked from joemakev/brackets-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (137 loc) · 5.88 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
180
181
182
define(function (require, exports, module) {
"use strict";
/* === LOAD BRACKET MODULES === */
var CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
AppInit = brackets.getModule("utils/AppInit"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
Preferences = require("preferences");
/*
File = brackets.getModule("filesystem/File"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileUtils = brackets.getModule("file/FileUtils");
*/
/* === MAIN VARIABLES === */
var menu,
CREATE_FILE_BACKUP,
START_AUTO_BACKUP,
STOP_AUTO_BACKUP,
SET_AUTO_BACKUP_TIME,
backupInterval = 'undefined',
msInMins = 60000,
timeInMins = 3,
backupTime = 180000,
suppliedTime = 3,
timeInputElem,
prefObj,
isAutoBackup = false;
/* === MAIN FUNCTIONS === */
function createFileBackup() {
console.log('createFileBackup()');
//Clear auto backup job
if(isAutoBackup) clearInterval(backupInterval);
//Get the current document
var currDoc = DocumentManager.getCurrentDocument();
//console.log(currDoc);
if(typeof currDoc != 'undefined' && currDoc != null) {
var currDocTxt = currDoc.getText();
var currDocFile = currDoc.file;
//Blind save the orig file first
var options = {};
options.blind = true;
currDocFile.write(currDocTxt, options);
//Change path and set file variables
var currDocOrigPath = currDoc.file._path;
var currDocBakPath = currDocOrigPath+'.bak';
//Change doc path to bak
currDoc.file._path = currDocBakPath;
//Create/write the backup file
currDocFile.write(currDocTxt, options);
//Change doc path to orig
currDoc.file._path = currDocOrigPath;
}
//Restart the auto backup, if enabled
if(isAutoBackup) startAutoBackup();
} //end of createFileBackup()
function startAutoBackup() {
console.log('startAutoBackup()');
if(!isAutoBackup) {
menu.removeMenuItem(START_AUTO_BACKUP);
menu.removeMenuItem(SET_AUTO_BACKUP_TIME);
menu.addMenuItem(STOP_AUTO_BACKUP);
isAutoBackup = true;
}
clearInterval(backupInterval);
backupInterval = window.setInterval(function() {
console.log('[autoBackup]');
createFileBackup();
}, backupTime);
}
function stopAutoBackup() {
menu.addMenuItem(START_AUTO_BACKUP);
menu.addMenuItem(SET_AUTO_BACKUP_TIME);
menu.removeMenuItem(STOP_AUTO_BACKUP);
isAutoBackup = false
console.log('stopAutoBackup()');
clearInterval(backupInterval);
}
function dummyFunction() {
console.log('dummyFunction()');
}
function updateAutoBackupTime() {
console.log('updateAutoBackupTime()');
backupTime = parseInt(timeInMins) * msInMins /*300000 MS = 5 mins., 180000 MS = 3mins.*/;
}
function setAutoBackupTime() {
console.log('setAutoBackupTime()');
var btnArr = [
{ className: 'DIALOG_BTN_CLASS_PRIMARY', id: 'DIALOG_BTN_OK', text: 'OK' },
]
var dialog = Dialogs.showModalDialog(
"backupTimerSettings",
"Backup - Set Auto Backup Time",
'<label class="label-time">Time in minutes (minimum is 1): </label>'+
'<input type="text" name="min-time" id="min-time" value="'+ timeInMins +'"/>',
btnArr
);
timeInputElem = document.querySelector("#min-time");
//dialog.done() example was seen in brackets-snippets
dialog.done(function (buttonId) {
if(buttonId === "DIALOG_BTN_OK") {
suppliedTime = parseInt(timeInputElem.value);
if(suppliedTime >= 1) timeInMins = suppliedTime;
else timeInMins = 1;
Preferences.set('autoBackupTime', timeInMins)
updateAutoBackupTime();
}
});
}
/* === APP INIT === */
AppInit.appReady(function () {
/* === REGISTER COMMANDS === */
CREATE_FILE_BACKUP = "bracketsBackup.createFileBackup";
CommandManager.register("Backup - CREATE", CREATE_FILE_BACKUP, createFileBackup);
START_AUTO_BACKUP = "bracketsBackup.startAutoFileBackup";
CommandManager.register("Backup - START Auto", START_AUTO_BACKUP, startAutoBackup);
STOP_AUTO_BACKUP = "bracketsBackup.stopAutoBackup";
CommandManager.register("Backup - STOP Auto", STOP_AUTO_BACKUP, stopAutoBackup);
SET_AUTO_BACKUP_TIME = "bracketsBackup.dummyFunction";
CommandManager.register("Backup - SET Auto Backup TIME", SET_AUTO_BACKUP_TIME, setAutoBackupTime);
//var DUMMY_FUNCTION = "bracketsBackup.dummyFunction";
//CommandManager.register("Backup - Dummy Function", DUMMY_FUNCTION, dummyFunction);
/* === CREATE MENU ITEM === */
menu = Menus.addMenu("Backup", 'Backup', Menus.AFTER, Menus.AppMenuBar.FILE_MENU);
menu.addMenuItem(CREATE_FILE_BACKUP);
menu.addMenuItem(START_AUTO_BACKUP);
menu.addMenuItem(SET_AUTO_BACKUP_TIME);
//Load the CSS
ExtensionUtils.loadStyleSheet(module, "main.css");
//Get the existing preferences
prefObj = Preferences.getAll();
timeInMins = prefObj.autoBackupTime;
//Update the auto backup time
updateAutoBackupTime();
});
});