1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

renew addStorage

This commit is contained in:
Dick Choi
2016-08-27 15:02:00 +09:00
parent db3a4d0f01
commit 5558403358
2 changed files with 41 additions and 124 deletions

View File

@@ -1,11 +1,7 @@
const _ = require('lodash')
const keygen = require('browser/lib/keygen')
const sander = require('sander')
const path = require('path')
const defaultBoostnoteJSON = {
folders: []
}
const resolveStorageData = require('./resolveStorageData')
const resolveStorageNotes = require('./resolveStorageNotes')
/**
* @param {Object}
@@ -42,30 +38,8 @@ function addStorage (input) {
path: input.path
}
const boostnoteJSONPath = path.join(newStorage.path, 'boostnote.json')
return Promise.resolve(newStorage)
.then(function resolveBoostnoteJSON () {
return sander.readFile(boostnoteJSONPath)
.then(function checkBoostnoteJSONExists (data) {
let parsedData = JSON.parse(data.toString())
if (!_.isArray(parsedData.folders)) throw new Error('`folders` must be array.')
newStorage.folders = parsedData.folders
.filter(function takeOnlyValidKey (folder) {
return _.isString(folder.key)
})
return newStorage
})
.catch(function tryToRewriteNewBoostnoteJSON (err) {
return sander
.writeFile(boostnoteJSONPath, JSON.stringify(defaultBoostnoteJSON))
.then(function () {
newStorage.folders = defaultBoostnoteJSON.folders
return newStorage
})
})
})
.then(resolveStorageData)
.then(function saveMetadataToLocalStorage () {
rawStorages.push({
key: newStorage.key,
@@ -75,36 +49,9 @@ function addStorage (input) {
})
localStorage.setItem('storages', JSON.stringify(rawStorages))
return newStorage
})
.then(function fetchNotes () {
var folderNotes = newStorage.folders
.map(function fetchNotesFromEachFolder (folder) {
var folderDataJSONPath = path.join(newStorage.path, folder.key, 'data.json')
return sander.readFile(folderDataJSONPath)
.then(function parseData (rawData) {
return JSON.parse(rawData)
})
.then(function validateNotes (data) {
if (!_.isArray(data.notes)) throw new Error('Invalid data.json')
return data.notes
.map(function (note) {
note.folder = folder.key
note.storage = newStorage.key
return note
})
})
.catch(function rewriteNotes (err) {
console.error(err)
return []
})
})
return Promise.all(folderNotes)
.then(function reduceFolderNotes (folderNotes) {
return folderNotes.reduce(function (sum, notes) {
return sum.concat(notes)
}, [])
})
})
.then(resolveStorageNotes)
.then(function returnValue (notes) {
return {
storage: newStorage,

View File

@@ -8,86 +8,56 @@ 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 sander = require('sander')
const _ = require('lodash')
const os = require('os')
function copyFile (filePath, targetPath) {
return sander.readFile(filePath)
.then(function writeFile (data) {
return sander.writeFile(targetPath, data.toString())
})
}
const v1StoragePath = path.join(os.tmpdir(), 'test/addStorage-v1-storage')
// const legacyStoragePath = path.join(os.tmpdir(), 'test/addStorage-legacy-storage')
// const emptyDirPath = path.join(os.tmpdir(), 'test/addStorage-empty-storage')
test('add a initialized storage', (t) => {
const dummyStoragePath = path.join(__dirname, '../dummy/dummyStorage')
const targetPath = path.join(__dirname, '../sandbox/test-add-storage1')
test.beforeEach((t) => {
t.context.v1StorageData = TestDummy.dummyStorage(v1StoragePath)
// t.context.legacyStorageData = TestDummy.dummyLegacyStorage(legacyStoragePath)
localStorage.setItem('storages', JSON.stringify([]))
})
test.serial('add a initialized storage', (t) => {
const input = {
type: 'FILESYSTEM',
name: 'test-add-storage1',
path: targetPath
name: 'add-storage1',
path: v1StoragePath
}
return Promise.resolve()
.then(function before () {
localStorage.setItem('storages', JSON.stringify([]))
sander.rimrafSync(targetPath)
return copyFile(path.join(dummyStoragePath, 'boostnote.json'), path.join(targetPath, 'boostnote.json'))
.then(() => {
return copyFile(path.join(dummyStoragePath, 'fc6ba88e8ecf/data.json'), path.join(targetPath, 'fc6ba88e8ecf/data.json'))
})
})
.then(function doTest (data) {
.then(function doTest () {
return addStorage(input)
})
.then(function validateResult (data) {
const { storage, notes } = data
let { storage, notes } = data
// Check data.storage
t.true(_.isString(storage.key))
t.is(storage.name, 'test-add-storage1')
t.true(_.isArray(storage.folders))
t.is(storage.folders.length, 1)
t.true(_.isArray(notes))
t.is(notes.length, 2)
t.is(notes[0].folder, 'fc6ba88e8ecf')
t.is(notes[0].storage, storage.key)
})
.then(function after () {
localStorage.clear()
sander.rimrafSync(targetPath)
t.is(storage.name, input.name)
t.is(storage.type, input.type)
t.is(storage.path, input.path)
// Check data.notes
t.is(notes.length, t.context.v1StorageData.notes.length)
notes.forEach(function validateNote (note) {
t.is(note.storage, storage.key)
})
// Check localStorage
let cacheData = _.find(JSON.parse(localStorage.getItem('storages')), {key: data.storage.key})
t.is(cacheData.name, input.name)
t.is(cacheData.type, input.type)
t.is(cacheData.path, input.path)
})
})
test('add a fresh storage', (t) => {
const targetPath = path.join(__dirname, '../sandbox/test-add-storage2')
const input = {
type: 'FILESYSTEM',
name: 'test-add-storage2',
path: targetPath
}
return Promise.resolve()
.then(function before () {
localStorage.setItem('storages', JSON.stringify([]))
sander.rimrafSync(targetPath)
})
.then(function doTest (data) {
return addStorage(input)
})
.then(function validateResult (data) {
const { storage, notes } = data
t.true(_.isString(storage.key))
t.is(storage.name, 'test-add-storage2')
t.true(_.isArray(storage.folders))
t.is(storage.folders.length, 0)
t.true(_.isArray(notes))
t.is(notes.length, 0)
t.true(sander.statSync(path.join(targetPath, 'boostnote.json')).isFile())
})
.then(function after () {
localStorage.clear()
sander.rimrafSync(targetPath)
})
test.after.always(() => {
localStorage.clear()
sander.rimrafSync(v1StoragePath)
})