From 9032a1debbad8c9d66d676963d3826c806419e80 Mon Sep 17 00:00:00 2001 From: Stefan du Fresne Date: Fri, 1 Dec 2017 16:24:40 +0000 Subject: [PATCH 1/3] Use existing file metadata for created and modified dates NB: this definintely works on OSX. Other operating systems may have slightly different interpretations of birthtime. See: https://nodejs.org/api/fs.html#fs_class_fs_stats --- browser/main/NoteList/index.js | 39 +++++++++++++++----------- browser/main/lib/dataApi/createNote.js | 4 +-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index fc28b362..76e4c947 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -584,22 +584,29 @@ class NoteList extends React.Component { filepaths.forEach((filepath) => { fs.readFile(filepath, (err, data) => { if (err) throw Error('File reading error: ', err) - const content = data.toString() - const newNote = { - content: content, - folder: folder.key, - title: markdown.strip(findNoteTitle(content)), - type: 'MARKDOWN_NOTE' - } - dataApi.createNote(storage.key, newNote) - .then((note) => { - dispatch({ - type: 'UPDATE_NOTE', - note: note - }) - hashHistory.push({ - pathname: location.pathname, - query: {key: getNoteKey(note)} + + fs.stat(filepath, (err, {mtime, birthtime}) => { + if (err) throw Error('File stat reading error: ', err); + + const content = data.toString() + const newNote = { + content: content, + folder: folder.key, + title: markdown.strip(findNoteTitle(content)), + type: 'MARKDOWN_NOTE', + createdAt: birthtime, + updatedAt: mtime + } + dataApi.createNote(storage.key, newNote) + .then((note) => { + dispatch({ + type: 'UPDATE_NOTE', + note: note + }) + hashHistory.push({ + pathname: location.pathname, + query: {key: getNoteKey(note)} + }) }) }) }) diff --git a/browser/main/lib/dataApi/createNote.js b/browser/main/lib/dataApi/createNote.js index 91abdb08..1bb7f42a 100644 --- a/browser/main/lib/dataApi/createNote.js +++ b/browser/main/lib/dataApi/createNote.js @@ -66,12 +66,12 @@ function createNote (storageKey, input) { } } } - const noteData = Object.assign({}, input, { + const noteData = Object.assign({}, { key, createdAt: new Date(), updatedAt: new Date(), storage: storageKey - }) + }, input) CSON.writeFileSync(path.join(storage.path, 'notes', key + '.cson'), _.omit(noteData, ['key', 'storage'])) From b1063eb38f80c084336f2d8cbd1613a310186088 Mon Sep 17 00:00:00 2001 From: Stefan du Fresne Date: Fri, 1 Dec 2017 16:32:52 +0000 Subject: [PATCH 2/3] Not used to this no semi colon thing --- browser/main/NoteList/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 76e4c947..ddae415e 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -586,7 +586,7 @@ class NoteList extends React.Component { if (err) throw Error('File reading error: ', err) fs.stat(filepath, (err, {mtime, birthtime}) => { - if (err) throw Error('File stat reading error: ', err); + if (err) throw Error('File stat reading error: ', err) const content = data.toString() const newNote = { From 745d2507874a63206f5878603945bada177795e0 Mon Sep 17 00:00:00 2001 From: Stefan du Fresne Date: Thu, 14 Dec 2017 13:16:37 +0000 Subject: [PATCH 3/3] Reorder Object.assign - Respects the dates that may be in input over default dates - Respects key and storage over what might be in input --- browser/main/lib/dataApi/createNote.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/browser/main/lib/dataApi/createNote.js b/browser/main/lib/dataApi/createNote.js index 1bb7f42a..4b667385 100644 --- a/browser/main/lib/dataApi/createNote.js +++ b/browser/main/lib/dataApi/createNote.js @@ -66,12 +66,16 @@ function createNote (storageKey, input) { } } } - const noteData = Object.assign({}, { - key, - createdAt: new Date(), - updatedAt: new Date(), - storage: storageKey - }, input) + const noteData = Object.assign({}, + { + createdAt: new Date(), + updatedAt: new Date() + }, + input, // input may contain more accurate dates + { + key, + storage: storageKey + }) CSON.writeFileSync(path.join(storage.path, 'notes', key + '.cson'), _.omit(noteData, ['key', 'storage']))