Skip to content

Commit a0f92b9

Browse files
committed
electron: rename databases directory.
1 parent 903d7cf commit a0f92b9

6 files changed

Lines changed: 50 additions & 37 deletions

File tree

src/main/Collaboration.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Collaboration.prototype.purgeSettings = async function () {
4747

4848
const removeSharedLayers = () => sharedProjects.map(async (project) => {
4949
try {
50-
const location = path.join(paths.databases(app), projectUUID(project))
50+
const location = path.join(paths.databases, projectUUID(project))
5151
console.log(`Purging collaboration settings from ${project.id} located at ${location}`)
5252
const db = L.leveldb({ location })
5353

@@ -87,4 +87,3 @@ Collaboration.prototype.purgeSettings = async function () {
8787

8888
console.log(`Purged collaboration settings from ${sharedProjects.length} shared projects.`)
8989
}
90-

src/main/WindowManager.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import util from 'util'
2-
import { BrowserWindow, app } from 'electron'
2+
import { BrowserWindow } from 'electron'
33
import * as paths from './paths'
44
import Emitter from '../shared/emitter'
55

6-
const url = app => {
6+
const url = (() => {
77
const notCold = process.argv.indexOf('--cold') === -1
88
const hot = process.defaultApp ||
99
/[\\/]electron-prebuilt[\\/]/.test(paths.execPath) ||
1010
/[\\/]electron[\\/]/.test(paths.execPath)
1111

1212
return (hot && notCold)
1313
? new URL('index.html', 'http://localhost:8080')
14-
: new URL(paths.staticIndexPage(app), 'file:')
15-
}
14+
: new URL(paths.staticIndexPage, 'file:')
15+
})()
1616

1717

1818
/**
@@ -188,13 +188,13 @@ WindowManager.prototype.reloadAll = function () {
188188
WindowManager.prototype.showProject = function (key, project) {
189189
const additionalArguments = [
190190
`--page=${key}`,
191-
`--databases=${paths.databases(app)}`
191+
`--databases=${paths.databases}`
192192
]
193193

194194
return this.createWindow({
195195
handle: key,
196196
title: project.name,
197-
url: url(app),
197+
url,
198198
...project.bounds,
199199
additionalArguments
200200
})
@@ -211,7 +211,7 @@ WindowManager.prototype.showSplash = function () {
211211
return this.createWindow({
212212
handle: 'splash',
213213
title: 'ODIN - Projects',
214-
url: url(app),
214+
url,
215215
additionalArguments
216216
})
217217
}
@@ -221,7 +221,7 @@ WindowManager.prototype.showLogin = function () {
221221
return this.createWindow({
222222
handle: 'login',
223223
title: 'Login and enable collaboration',
224-
url: url(app),
224+
url,
225225
minimizable: false,
226226
maximizable: false,
227227
resizable: true,
@@ -238,7 +238,7 @@ WindowManager.prototype.showLogout = function () {
238238
return this.createWindow({
239239
handle: 'logout',
240240
title: 'Logout and disable collaboration',
241-
url: url(app),
241+
url,
242242
minimizable: false,
243243
maximizable: false,
244244
resizable: true,
@@ -249,4 +249,3 @@ WindowManager.prototype.showLogout = function () {
249249
additionalArguments
250250
})
251251
}
252-

src/main/ipc.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import path from 'path'
22
import { promises as fs } from 'fs'
33
import { leveldb, sessionDB } from '../shared/level'
4+
import * as paths from './paths'
45

5-
export const ipc = (databases, ipcMain, projectStore) => {
6+
export const ipc = (ipcMain, projectStore) => {
67

78
ipcMain.handle('ipc:get:project', (_, id) => {
89
return projectStore.getProject(id)
@@ -23,7 +24,7 @@ export const ipc = (databases, ipcMain, projectStore) => {
2324
ipcMain.handle('ipc:post:project', async (_, project) => {
2425
// Create and close project database:
2526
const uuid = project.id.split(':')[1]
26-
const location = path.join(databases, uuid)
27+
const location = path.join(paths.databases, uuid)
2728
const db = leveldb({ location })
2829
await db.close()
2930

@@ -33,8 +34,8 @@ export const ipc = (databases, ipcMain, projectStore) => {
3334
ipcMain.handle('ipc:delete:project', async (_, id) => {
3435
// Delete project database:
3536
const uuid = id.split(':')[1]
36-
const location = path.join(databases, uuid)
37-
await fs.rmdir(location, { recursive: true })
37+
const location = path.join(paths.databases, uuid)
38+
await fs.rm(location, { recursive: true })
3839

3940
return projectStore.deleteProject(id)
4041
})
@@ -70,7 +71,7 @@ export const ipc = (databases, ipcMain, projectStore) => {
7071
ipcMain.handle('ipc:put:project:replication/seed', async (_, id, seed) => {
7172
try {
7273
const uuid = id.split(':')[1]
73-
const location = path.join(databases, uuid)
74+
const location = path.join(paths.databases, uuid)
7475
const db = leveldb({ location })
7576
const session = sessionDB(db)
7677
await session.put('replication:seed', seed)

src/main/legacy/transfer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path'
22
import { readSources } from './io'
33
import { readProjects } from './projects'
44
import * as L from '../../shared/level'
5+
import * as paths from '../paths'
56

67

78
/**
@@ -63,14 +64,14 @@ export const transferProject = async (db, project) => {
6364
* @param {Master} legacyStore master/main database
6465
* @param {String} databases directory to store project databases
6566
*/
66-
export const transferLegacy = async (location, legacyStore, databases) => {
67+
export const transferLegacy = async (location, legacyStore) => {
6768
await legacyStore.transferSources(await readSources(location))
6869
const projects = await readProjects(location)
6970
await legacyStore.transferMetadata(projects)
7071

7172
await Promise.all(projects.map(async project => {
7273
const uuid = project.id.split(':')[1]
73-
const location = path.join(databases, uuid)
74+
const location = path.join(paths.databases, uuid)
7475
const db = L.leveldb({ location })
7576
await transferProject(db, project)
7677
return db.close()

src/main/main.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ import { isEnabled } from './environment'
2121
*/
2222
const ready = async () => {
2323
// read environment variables from .env file and add to process.env
24-
console.log(`looking for .env file ${paths.dotenv(app)}`)
25-
dotenv.config({ debug: true, path: paths.dotenv(app) })
26-
27-
// loadReactChromeExtension()
24+
dotenv.config({ debug: false, quiet: true, path: paths.dotenv })
2825

2926
// Open/create master database.
30-
const databases = paths.databases(app)
31-
console.log('databases directory:', databases)
32-
paths.mkdir(databases)
33-
const db = leveldb({ location: paths.master(app), encoding: 'json' })
27+
paths.initStorageLocation()
28+
const db = leveldb({ location: paths.master, encoding: 'json' })
3429

3530
/* eslint-disable no-new */
3631
new IPCServer(db, ipcMain)
@@ -39,12 +34,14 @@ const ready = async () => {
3934
const projectStore = new ProjectStore(db)
4035
const sessionStore = new SessionStore(db)
4136
const legacyStore = new LegacyStore(db)
42-
ipc(databases, ipcMain, projectStore)
37+
38+
ipc(ipcMain, projectStore)
4339

4440
// Transfer legacy data if not already done.
4541
if (await legacyStore.getTransferred() === false) {
4642
const location = paths.odinHome
47-
await transferLegacy(location, legacyStore, databases)
43+
// TODO: remove databases parameter and directly use paths instead
44+
await transferLegacy(location, legacyStore)
4845
}
4946

5047
const windowManager = new WindowManager()

src/main/paths.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import path from 'path'
66
import fs from 'fs'
77
import os from 'os'
8+
import { app } from 'electron'
89

910
export const execPath = process.execPath
1011

@@ -18,36 +19,40 @@ export const execPath = process.execPath
1819
* Note: Currently unused; included only for documentation purpose.
1920
*/
2021
/* eslint-disable no-unused-vars */
21-
const appData = app => app.getPath('appData')
22+
const appData = app.getPath('appData')
2223
/* eslint-enable no-unused-vars */
2324

2425
/**
2526
* The directory for storing your app's configuration files,
2627
* which by default it is the appData directory appended
2728
* with your app's name.
29+
*
30+
* NOTE: Starting with v32.x Electron unconditionally deletes 'databases' directory
31+
* where we used to store all databases.
32+
* REFERENCE: https://github.com/electron/electron/issues/45396
2833
*/
29-
const userData = app => app.getPath('userData')
34+
const userData = app.getPath('userData')
3035

3136
/**
3237
* The current application directory.
3338
*/
34-
const appPath = app => app.getAppPath()
39+
const appPath = app.getAppPath()
3540

3641
/**
3742
* Directory to store all main/renderer databases.
3843
*/
39-
export const databases = app => path.join(userData(app), 'databases')
44+
export const databases = path.join(userData, 'leveldb')
45+
const databasesLegacy = path.join(userData, 'databases')
4046

4147
/**
4248
* Directory to store main/master database.
4349
*/
44-
export const master = app => path.join(databases(app), 'master')
50+
export const master = path.join(databases, 'master')
4551

4652
/**
4753
* .env file for providing user related environment settings
48-
*
4954
*/
50-
export const dotenv = app => path.join(userData(app), '.env')
55+
export const dotenv = path.join(userData, '.env')
5156

5257
/**
5358
* (Recusively) create directory for given path.
@@ -95,4 +100,15 @@ export const metadata = (location, uuid) => path.join(projects(location), uuid,
95100
*/
96101
export const preferences = (location, uuid) => path.join(projects(location), uuid, 'preferences.json')
97102

98-
export const staticIndexPage = app => path.join(appPath(app), 'dist', 'index.html')
103+
export const staticIndexPage = path.join(appPath, 'dist', 'index.html')
104+
105+
/**
106+
*
107+
*/
108+
export const initStorageLocation = () => {
109+
if (fs.existsSync(databasesLegacy)) {
110+
fs.renameSync(databasesLegacy, databases)
111+
} else if (!fs.existsSync(databases)) {
112+
mkdir(databases)
113+
}
114+
}

0 commit comments

Comments
 (0)