Skip to content

Commit

Permalink
Add SimplenoteImporter (#975)
Browse files Browse the repository at this point in the history
* Add SimplenoteImporter

* Cap file size at 1 MB

Will tweak later

* Fix tests
  • Loading branch information
mirka authored Nov 2, 2018
1 parent cea1869 commit 6bcc12c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import"
]
],
"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
}
}
6 changes: 3 additions & 3 deletions lib/utils/import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CoreImporter extends EventEmitter {
this.noteBucket.add(importedNote);
};

importNotes = (notes = {}) => {
importNotes = (notes = {}, options) => {
if (isEmpty(notes)) {
this.emit('status', 'error', 'No notes to import.');
return;
Expand All @@ -80,9 +80,9 @@ class CoreImporter extends EventEmitter {
return;
}

get(notes, 'activeNotes', []).map(note => this.importNote(note));
get(notes, 'activeNotes', []).map(note => this.importNote(note, options));
get(notes, 'trashedNotes', []).map(note =>
this.importNote(note, { isTrashed: true })
this.importNote(note, { ...options, isTrashed: true })
);
};
}
Expand Down
59 changes: 59 additions & 0 deletions lib/utils/import/simplenote/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { EventEmitter } from 'events';
import CoreImporter from '../';
import { endsWith, isEmpty } from 'lodash';

class SimplenoteImporter extends EventEmitter {
constructor({ noteBucket, tagBucket, options }) {
super();
this.noteBucket = noteBucket;
this.tagBucket = tagBucket;
this.options = options;
}

importNotes = filesArray => {
const coreImporter = new CoreImporter({
noteBucket: this.noteBucket,
tagBucket: this.tagBucket,
});

if (isEmpty(filesArray)) {
this.emit('status', 'error', 'No file to import.');
return;
}

const file = filesArray[0];

if (!endsWith(file.name.toLowerCase(), '.json')) {
this.emit('status', 'error', 'File name does not end in ".json".');
return;
}

// Limit file size we will read to 1mb
if (file.size > 1000000) {
this.emit('status', 'error', 'File should be less than 1 MB.');
return;
}

const fileReader = new FileReader();

fileReader.onload = event => {
const fileContent = event.target.result;

if (!fileContent) {
this.emit('status', 'error', 'File was empty.');
return;
}

const dataObj = JSON.parse(fileContent);
const noteCount =
dataObj.activeNotes.length + dataObj.trashedNotes.length;

coreImporter.importNotes(dataObj, this.options);
this.emit('status', 'complete', noteCount);
};

fileReader.readAsText(file);
};
}

export default SimplenoteImporter;
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "7.2.3",
"babel-loader": "8.0.0",
"babel-plugin-dynamic-import-node": "2.2.0",
"classnames": "2.2.5",
"concurrently": "^4.0.1",
"css-loader": "0.28.9",
Expand Down

0 comments on commit 6bcc12c

Please sign in to comment.