1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 10:16:26 +00:00

createFolder

This commit is contained in:
Dick Choi
2016-08-27 16:31:45 +09:00
parent 8d96368ea6
commit 67dd089e67
2 changed files with 39 additions and 75 deletions

View File

@@ -1,11 +1,8 @@
const _ = require('lodash') const _ = require('lodash')
const sander = require('sander')
const keygen = require('browser/lib/keygen') const keygen = require('browser/lib/keygen')
const path = require('path') const path = require('path')
const resolveStorageData = require('./resolveStorageData')
const defaultDataJSON = { const CSON = require('season')
notes: []
}
/** /**
* @param {String} storageKey * @param {String} storageKey
@@ -17,7 +14,12 @@ const defaultDataJSON = {
* } * }
* ``` * ```
* *
* @return {key} * @return {Object}
* ```
* {
* storage: Object
* }
* ```
*/ */
function createFolder (storageKey, input) { function createFolder (storageKey, input) {
let rawStorages let rawStorages
@@ -36,49 +38,24 @@ function createFolder (storageKey, input) {
return Promise.reject(e) return Promise.reject(e)
} }
const storageData = Object.assign({}, targetStorage) return resolveStorageData(targetStorage)
.then(function createFolder (storage) {
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() let key = keygen()
while (boostnoteJSON.folders.some((folder) => folder.key === key)) { while (storage.folders.some((folder) => folder.key === key)) {
key = keygen() key = keygen()
} }
let newFolder = { let newFolder = {
key, key,
color: input.color, color: input.color,
name: input.name name: input.name
} }
boostnoteJSON.folders.push(newFolder)
storageData.folders = boostnoteJSON.folders storage.folders.push(newFolder)
CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version']))
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 { return {
storage: storageData storage
} }
}) })
} }

View File

@@ -1,6 +1,5 @@
const test = require('ava') const test = require('ava')
const createFolder = require('browser/main/lib/dataApi/createFolder') const createFolder = require('browser/main/lib/dataApi/createFolder')
const sander = require('sander')
global.document = require('jsdom').jsdom('<body></body>') global.document = require('jsdom').jsdom('<body></body>')
global.window = document.defaultView global.window = document.defaultView
@@ -9,50 +8,38 @@ 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 _ = require('lodash') const _ = require('lodash')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const CSON = require('season')
function copyFile (filePath, targetPath) { const storagePath = path.join(os.tmpdir(), 'test/rename-storage')
return sander.readFile(filePath)
.then(function writeFile (data) {
return sander.writeFile(targetPath, data.toString())
})
}
const dummyStoragePath = path.join(__dirname, '..', 'dummy/dummyStorage') test.beforeEach((t) => {
const targetPath = path.join(__dirname, '../sandbox/test-add-folder') t.context.storage = TestDummy.dummyStorage(storagePath)
const dummyRawStorage = { localStorage.setItem('storages', JSON.stringify([t.context.storage.cache]))
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) => { test.serial('Create a folder', (t) => {
return createFolder(dummyRawStorage.key, input) const stoargeKey = t.context.storage.cache.key
const input = {
name: 'created',
color: '#ff5555'
}
return Promise.resolve()
.then(function doTest () {
return createFolder(stoargeKey, input)
})
.then(function assert (data) { .then(function assert (data) {
t.not(data.storage, null) t.true(_.find(data.storage.folders, input) != null)
let jsonData = CSON.readFileSync(path.join(data.storage.path, 'boostnote.json'))
let targetFolder = _.find(data.storage.folders, { console.log(path.join(data.storage.path, 'boostnote.json'))
name: input.name, t.true(_.find(jsonData.folders, input) != null)
color: input.color
})
t.not(targetFolder, null)
t.true(sander.statSync(path.join(targetPath, targetFolder.key)).isDirectory())
}) })
}) })
test.after.always(function () { test.after(function after () {
localStorage.clear() localStorage.clear()
sander.rimrafSync(targetPath) sander.rimrafSync(storagePath)
}) })