Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
MCStreetguy committed Jul 24, 2017
2 parents bd745b8 + 21c906a commit 5dfc928
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 61 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- Debug functionalities *(for easier bug reporting)*
#### Improvements
- Path resolvement
- Debug options storing
- Debug grouping in console
- Performance
#### Patches
- Error while reading stored files
- Migration even if there were no changes made
Expand Down
3 changes: 1 addition & 2 deletions lib/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"default": {
"timings": false,
"objectLogging": false
},
"usrPref": null
}
}
72 changes: 36 additions & 36 deletions lib/snippet-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ export default {

subscriptions: null,
storage: null,
debug: null,

activate(state) {
this.debug = JSON.parse(fs.readFileSync(path.join(__dirname,'debug.json')));
if(this.debug.allowDebug) {
if(this.debug.usrPref === null) {
this.debug.usrPref = this.debug.default;
var _debug = JSON.parse(fs.readFileSync(path.join(__dirname,'debug.json')));
if(_debug.allowDebug) {
if(window.localStorage.getItem('snippet-injector-debug') === null || window.localStorage.getItem('snippet-injector-debug') === undefined || window.localStorage.getItem('snippet-injector-debug') === 'null' || Util.compareVersions(window.localStorage.getItem('snippet-injector-debug-version'),Util.getPackageVersion()) === Util.getPackageVersion()) {
window.localStorage.setItem('snippet-injector-debug',true.toString());
window.localStorage.setItem('snippet-injector-debug-time',_debug.default.timings.toString());
window.localStorage.setItem('snippet-injector-debug-objl',_debug.default.objectLogging.toString());
window.localStorage.setItem('snippet-injector-debug-group',_debug.groupTitle.main+' ~ '+_debug.groupTitle.timings);
window.localStorage.setItem('snippet-injector-debug-version',Util.getPackageVersion());
}

// Make the debug config available for each submodule
window.snippet_injector_debug = this.debug;
if(window.localStorage.getItem('snippet-injector-debug-time') === 'true' || window.localStorage.getItem('snippet-injector-debug-objl') === 'true') {
console.warn(_debug.warn);
console.groupCollapsed(_debug.groupTitle.main);

if(this.debug.usrPref.timings || this.debug.usrPref.objectLogging) {
console.warn(this.debug.warn);
console.groupCollapsed(this.debug.groupTitle.main);

if(this.debug.usrPref.timings) {
console.group(this.debug.groupTitle.timings);
if(window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.group(_debug.groupTitle.timings);
console.time("snippet-injector:activate duration");
}
}
Expand All @@ -45,73 +45,73 @@ export default {
'snippet-injector:delete': () => this.delete(),
'snippet-injector:toggle-time-debug': () => this.debugTime(),
'snippet-injector:toggle-object-debug': () => this.debugObjects(),
'snippet-injector:toggledebug': () => this.debug()
'snippet-injector:toggledebug': () => this.toggleDebug()
}));

if(this.debug.allowDebug) {
if(this.debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true') {
if(window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("snippet-injector:activate duration");
console.groupEnd();
}

if(this.debug.usrPref.objectLogging) {
console.group(this.debug.groupTitle.objectLogging);
if(window.localStorage.getItem('snippet-injector-debug-objl') === 'true') {
console.group(_debug.groupTitle.objectLogging);
console.log({
subscriptions: this.subscriptions,
storage: this.storage,
debugConfig: this.debug
storage: this.storage
});
console.groupEnd();
}

if(this.debug.usrPref.timings || this.debug.usrPref.objectLogging) {
if(window.localStorage.getItem('snippet-injector-debug-time') === 'true' || window.localStorage.getItem('snippet-injector-debug-objl') === 'true') {
console.groupEnd();
}
}
},

deactivate() {
this.subscriptions.dispose();
fs.writeFileSync(path.join(__dirname,'debug.json'),JSON.stringify(this.debug,null,2));
},

serialize() {
return new Object();
},

debug() {
toggleDebug() {
this.debugTime();
this.debugObjects();
},

debugTime() {
if(this.debug.allowDebug) {
this.debug.usrPref.timings = !this.debug.usrPref.timings;
if(this.debug.usrPref.timings) {
atom.notifications.addInfo('Enabled timings debug.', {detail: 'Changes take effect on next full restart of Atom.'});
if(window.localStorage.getItem('snippet-injector-debug') === 'true') {
if(window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
window.localStorage.setItem('snippet-injector-debug-time',false.toString());
atom.notifications.addInfo('Disabled timings debug.', {detail: 'Reload the window (with Ctrl+Shift+F5 e.g.) to let the changes take effect.'});
} else {
atom.notifications.addInfo('Disabled timings debug.', {detail: 'Changes take effect on next full restart of Atom.'});
window.localStorage.setItem('snippet-injector-debug-time',true.toString());
atom.notifications.addInfo('Enabled timings debug.', {detail: 'Reload the window (with Ctrl+Shift+F5 e.g.) to let the changes take effect.'});
}
} else {
atom.notifications.addWarning('Cannot toggle debug mode since it is disabled.', null);
}
},

debugObjects() {
if(this.debug.allowDebug) {
this.debug.usrPref.objectLogging = !this.debug.usrPref.objectLogging;
if(this.debug.usrPref.objectLogging) {
atom.notifications.addInfo('Enabled object debug.', {detail: 'Changes take effect on next full restart of Atom.'});
if(window.localStorage.getItem('snippet-injector-debug') === 'true') {
if(window.localStorage.getItem('snippet-injector-debug-objl') === 'true') {
window.localStorage.setItem('snippet-injector-debug-objl',false.toString());
atom.notifications.addInfo('Disabled object debug.', {detail: 'Reload the window (with Ctrl+Shift+F5 e.g.) to let the changes take effect.'});
} else {
atom.notifications.addInfo('Disabled object debug.', {detail: 'Changes take effect on next full restart of Atom.'});
window.localStorage.setItem('snippet-injector-debug-objl',true.toString());
atom.notifications.addInfo('Enabled object debug.', {detail: 'Reload the window (with Ctrl+Shift+F5 e.g.) to let the changes take effect.'});
}
} else {
atom.notifications.addWarning('Cannot toggle debug mode since it is disabled.', null);
}
},

create() {
console.groupCollapsed("Snippet Injector - DEBUG");
console.groupCollapsed(window.localStorage.getItem('snippet-injector-debug-group'));
console.time("snippet-injector:create duration");

var selection = atom.workspace.getActiveTextEditor().getSelectedText();
Expand Down Expand Up @@ -151,7 +151,7 @@ export default {
},

insert() {
console.groupCollapsed("Snippet Injector - DEBUG");
console.groupCollapsed(window.localStorage.getItem('snippet-injector-debug-group'));
console.time("snippet-injector:insert duration");

var storage = this.storage;
Expand Down Expand Up @@ -197,7 +197,7 @@ export default {
},

delete() {
console.groupCollapsed("Snippet Injector - DEBUG");
console.groupCollapsed(window.localStorage.getItem('snippet-injector-debug-group'));
console.time("snippet-injector:delete duration");

var storage = this.storage;
Expand Down
40 changes: 26 additions & 14 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Storage {
destroy() {}

init(directory) {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:init duration");
}

Expand Down Expand Up @@ -59,25 +59,25 @@ export default class Storage {
});
this.dir = temp;

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:init duration");
}
}

store(snippet) {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:store duration");
}

if(Util.isset(snippet,Snippet)) {
fs.writeFileSync(path.join(this.storageDir,this.dir,snippet.getUID().split(' ').join('-')+'.json'),JSON.stringify(snippet,null,2));
var temp = fs.existsSync(path.join(this.storageDir,this.dir,snippet.getUID().split(' ').join('-')+'.json'));
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:store duration");
}
return temp;
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:store duration");
}
return false;
Expand All @@ -88,11 +88,11 @@ export default class Storage {
if(this.dir === '') {
throw new TypeError('Storage directory has not been initialized.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:retrieveFiles duration");
}
var temp = fs.readdirSync(path.join(this.storageDir,this.dir));
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:retrieveFiles duration");
}
return temp;
Expand All @@ -103,11 +103,11 @@ export default class Storage {
if(this.dir === '') {
throw new TypeError('Storage directory has not been initialized.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:retrieveFile duration");
}
var temp = fs.readFileSync(path.join(this.storageDir,this.dir,uid.split(' ').join('-')+'.json'));
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:retrieveFile duration");
}
return temp;
Expand All @@ -132,17 +132,17 @@ export default class Storage {
if(this.dir === '') {
throw new TypeError('Storage directory has not been initialized.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:deleteFile duration");
}
if(fs.existsSync(path.join(this.storageDir,this.dir,uid.split(' ').join('-')+'.json'))) {
fs.unlinkSync(path.join(this.storageDir,this.dir,uid.split(' ').join('-')+'.json'));
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:deleteFile duration");
}
return true;
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:deleteFile duration");
}
return false;
Expand All @@ -154,7 +154,7 @@ export default class Storage {
if(this.dir === '') {
throw new TypeError('Storage directory has not been initialized.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("storage:migrate duration");
}

Expand All @@ -171,6 +171,7 @@ export default class Storage {
} else {
current = current.replace('.json','');
}
_this.backup(current);

var snippet = new Snippet(JSON.parse(_this.retrieveFile(current)));
var res = Util.compareVersions('1.0.0',snippet.getVersion());
Expand All @@ -196,9 +197,20 @@ export default class Storage {
}
}

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("storage:migrate duration");
}
}
}

backup(uid) {
var backupdir = path.join(this.storageDir,'recovery','snippet-injector');
if(!fs.existsSync(backupdir)) {
fs.mkdirSync(backupdir);
}
if(fs.existsSync(path.join(this.storageDir,this.dir,uid+'.json'))) {
var snippet = JSON.parse(this.retrieveFile(uid));
fs.writeFileSync(path.join(backupdir,uid+'.json'),JSON.stringify(snippet));
}
}
}
23 changes: 14 additions & 9 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Util {
} else if(typeof callback !== 'function') {
throw TypeError('Param \'callback\' has to be a function.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("util.js:promptUser duration");
}

Expand Down Expand Up @@ -83,7 +83,7 @@ export default class Util {

modal.show();

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("util.js:promptUser duration");
}
}
Expand All @@ -95,7 +95,7 @@ export default class Util {
} else if(typeof callback !== 'function') {
throw TypeError('Param \'callback\' has to be a function.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("util.js:promptSearch duration");
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export default class Util {

modal.show();

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("util.js:promptSearch duration");
}
}
Expand All @@ -204,7 +204,7 @@ export default class Util {
} else if(typeof callback !== 'function') {
throw TypeError('Param \'callback\' has to be a function.');
} else {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("util.js:promptDelete duration");
}

Expand Down Expand Up @@ -258,7 +258,7 @@ export default class Util {

modal.show();

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("util.js:promptDelete duration");
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@ export default class Util {
}

static generateUID(options) {
if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.time("util.js:generateUID duration");
}

Expand All @@ -341,7 +341,7 @@ export default class Util {
uid = this._gen();
}

if(window.snippet_injector_debug.allowDebug && window.snippet_injector_debug.usrPref.timings) {
if(window.localStorage.getItem('snippet-injector-debug') === 'true' && window.localStorage.getItem('snippet-injector-debug-time') === 'true') {
console.timeEnd("util.js:generateUID duration");
}
return uid;
Expand Down Expand Up @@ -397,6 +397,11 @@ export default class Util {
}

static isset(attr,type) {
return (attr !== null && attr !== undefined && (typeof attr === type || attr instanceof type));
try {
var temp = attr instanceof type;
} catch (e) {
var temp = false;
} finally {}
return (attr !== null && attr !== undefined && (typeof attr === type || temp));
}
}

0 comments on commit 5dfc928

Please sign in to comment.