Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions browser/main/SideNav/StorageItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ class StorageItem extends React.Component {
{
type: 'separator'
},
{
label: i18n.__('Export Storage'),
submenu: [
{
label: i18n.__('Export as txt'),
click: (e) => this.handleExportStorageClick(e, 'txt')
},
{
label: i18n.__('Export as md'),
click: (e) => this.handleExportStorageClick(e, 'md')
}
]
},
{
type: 'separator'
},
{
label: i18n.__('Unlink Storage'),
click: (e) => this.handleUnlinkStorageClick(e)
Expand Down Expand Up @@ -68,6 +84,30 @@ class StorageItem extends React.Component {
}
}

handleExportStorageClick (e, fileType) {
const options = {
properties: ['openDirectory', 'createDirectory'],
buttonLabel: i18n.__('Select directory'),
title: i18n.__('Select a folder to export the files to'),
multiSelections: false
}
dialog.showOpenDialog(remote.getCurrentWindow(), options,
(paths) => {
if (paths && paths.length === 1) {
const { storage, dispatch } = this.props
dataApi
.exportStorage(storage.key, fileType, paths[0])
.then(data => {
dispatch({
type: 'EXPORT_STORAGE',
storage: data.storage,
fileType: data.fileType
})
})
}
})
}

handleToggleButtonClick (e) {
const { storage, dispatch } = this.props
const isOpen = !this.state.isOpen
Expand Down
63 changes: 63 additions & 0 deletions browser/main/lib/dataApi/exportStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { findStorage } from 'browser/lib/findStorage'
import resolveStorageData from './resolveStorageData'
import resolveStorageNotes from './resolveStorageNotes'
import filenamify from 'filenamify'
import * as path from 'path'
import * as fs from 'fs'

/**
* @param {String} storageKey
* @param {String} fileType
* @param {String} exportDir
*
* @return {Object}
* ```
* {
* storage: Object,
* fileType: String,
* exportDir: String
* }
* ```
*/

function exportStorage (storageKey, fileType, exportDir) {
let targetStorage
try {
targetStorage = findStorage(storageKey)
} catch (e) {
return Promise.reject(e)
}

return resolveStorageData(targetStorage)
.then(storage => (
resolveStorageNotes(storage).then(notes => ({storage, notes}))
))
.then(function exportNotes (data) {
const { storage, notes } = data
const folderNamesMapping = {}
storage.folders.forEach(folder => {
const folderExportedDir = path.join(exportDir, filenamify(folder.name, {replacement: '_'}))
folderNamesMapping[folder.key] = folderExportedDir
// make sure directory exists
try {
fs.mkdirSync(folderExportedDir)
} catch (e) {}
})
notes
.filter(note => !note.isTrashed && note.type === 'MARKDOWN_NOTE')
.forEach(markdownNote => {
const folderExportedDir = folderNamesMapping[markdownNote.folder]
const snippetName = `${filenamify(markdownNote.title, {replacement: '_'})}.${fileType}`
const notePath = path.join(folderExportedDir, snippetName)
fs.writeFileSync(notePath, markdownNote.content)
})

return {
storage,
fileType,
exportDir
}
})
}

module.exports = exportStorage
1 change: 1 addition & 0 deletions browser/main/lib/dataApi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const dataApi = {
deleteFolder: require('./deleteFolder'),
reorderFolder: require('./reorderFolder'),
exportFolder: require('./exportFolder'),
exportStorage: require('./exportStorage'),
createNote: require('./createNote'),
updateNote: require('./updateNote'),
deleteNote: require('./deleteNote'),
Expand Down
15 changes: 2 additions & 13 deletions browser/main/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,10 @@ function data (state = defaultDataMap(), action) {
return state
}
case 'UPDATE_FOLDER':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage)
return state
case 'REORDER_FOLDER':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage)
return state
case 'EXPORT_FOLDER':
case 'RENAME_STORAGE':
case 'EXPORT_STORAGE':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage)
Expand Down Expand Up @@ -355,11 +349,6 @@ function data (state = defaultDataMap(), action) {
})
}
return state
case 'RENAME_STORAGE':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
state.storageMap.set(action.storage.key, action.storage)
return state
case 'EXPAND_STORAGE':
state = Object.assign({}, state)
state.storageMap = new Map(state.storageMap)
Expand Down
52 changes: 52 additions & 0 deletions tests/dataApi/exportStorage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const test = require('ava')
const exportStorage = require('browser/main/lib/dataApi/exportStorage')

global.document = require('jsdom').jsdom('<body></body>')
global.window = document.defaultView
global.navigator = window.navigator

const Storage = require('dom-storage')
const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true })
const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const os = require('os')
const fs = require('fs')
const sander = require('sander')

test.beforeEach(t => {
t.context.storageDir = path.join(os.tmpdir(), 'test/export-storage')
t.context.storage = TestDummy.dummyStorage(t.context.storageDir)
t.context.exportDir = path.join(os.tmpdir(), 'test/export-storage-output')
try { fs.mkdirSync(t.context.exportDir) } catch (e) {}
localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
})

test.serial('Export a storage', t => {
const storageKey = t.context.storage.cache.key
const folders = t.context.storage.json.folders
const notes = t.context.storage.notes
const exportDir = t.context.exportDir
const folderKeyToName = folders.reduce(
(acc, folder) => {
acc[folder.key] = folder.name
return acc
}, {})
return exportStorage(storageKey, 'md', exportDir)
.then(() => {
notes.forEach(note => {
const noteDir = path.join(exportDir, folderKeyToName[note.folder], `${note.title}.md`)
if (note.type === 'MARKDOWN_NOTE') {
t.true(fs.existsSync(noteDir))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check note contents too. Not just check the existence of the files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Rokt33r , done it!

t.is(fs.readFileSync(noteDir, 'utf8'), note.content)
} else if (note.type === 'SNIPPET_NOTE') {
t.false(fs.existsSync(noteDir))
}
})
})
})

test.afterEach.always(t => {
localStorage.clear()
sander.rimrafSync(t.context.storageDir)
sander.rimrafSync(t.context.exportDir)
})