mirror of
https://github.com/frappe/books.git
synced 2024-11-10 07:40:55 +00:00
refactor: move fjs init boilerplate out, shift db init
This commit is contained in:
parent
5251138542
commit
512e16d910
@ -30,8 +30,9 @@ import DatabaseSelector from './pages/DatabaseSelector';
|
||||
import WindowsTitleBar from '@/components/WindowsTitleBar';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import config from '@/config';
|
||||
import { connectToLocalDatabase, routeTo, purgeCache } from '@/utils';
|
||||
import { routeTo } from '@/utils';
|
||||
import { IPC_MESSAGES, IPC_ACTIONS } from '@/messages';
|
||||
import { connectToLocalDatabase, purgeCache } from '@/initialization';
|
||||
|
||||
export default {
|
||||
name: 'App',
|
||||
|
103
src/initialization.js
Normal file
103
src/initialization.js
Normal file
@ -0,0 +1,103 @@
|
||||
import config from '@/config';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { _ } from 'frappejs';
|
||||
import SQLite from 'frappejs/backends/sqlite';
|
||||
import fs from 'fs';
|
||||
import postStart from '../server/postStart';
|
||||
import migrate from './migrate';
|
||||
import { IPC_ACTIONS } from './messages';
|
||||
|
||||
export async function createNewDatabase() {
|
||||
const options = {
|
||||
title: _('Select folder'),
|
||||
defaultPath: 'frappe-books.db',
|
||||
};
|
||||
|
||||
let { canceled, filePath } = await ipcRenderer.invoke(
|
||||
IPC_ACTIONS.GET_SAVE_FILEPATH,
|
||||
options
|
||||
);
|
||||
|
||||
if (canceled || filePath.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!filePath.endsWith('.db')) {
|
||||
showMessageDialog({
|
||||
message: "Please select a filename ending with '.db'.",
|
||||
});
|
||||
return '';
|
||||
}
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export async function loadExistingDatabase() {
|
||||
const options = {
|
||||
title: _('Select file'),
|
||||
properties: ['openFile'],
|
||||
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
|
||||
};
|
||||
|
||||
const { filePaths } = await ipcRenderer.invoke(
|
||||
IPC_ACTIONS.GET_OPEN_FILEPATH,
|
||||
options
|
||||
);
|
||||
|
||||
if (filePaths && filePaths[0]) {
|
||||
return filePaths[0];
|
||||
}
|
||||
}
|
||||
|
||||
export async function connectToLocalDatabase(filePath) {
|
||||
if (!filePath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
frappe.login('Administrator');
|
||||
try {
|
||||
frappe.db = new SQLite({
|
||||
dbPath: filePath,
|
||||
});
|
||||
await frappe.db.connect();
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await migrate();
|
||||
await postStart();
|
||||
|
||||
// set file info in config
|
||||
let files = config.get('files') || [];
|
||||
if (!files.find((file) => file.filePath === filePath)) {
|
||||
files = [
|
||||
{
|
||||
companyName: frappe.AccountingSettings.companyName,
|
||||
filePath: filePath,
|
||||
},
|
||||
...files,
|
||||
];
|
||||
config.set('files', files);
|
||||
}
|
||||
|
||||
// set last selected file
|
||||
config.set('lastSelectedFilePath', filePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function purgeCache(purgeAll = false) {
|
||||
const filterFunction = purgeAll
|
||||
? (d) => true
|
||||
: (d) => frappe.docs[d][d] instanceof frappe.BaseMeta;
|
||||
|
||||
Object.keys(frappe.docs)
|
||||
.filter(filterFunction)
|
||||
.forEach((d) => {
|
||||
frappe.removeFromCache(d, d);
|
||||
delete frappe[d];
|
||||
});
|
||||
}
|
20
src/main.js
20
src/main.js
@ -1,28 +1,18 @@
|
||||
// frappejs imports
|
||||
import { ipcRenderer } from 'electron';
|
||||
import frappe from 'frappejs';
|
||||
import common from 'frappejs/common';
|
||||
import coreModels from 'frappejs/models';
|
||||
import FeatherIcon from 'frappejs/ui/components/FeatherIcon';
|
||||
import outsideClickDirective from 'frappejs/ui/plugins/outsideClickDirective';
|
||||
import models from '../models';
|
||||
|
||||
// vue imports
|
||||
import Vue from 'vue';
|
||||
import PortalVue from 'portal-vue';
|
||||
import Vue from 'vue';
|
||||
import models from '../models';
|
||||
import App from './App';
|
||||
import router from './router';
|
||||
|
||||
// other imports
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { IPC_MESSAGES } from './messages';
|
||||
import router from './router';
|
||||
|
||||
(async () => {
|
||||
frappe.isServer = true;
|
||||
frappe.isElectron = true;
|
||||
frappe.init();
|
||||
frappe.registerLibs(common);
|
||||
frappe.registerModels(coreModels);
|
||||
frappe.registerModels(models);
|
||||
frappe.initializeAndRegister(models);
|
||||
frappe.fetch = window.fetch.bind();
|
||||
|
||||
frappe.events.on('reload-main-window', () => {
|
||||
|
@ -157,7 +157,7 @@ import {
|
||||
createNewDatabase,
|
||||
loadExistingDatabase,
|
||||
connectToLocalDatabase,
|
||||
} from '@/utils';
|
||||
} from '@/initialization';
|
||||
|
||||
export default {
|
||||
name: 'DatabaseSelector',
|
||||
|
@ -68,13 +68,11 @@ import Popover from '@/components/Popover';
|
||||
import config from '@/config';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { connectToLocalDatabase } from '@/utils';
|
||||
|
||||
import { purgeCache, connectToLocalDatabase } from '@/initialization';
|
||||
import {
|
||||
getErrorMessage,
|
||||
handleErrorWithDialog,
|
||||
showMessageDialog,
|
||||
purgeCache,
|
||||
} from '@/utils';
|
||||
|
||||
export default {
|
||||
|
100
src/utils.js
100
src/utils.js
@ -1,96 +1,9 @@
|
||||
import Avatar from '@/components/Avatar';
|
||||
import config from '@/config';
|
||||
import router from '@/router';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import frappe from 'frappejs';
|
||||
import SQLite from 'frappejs/backends/sqlite';
|
||||
import { _ } from 'frappejs/utils';
|
||||
import fs from 'fs';
|
||||
import postStart from '../server/postStart';
|
||||
import { IPC_ACTIONS, IPC_MESSAGES } from './messages';
|
||||
import migrate from './migrate';
|
||||
|
||||
export async function createNewDatabase() {
|
||||
const options = {
|
||||
title: _('Select folder'),
|
||||
defaultPath: 'frappe-books.db',
|
||||
};
|
||||
|
||||
let { canceled, filePath } = await ipcRenderer.invoke(
|
||||
IPC_ACTIONS.GET_SAVE_FILEPATH,
|
||||
options
|
||||
);
|
||||
|
||||
if (canceled || filePath.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!filePath.endsWith('.db')) {
|
||||
showMessageDialog({
|
||||
message: "Please select a filename ending with '.db'.",
|
||||
});
|
||||
return '';
|
||||
}
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export async function loadExistingDatabase() {
|
||||
const options = {
|
||||
title: _('Select file'),
|
||||
properties: ['openFile'],
|
||||
filters: [{ name: 'SQLite DB File', extensions: ['db'] }],
|
||||
};
|
||||
|
||||
const { filePaths } = await ipcRenderer.invoke(
|
||||
IPC_ACTIONS.GET_OPEN_FILEPATH,
|
||||
options
|
||||
);
|
||||
|
||||
if (filePaths && filePaths[0]) {
|
||||
return filePaths[0];
|
||||
}
|
||||
}
|
||||
|
||||
export async function connectToLocalDatabase(filePath) {
|
||||
if (!filePath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
frappe.login('Administrator');
|
||||
try {
|
||||
frappe.db = new SQLite({
|
||||
dbPath: filePath,
|
||||
});
|
||||
await frappe.db.connect();
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await migrate();
|
||||
await postStart();
|
||||
|
||||
// set file info in config
|
||||
let files = config.get('files') || [];
|
||||
if (!files.find((file) => file.filePath === filePath)) {
|
||||
files = [
|
||||
{
|
||||
companyName: frappe.AccountingSettings.companyName,
|
||||
filePath: filePath,
|
||||
},
|
||||
...files,
|
||||
];
|
||||
config.set('files', files);
|
||||
}
|
||||
|
||||
// set last selected file
|
||||
config.set('lastSelectedFilePath', filePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function showMessageDialog({
|
||||
message,
|
||||
@ -339,19 +252,6 @@ export function routeTo(route) {
|
||||
}
|
||||
}
|
||||
|
||||
export function purgeCache(purgeAll = false) {
|
||||
const filterFunction = purgeAll
|
||||
? (d) => true
|
||||
: (d) => frappe.docs[d][d] instanceof frappe.BaseMeta;
|
||||
|
||||
Object.keys(frappe.docs)
|
||||
.filter(filterFunction)
|
||||
.forEach((d) => {
|
||||
frappe.removeFromCache(d, d);
|
||||
delete frappe[d];
|
||||
});
|
||||
}
|
||||
|
||||
export function fuzzyMatch(keyword, candidate) {
|
||||
const keywordLetters = [...keyword];
|
||||
const candidateLetters = [...candidate];
|
||||
|
Loading…
Reference in New Issue
Block a user