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

Commit 2b745d9

Browse files
committed
WIP: Migrations
1 parent 585cde0 commit 2b745d9

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

lib/snippet-injector.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default {
1313

1414
activate(state) {
1515
this.storage = new Storage({dir: '/storage/snippet-injector/snippets/'});
16-
console.log(this.storage);
17-
16+
this.storage.migrate();
17+
1818
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
1919
this.subscriptions = new CompositeDisposable();
2020
// Register command that toggles this view
@@ -23,8 +23,6 @@ export default {
2323
'snippet-injector:insert': () => this.insert(),
2424
'snippet-injector:delete': () => this.delete()
2525
}));
26-
27-
console.log('Current Version: '+Util.getPackageVersion());
2826
},
2927

3028
deactivate() {

lib/snippet.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,29 @@ export default class Snippet {
88
title = '';
99
tags = new Array();
1010
lang = '';
11-
uid = '';
12-
version = '';
11+
uid = Date.now().toString();
12+
version = Util.getPackageVersion();
1313

1414
constructor(state) {
1515
if(typeof state === 'object') {
16-
if(state.title !== null && state.title !== undefined) {
16+
if(Util.isset(state.title,'string')) {
1717
this.title = state.title;
18-
} else {
19-
this.title = "This snippet has no title.";
2018
}
21-
if(state.tags !== null && state.tags !== undefined) {
19+
if(Util.isset(state.tags,'object')) {
2220
this.tags = state.tags;
23-
} else {
24-
this.tags = new Array();
2521
}
26-
if(state.content !== null && state.content !== undefined) {
22+
if(Util.isset(state.content,'string')) {
2723
this.content = state.content;
28-
} else {
29-
this.content = "This snippet has no content";
3024
}
31-
if(state.lang !== null && state.lang !== undefined) {
25+
if(Util.isset(state.lang,'string')) {
3226
this.lang = state.lang;
33-
} else {
34-
this.lang = "";
3527
}
36-
if(state.uid !== null && state.uid !== undefined) {
28+
if(Util.isset(state.uid,'string')) {
3729
this.uid = state.uid;
38-
} else {
39-
this.uid = "";
4030
}
41-
if(state.version !== null && state.uid !== undefined) {
31+
if(Util.isset(state.version,'string')) {
4232
this.version = state.version;
43-
} else {
44-
this.version = '0.0.0';
4533
}
46-
} else {
47-
this.title = "";
48-
this.tags = new Array();
49-
this.content = "";
50-
this.lang = "";
51-
this.uid = Date.now().toString();
52-
this.version = Util.getPackageVersion();
5334
}
5435
}
5536

lib/storage.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,32 @@ export default class Storage {
112112
throw new TypeError('Storage directory has not been initialized.');
113113
} else {
114114
var files = this.retrieveFiles();
115+
var changed = 0;
116+
var migrated = 'The following snippets have been migrated:\n';
115117
if(files.length > 0) {
116-
file.forEach(function(current) {
118+
files.forEach(function(current,index) {
117119
if(current.endsWith('.snippet.json')) {
118120
fs.renameSync(this.storageDir+this.dir+current,this.storageDir+this.dir+current.replace('.snippet.json','.json'));
119121
current = current.replace('.snippet.json','.json');
122+
} else {
123+
current = current.replace('.json','');
124+
}
125+
126+
var snippet = new Snippet(JSON.parse(this.retrieveFile(current)));
127+
var res = Util.compareVersions(Util.getPackageVersion(),snippet.getVersion());
128+
if(res !== false && res === Util.getPackageVersion()) {
129+
this.store(snippet);
130+
this.deleteFile(current);
131+
migrated += snippet.getTitle();
132+
if(index < files.length-1) {
133+
migrated += ', ';
134+
}
135+
changed++;
120136
}
121-
var snippet = new Snippet(JSON.parse(this.retrieveFile(current.replace('.json',''))));
122137
});
138+
if(changed > 0) {
139+
atom.notifications.addInfo('Successfully migrated snippets within local storage to newer version.', {detail: migrated});
140+
}
123141
}
124142
}
125143
}

0 commit comments

Comments
 (0)