.then(function saveNote (storage) { let key = keygen() let isUnique = false while (!isUnique) { try { sander.statSync(path.join(storage.path, 'notes', key + '.cson')) key = keygen() } catch (err) { if (err.code === 'ENOENT') { isUnique = true } else { throw err } } } let noteData = Object.assign({}, input, { key, createdAt: new Date(), updatedAt: new Date(), storage: storageKey }) CSON.writeFileSync(path.join(storage.path, 'notes', key + '.cson'), _.omit(noteData, ['key', 'storage'])) return noteData })
function resolveStorageData (storageCache) { const storage = { key: storageCache.key, name: storageCache.name, type: storageCache.type, path: storageCache.path, isOpen: storageCache.isOpen } const boostnoteJSONPath = path.join(storageCache.path, 'boostnote.json') try { const 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' } const version = parseInt(storage.version, 10) if (version >= 1) { return Promise.resolve(storage) } return migrateFromV6Storage(storage.path) .then(() => storage) }
.then(function (storage) { CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) return { storage, folderKey } })
.then(function reorderFolder (storage) { storage.folders = _.move(storage.folders, oldIndex, newIndex) CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) return { storage } })
.then(function updateFolder (storage) { let targetFolder = _.find(storage.folders, {key: folderKey}) if (targetFolder == null) throw new Error('Target folder doesn\'t exist.') targetFolder.name = input.name targetFolder.color = input.color CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) return { storage } })
.then((notes) => { let unknownCount = 0 notes.forEach((note) => { if (!storage.folders.some((folder) => note.folder === folder.key)) { unknownCount++ storage.folders.push({ key: note.folder, color: consts.FOLDER_COLORS[(unknownCount - 1) % 7], name: 'Unknown ' + unknownCount }) } }) if (unknownCount > 0) { CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['folders', 'version'])) } return notes })
.then(function saveNote (storage) { let noteData let notePath = path.join(storage.path, 'notes', noteKey + '.cson') try { noteData = CSON.readFileSync(notePath) } catch (err) { console.warn('Failed to find note cson', err) noteData = input.type === 'SNIPPET_NOTE' ? { type: 'SNIPPET_NOTE', description: [], snippets: [{ name: '', mode: 'text', content: '' }] } : { type: 'MARKDOWN_NOTE', content: '' } noteData.title = '' if (storage.folders.length === 0) throw new Error('Failed to restore note: No folder exists.') noteData.folder = storage.folders[0].key noteData.createdAt = new Date() noteData.updatedAt = new Date() noteData.isStarred = false noteData.tags = [] } if (noteData.type === 'SNIPPET_NOTE') { noteData.title } Object.assign(noteData, input, { key: noteKey, updatedAt: new Date(), storage: storageKey }) CSON.writeFileSync(path.join(storage.path, 'notes', noteKey + '.cson'), _.omit(noteData, ['key', 'storage'])) return noteData })
function dummyLegacyStorage (storagePath, override = {}) { var jsonData = override.json != null ? override.json : dummyBoostnoteJSONData({}, true) var cacheData = override.cache != null ? override.cache : {} if (cacheData.key == null) cacheData.key = keygen() if (cacheData.name == null) cacheData.name = faker.random.word() if (cacheData.type == null) cacheData.type = 'FILESYSTEM' cacheData.path = storagePath sander.writeFileSync(path.join(storagePath, 'boostnote.json'), JSON.stringify(jsonData)) var notesData = [] for (var j = 0; j < jsonData.folders.length; j++) { var folderNotes = [] var noteCount = Math.floor((Math.random() * 5)) + 1 for (var i = 0; i < noteCount; i++) { var key = keygen(true) while (folderNotes.some((note) => note.key === key)) { key = keygen(true) } var noteData = dummyNote({ key, folder: jsonData.folders[j].key }) folderNotes.push(noteData) } notesData = notesData.concat(folderNotes) CSON.writeFileSync(path.join(storagePath, jsonData.folders[j].key, 'data.json'), {notes: folderNotes.map((note) => _.omit(note, ['folder']))}) } return { json: jsonData, cache: cacheData, notes: notesData } }
.map(function saveNote (note) { CSON.writeFileSync(path.join(noteDirPath, note.key) + '.cson', note) })
notesData.forEach(function saveNoteCSON (note) { CSON.writeFileSync(path.join(storagePath, 'notes', note.key + '.cson'), _.omit(note, ['key'])) })