mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
renew init method
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const sander = require('sander')
|
const resolveStorageData = require('./resolveStorageData')
|
||||||
const path = require('path')
|
const resolveStorageNotes = require('./resolveStorageNotes')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {Object} all storages and notes
|
* @return {Object} all storages and notes
|
||||||
* ```
|
* ```
|
||||||
@@ -11,6 +10,11 @@ const path = require('path')
|
|||||||
* notes: [...]
|
* notes: [...]
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
|
*
|
||||||
|
* This method deals with 3 patterns.
|
||||||
|
* 1. v1
|
||||||
|
* 2. legacy
|
||||||
|
* 3. empty directory
|
||||||
*/
|
*/
|
||||||
function init () {
|
function init () {
|
||||||
let fetchStorages = function () {
|
let fetchStorages = function () {
|
||||||
@@ -19,67 +23,38 @@ function init () {
|
|||||||
rawStorages = JSON.parse(window.localStorage.getItem('storages'))
|
rawStorages = JSON.parse(window.localStorage.getItem('storages'))
|
||||||
if (!_.isArray(rawStorages)) throw new Error('Cached data is not valid.')
|
if (!_.isArray(rawStorages)) throw new Error('Cached data is not valid.')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.warn('Failed to parse cached data from localStorage', e)
|
||||||
rawStorages = []
|
rawStorages = []
|
||||||
window.localStorage.setItem('storages', JSON.stringify(rawStorages))
|
window.localStorage.setItem('storages', JSON.stringify(rawStorages))
|
||||||
}
|
}
|
||||||
return Promise.all(rawStorages
|
return Promise.all(rawStorages
|
||||||
.map(function assignFoldersToStorage (rawStorage) {
|
.map(resolveStorageData))
|
||||||
let data
|
|
||||||
let boostnoteJSONPath = path.join(rawStorage.path, 'boostnote.json')
|
|
||||||
try {
|
|
||||||
data = JSON.parse(sander.readFileSync(boostnoteJSONPath))
|
|
||||||
if (!_.isArray(data.folders)) throw new Error('folders should be an array.')
|
|
||||||
rawStorage.folders = data.folders
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code === 'ENOENT') {
|
|
||||||
console.warn('boostnote.json file doesn\'t exist the given path')
|
|
||||||
} else {
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
rawStorage.folders = []
|
|
||||||
}
|
|
||||||
return Promise.resolve(rawStorage)
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let fetchNotes = function (storages) {
|
let fetchNotes = function (storages) {
|
||||||
let notes = []
|
let findNotesFromEachStorage = storages
|
||||||
|
.map(resolveStorageNotes)
|
||||||
storages
|
return Promise.all(findNotesFromEachStorage)
|
||||||
.forEach((storage) => {
|
.then(function concatNoteGroup (noteGroups) {
|
||||||
storage.folders.forEach((folder) => {
|
return noteGroups.reduce(function (sum, group) {
|
||||||
let dataPath = path.join(storage.path, folder.key, 'data.json')
|
return sum.concat(group)
|
||||||
let data
|
}, [])
|
||||||
try {
|
})
|
||||||
data = JSON.parse(sander.readFileSync(dataPath))
|
.then(function returnData (notes) {
|
||||||
if (!_.isArray(data.notes)) throw new Error('notes should be an array.')
|
return {
|
||||||
} catch (e) {
|
storages,
|
||||||
// Remove folder if fetching failed.
|
notes
|
||||||
console.error('Failed to load data: %s', dataPath)
|
}
|
||||||
storage.folders = storage.folders.filter((_folder) => _folder.key !== folder.key)
|
|
||||||
data = {notes: []}
|
|
||||||
}
|
|
||||||
data.notes.forEach((note) => {
|
|
||||||
note.storage = storage.key
|
|
||||||
note.folder = folder.key
|
|
||||||
notes.push(note)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
return Promise.resolve({
|
|
||||||
storages,
|
|
||||||
notes
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(fetchStorages())
|
return Promise.resolve(fetchStorages())
|
||||||
.then((storages) => {
|
.then((storages) => {
|
||||||
storages = storages.filter((storage) => {
|
|
||||||
if (!_.isObject(storage)) return false
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return storages
|
return storages
|
||||||
|
.filter((storage) => {
|
||||||
|
if (!_.isObject(storage)) return false
|
||||||
|
return true
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.then(fetchNotes)
|
.then(fetchNotes)
|
||||||
}
|
}
|
||||||
|
|||||||
39
browser/main/lib/dataApi/resolveStorageData.js
Normal file
39
browser/main/lib/dataApi/resolveStorageData.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
const _ = require('lodash')
|
||||||
|
const path = require('path')
|
||||||
|
const CSON = require('season')
|
||||||
|
const transform = require('./transform')
|
||||||
|
|
||||||
|
function resolveStorageData (storageCache) {
|
||||||
|
let storage = {
|
||||||
|
key: storageCache.key,
|
||||||
|
name: storageCache.name,
|
||||||
|
type: storageCache.type,
|
||||||
|
path: storageCache.path
|
||||||
|
}
|
||||||
|
|
||||||
|
const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json')
|
||||||
|
try {
|
||||||
|
let jsonData = CSON.readFileSync(boostnoteJSONPath)
|
||||||
|
if (!_.isArray(jsonData.folders)) throw new Error('folders should be an array.')
|
||||||
|
storage.folders = jsonData.folders
|
||||||
|
storage.version = jsonData.version
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
console.warn('boostnote.json file doesn\'t exist the given path')
|
||||||
|
CSON.writeFileSync(boostnoteJSONPath, {folders: [], version: '1.0'})
|
||||||
|
} else {
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
storage.folders = []
|
||||||
|
storage.version = '1.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (storage.version === '1.0') {
|
||||||
|
return Promise.resolve(storage)
|
||||||
|
}
|
||||||
|
console.log('Transform Legacy storage', storage.path)
|
||||||
|
return transform(storage.path)
|
||||||
|
.then(() => storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = resolveStorageData
|
||||||
30
browser/main/lib/dataApi/resolveStorageNotes.js
Normal file
30
browser/main/lib/dataApi/resolveStorageNotes.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
const sander = require('sander')
|
||||||
|
const path = require('path')
|
||||||
|
const CSON = require('season')
|
||||||
|
|
||||||
|
function resolveStorageNotes (storage) {
|
||||||
|
const notesDirPath = path.join(storage.path, 'notes')
|
||||||
|
let notePathList
|
||||||
|
try {
|
||||||
|
notePathList = sander.readdirSync(notesDirPath)
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
console.log(notesDirPath, ' doesn\'t exist.')
|
||||||
|
sander.mkdirSync(notesDirPath)
|
||||||
|
} else {
|
||||||
|
console.warn('Failed to find note dir', notesDirPath, err)
|
||||||
|
}
|
||||||
|
notePathList = []
|
||||||
|
}
|
||||||
|
let notes = notePathList
|
||||||
|
.map(function parseCSONFile (notePath) {
|
||||||
|
let data = CSON.readFileSync(path.join(notesDirPath, notePath))
|
||||||
|
data.key = path.basename(notePath, '.cson')
|
||||||
|
data.storage = storage.key
|
||||||
|
return data
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.resolve(notes)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = resolveStorageNotes
|
||||||
@@ -8,68 +8,58 @@ 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 crypto = require('crypto')
|
const TestDummy = require('../fixtures/TestDummy')
|
||||||
|
const keygen = require('browser/lib/keygen')
|
||||||
|
const sander = require('sander')
|
||||||
|
const _ = require('lodash')
|
||||||
|
const os = require('os')
|
||||||
|
|
||||||
|
const v1StoragePath = path.join(os.tmpdir(), 'test/init-v1-storage')
|
||||||
|
const legacyStoragePath = path.join(os.tmpdir(), 'test/init-legacy-storage')
|
||||||
|
const emptyDirPath = path.join(os.tmpdir(), 'test/init-empty-storage')
|
||||||
|
|
||||||
|
test.beforeEach((t) => {
|
||||||
|
localStorage.clear()
|
||||||
|
// Prepare 3 types of dir
|
||||||
|
t.context.v1StorageData = TestDummy.dummyStorage(v1StoragePath, {cache: {name: 'v1'}})
|
||||||
|
t.context.legacyStorageData = TestDummy.dummyLegacyStorage(legacyStoragePath, {cache: {name: 'legacy'}})
|
||||||
|
t.context.emptyStorageData = {
|
||||||
|
cache: {
|
||||||
|
type: 'FILESYSTEM',
|
||||||
|
name: 'empty',
|
||||||
|
key: keygen(),
|
||||||
|
path: emptyDirPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem('storages', JSON.stringify([t.context.v1StorageData.cache, t.context.legacyStorageData.cache, t.context.emptyStorageData.cache]))
|
||||||
|
})
|
||||||
|
|
||||||
test.serial('Fetch storages and notes', (t) => {
|
test.serial('Fetch storages and notes', (t) => {
|
||||||
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage')
|
const { v1StorageData, legacyStorageData, emptyStorageData } = t.context
|
||||||
const dummyRawStorage = {
|
|
||||||
name: 'test1',
|
|
||||||
key: crypto.randomBytes(6).toString('hex'),
|
|
||||||
path: dummyStoragePath
|
|
||||||
}
|
|
||||||
const dummyFolderKey = 'fc6ba88e8ecf'
|
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(function before () {
|
|
||||||
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
|
|
||||||
})
|
|
||||||
.then(function test () {
|
.then(function test () {
|
||||||
return init()
|
return init()
|
||||||
})
|
})
|
||||||
.then(function assert (data) {
|
.then(function assert (data) {
|
||||||
t.true(Array.isArray(data.storages))
|
t.true(Array.isArray(data.storages))
|
||||||
var targetStorage = data.storages.filter((storage) => storage.key === dummyRawStorage.key)[0]
|
t.is(data.notes.length, v1StorageData.notes.length + legacyStorageData.notes.length)
|
||||||
t.not(targetStorage, null)
|
t.is(data.storages.length, 3)
|
||||||
t.is(targetStorage.name, dummyRawStorage.name)
|
data.storages.forEach(function assertStorage (storage) {
|
||||||
t.is(targetStorage.key, dummyRawStorage.key)
|
t.true(_.isString(storage.key))
|
||||||
t.is(targetStorage.path, dummyRawStorage.path)
|
t.true(_.isString(storage.name))
|
||||||
t.is(data.notes.length, 2)
|
t.true(storage.type === 'FILESYSTEM')
|
||||||
data.notes.forEach((note) => {
|
t.true(_.isString(storage.path))
|
||||||
t.is(note.folder, dummyFolderKey)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.true(Array.isArray(data.notes))
|
|
||||||
})
|
})
|
||||||
.then(function after () {
|
.then(function after () {
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test.serial('If storage path is a empty folder, return metadata with empty folder array and empty note array.', (t) => {
|
test.after.always(() => {
|
||||||
const emptyFolderPath = path.join(__dirname, '..', 'dummy/empty')
|
localStorage.clear()
|
||||||
const dummyRawStorage = {
|
sander.rimrafSync(v1StoragePath)
|
||||||
name: 'test2',
|
sander.rimrafSync(legacyStoragePath)
|
||||||
key: crypto.randomBytes(6).toString('hex'),
|
sander.rimrafSync(emptyDirPath)
|
||||||
path: emptyFolderPath
|
|
||||||
}
|
|
||||||
return Promise.resolve()
|
|
||||||
.then(function before () {
|
|
||||||
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
|
|
||||||
})
|
|
||||||
.then(function test () {
|
|
||||||
return init()
|
|
||||||
})
|
|
||||||
.then(function assert (data) {
|
|
||||||
t.true(Array.isArray(data.storages))
|
|
||||||
var targetStorage = data.storages.filter((storage) => storage.key === dummyRawStorage.key)[0]
|
|
||||||
t.not(targetStorage, null)
|
|
||||||
t.is(targetStorage.name, dummyRawStorage.name)
|
|
||||||
t.is(targetStorage.key, dummyRawStorage.key)
|
|
||||||
t.is(targetStorage.path, dummyRawStorage.path)
|
|
||||||
|
|
||||||
t.true(Array.isArray(data.notes))
|
|
||||||
})
|
|
||||||
.then(function after () {
|
|
||||||
localStorage.clear()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user