mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-15 10:46:32 +00:00
extract createFolder method
This commit is contained in:
86
browser/main/lib/dataApi/createFolder.js
Normal file
86
browser/main/lib/dataApi/createFolder.js
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
const _ = require('lodash')
|
||||||
|
const sander = require('sander')
|
||||||
|
const keygen = require('browser/lib/keygen')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
const defaultDataJSON = {
|
||||||
|
notes: []
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {String} storageKey
|
||||||
|
* @param {Object} input
|
||||||
|
* ```
|
||||||
|
* {
|
||||||
|
* color: String,
|
||||||
|
* name: String
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @return {key}
|
||||||
|
*/
|
||||||
|
function createFolder (storageKey, input) {
|
||||||
|
let rawStorages
|
||||||
|
let targetStorage
|
||||||
|
try {
|
||||||
|
if (input == null) throw new Error('No input found.')
|
||||||
|
if (!_.isString(input.name)) throw new Error('Name must be a string.')
|
||||||
|
if (!_.isString(input.color)) throw new Error('Color must be a string.')
|
||||||
|
|
||||||
|
rawStorages = JSON.parse(localStorage.getItem('storages'))
|
||||||
|
if (!_.isArray(rawStorages)) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
|
||||||
|
targetStorage = _.find(rawStorages, {key: storageKey})
|
||||||
|
if (targetStorage == null) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
} catch (e) {
|
||||||
|
return Promise.reject(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
const storageData = Object.assign({}, targetStorage)
|
||||||
|
|
||||||
|
const boostnoteJSONPath = path.join(targetStorage.path, 'boostnote.json')
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(function fetchBoostnoteJSON () {
|
||||||
|
return sander.readFile(boostnoteJSONPath)
|
||||||
|
})
|
||||||
|
.then(function updateBoostnoteJSON (data) {
|
||||||
|
let boostnoteJSON
|
||||||
|
// If `boostnote.json` is invalid, reset `boostnote.json`.
|
||||||
|
try {
|
||||||
|
boostnoteJSON = JSON.parse(data)
|
||||||
|
if (!_.isArray(boostnoteJSON.folders)) throw new Error('the value of `folders` must be array')
|
||||||
|
} catch (err) {
|
||||||
|
boostnoteJSON = {
|
||||||
|
folders: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = keygen()
|
||||||
|
while (boostnoteJSON.folders.some((folder) => folder.key === key)) {
|
||||||
|
key = keygen()
|
||||||
|
}
|
||||||
|
|
||||||
|
let newFolder = {
|
||||||
|
key,
|
||||||
|
color: input.color,
|
||||||
|
name: input.name
|
||||||
|
}
|
||||||
|
boostnoteJSON.folders.push(newFolder)
|
||||||
|
|
||||||
|
storageData.folders = boostnoteJSON.folders
|
||||||
|
|
||||||
|
return sander.writeFile(boostnoteJSONPath, JSON.stringify(boostnoteJSON))
|
||||||
|
.then(() => newFolder)
|
||||||
|
})
|
||||||
|
.then(function createDataJSON (newFolder) {
|
||||||
|
const folderDirPath = path.join(targetStorage.path, newFolder.key)
|
||||||
|
return sander.writeFile(folderDirPath, 'data.json', JSON.stringify(defaultDataJSON))
|
||||||
|
})
|
||||||
|
.then(function returnData () {
|
||||||
|
return {
|
||||||
|
storage: storageData
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createFolder
|
||||||
58
tests/dataApi/createFolder.js
Normal file
58
tests/dataApi/createFolder.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
const test = require('ava')
|
||||||
|
const createFolder = require('browser/main/lib/dataApi/createFolder')
|
||||||
|
const sander = require('sander')
|
||||||
|
|
||||||
|
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 crypto = require('crypto')
|
||||||
|
const _ = require('lodash')
|
||||||
|
|
||||||
|
function copyFile (filePath, targetPath) {
|
||||||
|
return sander.readFile(filePath)
|
||||||
|
.then(function writeFile (data) {
|
||||||
|
return sander.writeFile(targetPath, data.toString())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage')
|
||||||
|
const targetPath = path.join(__dirname, '../sandbox/test-add-folder')
|
||||||
|
const dummyRawStorage = {
|
||||||
|
name: 'test1',
|
||||||
|
key: crypto.randomBytes(6).toString('hex'),
|
||||||
|
path: targetPath
|
||||||
|
}
|
||||||
|
test.before(function () {
|
||||||
|
localStorage.setItem('storages', JSON.stringify([dummyRawStorage]))
|
||||||
|
|
||||||
|
return copyFile(path.join(dummyStoragePath, 'boostnote.json'), path.join(targetPath, 'boostnote.json'))
|
||||||
|
})
|
||||||
|
const input = {
|
||||||
|
name: 'test folder',
|
||||||
|
color: '#FF5555'
|
||||||
|
}
|
||||||
|
|
||||||
|
test('Add note to storage', (t) => {
|
||||||
|
return createFolder(dummyRawStorage.key, input)
|
||||||
|
.then(function assert (data) {
|
||||||
|
t.not(data.storage, null)
|
||||||
|
|
||||||
|
let targetFolder = _.find(data.storage.folders, {
|
||||||
|
name: input.name,
|
||||||
|
color: input.color
|
||||||
|
})
|
||||||
|
|
||||||
|
t.not(targetFolder, null)
|
||||||
|
|
||||||
|
t.true(sander.statSync(path.join(targetPath, targetFolder.key)).isDirectory())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.after.always(function () {
|
||||||
|
localStorage.clear()
|
||||||
|
sander.rimrafSync(targetPath)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user