mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-14 10:16:26 +00:00
renew addStorage
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const keygen = require('browser/lib/keygen')
|
const keygen = require('browser/lib/keygen')
|
||||||
const sander = require('sander')
|
const resolveStorageData = require('./resolveStorageData')
|
||||||
const path = require('path')
|
const resolveStorageNotes = require('./resolveStorageNotes')
|
||||||
|
|
||||||
const defaultBoostnoteJSON = {
|
|
||||||
folders: []
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object}
|
* @param {Object}
|
||||||
@@ -42,30 +38,8 @@ function addStorage (input) {
|
|||||||
path: input.path
|
path: input.path
|
||||||
}
|
}
|
||||||
|
|
||||||
const boostnoteJSONPath = path.join(newStorage.path, 'boostnote.json')
|
|
||||||
|
|
||||||
return Promise.resolve(newStorage)
|
return Promise.resolve(newStorage)
|
||||||
.then(function resolveBoostnoteJSON () {
|
.then(resolveStorageData)
|
||||||
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(function saveMetadataToLocalStorage () {
|
.then(function saveMetadataToLocalStorage () {
|
||||||
rawStorages.push({
|
rawStorages.push({
|
||||||
key: newStorage.key,
|
key: newStorage.key,
|
||||||
@@ -75,36 +49,9 @@ function addStorage (input) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
localStorage.setItem('storages', JSON.stringify(rawStorages))
|
localStorage.setItem('storages', JSON.stringify(rawStorages))
|
||||||
|
return newStorage
|
||||||
})
|
})
|
||||||
.then(function fetchNotes () {
|
.then(resolveStorageNotes)
|
||||||
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(function returnValue (notes) {
|
.then(function returnValue (notes) {
|
||||||
return {
|
return {
|
||||||
storage: newStorage,
|
storage: newStorage,
|
||||||
|
|||||||
@@ -8,86 +8,56 @@ global.navigator = window.navigator
|
|||||||
const Storage = require('dom-storage')
|
const Storage = require('dom-storage')
|
||||||
const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true })
|
const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true })
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const TestDummy = require('../fixtures/TestDummy')
|
||||||
const sander = require('sander')
|
const sander = require('sander')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
const os = require('os')
|
||||||
|
|
||||||
function copyFile (filePath, targetPath) {
|
const v1StoragePath = path.join(os.tmpdir(), 'test/addStorage-v1-storage')
|
||||||
return sander.readFile(filePath)
|
// const legacyStoragePath = path.join(os.tmpdir(), 'test/addStorage-legacy-storage')
|
||||||
.then(function writeFile (data) {
|
// const emptyDirPath = path.join(os.tmpdir(), 'test/addStorage-empty-storage')
|
||||||
return sander.writeFile(targetPath, data.toString())
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
t.context.v1StorageData = TestDummy.dummyStorage(v1StoragePath)
|
||||||
|
// t.context.legacyStorageData = TestDummy.dummyLegacyStorage(legacyStoragePath)
|
||||||
|
|
||||||
|
localStorage.setItem('storages', JSON.stringify([]))
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
test('add a initialized storage', (t) => {
|
test.serial('add a initialized storage', (t) => {
|
||||||
const dummyStoragePath = path.join(__dirname, '../dummy/dummyStorage')
|
|
||||||
const targetPath = path.join(__dirname, '../sandbox/test-add-storage1')
|
|
||||||
const input = {
|
const input = {
|
||||||
type: 'FILESYSTEM',
|
type: 'FILESYSTEM',
|
||||||
name: 'test-add-storage1',
|
name: 'add-storage1',
|
||||||
path: targetPath
|
path: v1StoragePath
|
||||||
}
|
}
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(function before () {
|
.then(function doTest () {
|
||||||
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) {
|
|
||||||
return addStorage(input)
|
return addStorage(input)
|
||||||
})
|
})
|
||||||
.then(function validateResult (data) {
|
.then(function validateResult (data) {
|
||||||
const { storage, notes } = data
|
let { storage, notes } = data
|
||||||
|
|
||||||
|
// Check data.storage
|
||||||
t.true(_.isString(storage.key))
|
t.true(_.isString(storage.key))
|
||||||
t.is(storage.name, 'test-add-storage1')
|
t.is(storage.name, input.name)
|
||||||
t.true(_.isArray(storage.folders))
|
t.is(storage.type, input.type)
|
||||||
t.is(storage.folders.length, 1)
|
t.is(storage.path, input.path)
|
||||||
t.true(_.isArray(notes))
|
|
||||||
t.is(notes.length, 2)
|
// Check data.notes
|
||||||
t.is(notes[0].folder, 'fc6ba88e8ecf')
|
t.is(notes.length, t.context.v1StorageData.notes.length)
|
||||||
t.is(notes[0].storage, storage.key)
|
notes.forEach(function validateNote (note) {
|
||||||
|
t.is(note.storage, storage.key)
|
||||||
})
|
})
|
||||||
.then(function after () {
|
|
||||||
|
// 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.after.always(() => {
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
sander.rimrafSync(targetPath)
|
sander.rimrafSync(v1StoragePath)
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user