mirror of
https://github.com/frappe/books.git
synced 2024-11-09 23:30:56 +00:00
incr: remove fs and path calls
This commit is contained in:
parent
0951e11cb1
commit
341c1bb639
46
main/helpers.ts
Normal file
46
main/helpers.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import { constants } from 'fs';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
import { ConfigFile, ConfigKeys } from 'fyo/core/types';
|
||||||
|
import config from 'utils/config';
|
||||||
|
|
||||||
|
interface ConfigFilesWithModified extends ConfigFile {
|
||||||
|
modified: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setAndGetCleanedConfigFiles() {
|
||||||
|
const files = config.get(ConfigKeys.Files, []) as ConfigFile[];
|
||||||
|
|
||||||
|
const cleanedFileMap: Map<string, ConfigFile> = new Map();
|
||||||
|
for (const file of files) {
|
||||||
|
const exists = await fs
|
||||||
|
.access(file.dbPath, constants.W_OK)
|
||||||
|
.then(() => true)
|
||||||
|
.catch(() => false);
|
||||||
|
|
||||||
|
const key = `${file.companyName}-${file.dbPath}`;
|
||||||
|
if (!exists || cleanedFileMap.has(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanedFileMap.set(key, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanedFiles = Array.from(cleanedFileMap.values());
|
||||||
|
config.set(ConfigKeys.Files, cleanedFiles);
|
||||||
|
return cleanedFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getConfigFilesWithModified(files: ConfigFile[]) {
|
||||||
|
const filesWithModified: ConfigFilesWithModified[] = [];
|
||||||
|
for (const { dbPath, id, companyName } of files) {
|
||||||
|
const { mtime } = await fs.stat(dbPath);
|
||||||
|
filesWithModified.push({
|
||||||
|
id,
|
||||||
|
dbPath,
|
||||||
|
companyName,
|
||||||
|
modified: mtime.toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return filesWithModified;
|
||||||
|
}
|
@ -9,6 +9,10 @@ import { DatabaseResponse } from '../utils/ipc/types';
|
|||||||
import { IPC_ACTIONS } from '../utils/messages';
|
import { IPC_ACTIONS } from '../utils/messages';
|
||||||
import { getUrlAndTokenString, sendError } from './contactMothership';
|
import { getUrlAndTokenString, sendError } from './contactMothership';
|
||||||
import { getLanguageMap } from './getLanguageMap';
|
import { getLanguageMap } from './getLanguageMap';
|
||||||
|
import {
|
||||||
|
getConfigFilesWithModified,
|
||||||
|
setAndGetCleanedConfigFiles,
|
||||||
|
} from './helpers';
|
||||||
import { saveHtmlAsPdf } from './saveHtmlAsPdf';
|
import { saveHtmlAsPdf } from './saveHtmlAsPdf';
|
||||||
|
|
||||||
export default function registerIpcMainActionListeners(main: Main) {
|
export default function registerIpcMainActionListeners(main: Main) {
|
||||||
@ -120,6 +124,11 @@ export default function registerIpcMainActionListeners(main: Main) {
|
|||||||
await fs.unlink(filePath);
|
await fs.unlink(filePath);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.handle(IPC_ACTIONS.GET_DB_LIST, async (_) => {
|
||||||
|
const files = await setAndGetCleanedConfigFiles();
|
||||||
|
return await getConfigFilesWithModified(files);
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database Related Actions
|
* Database Related Actions
|
||||||
*/
|
*/
|
||||||
|
@ -57,8 +57,6 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { ipcRenderer } from 'electron';
|
import { ipcRenderer } from 'electron';
|
||||||
import fs from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
import { fyo } from 'src/initFyo';
|
import { fyo } from 'src/initFyo';
|
||||||
import { IPC_ACTIONS } from 'utils/messages';
|
import { IPC_ACTIONS } from 'utils/messages';
|
||||||
import Base from './Base';
|
import Base from './Base';
|
||||||
@ -82,24 +80,28 @@ export default {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const { filePaths } = await ipcRenderer.invoke(
|
const { name, success, data } = await ipcRenderer.invoke(
|
||||||
IPC_ACTIONS.GET_OPEN_FILEPATH,
|
IPC_ACTIONS.GET_FILE,
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
if (filePaths && filePaths[0]) {
|
|
||||||
let dataURL = await this.getDataURL(filePaths[0]);
|
if (!success) {
|
||||||
this.triggerChange(dataURL);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dataURL = await this.getDataURL(name, data);
|
||||||
|
this.triggerChange(dataURL);
|
||||||
},
|
},
|
||||||
getDataURL(filePath) {
|
getDataURL(name, data) {
|
||||||
let typedArray = fs.readFileSync(filePath);
|
const extension = name.split('.').at(-1);
|
||||||
let extension = path.extname(filePath).slice(1);
|
const blob = new Blob([data], { type: 'image/' + extension });
|
||||||
let blob = new Blob([typedArray.buffer], { type: 'image/' + extension });
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let fr = new FileReader();
|
const fr = new FileReader();
|
||||||
fr.addEventListener('loadend', () => {
|
fr.addEventListener('loadend', () => {
|
||||||
resolve(fr.result);
|
resolve(fr.result);
|
||||||
});
|
});
|
||||||
|
|
||||||
fr.readAsDataURL(blob);
|
fr.readAsDataURL(blob);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -102,7 +102,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import path from 'path';
|
|
||||||
import Button from 'src/components/Button.vue';
|
import Button from 'src/components/Button.vue';
|
||||||
import { reportIssue } from 'src/errorHandling';
|
import { reportIssue } from 'src/errorHandling';
|
||||||
import { fyo } from 'src/initFyo';
|
import { fyo } from 'src/initFyo';
|
||||||
@ -126,10 +125,6 @@ export default {
|
|||||||
appVersion() {
|
appVersion() {
|
||||||
return fyo.store.appVersion;
|
return fyo.store.appVersion;
|
||||||
},
|
},
|
||||||
dbPath() {
|
|
||||||
const splits = fyo.db.dbPath.split(path.sep);
|
|
||||||
return path.join(...splits.slice(splits.length - 2));
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
WindowControls,
|
WindowControls,
|
||||||
@ -178,7 +173,8 @@ export default {
|
|||||||
itemActiveClass(item) {
|
itemActiveClass(item) {
|
||||||
let { path: currentRoute, params } = this.$route;
|
let { path: currentRoute, params } = this.$route;
|
||||||
let routeMatch = currentRoute === item.route;
|
let routeMatch = currentRoute === item.route;
|
||||||
let schemaNameMatch = item.schemaName && params.schemaName === item.schemaName;
|
let schemaNameMatch =
|
||||||
|
item.schemaName && params.schemaName === item.schemaName;
|
||||||
return routeMatch || schemaNameMatch ? 'bg-white text-blue-500' : '';
|
return routeMatch || schemaNameMatch ? 'bg-white text-blue-500' : '';
|
||||||
},
|
},
|
||||||
isActiveGroup(group) {
|
isActiveGroup(group) {
|
||||||
|
@ -95,7 +95,7 @@
|
|||||||
{{ file.companyName }}
|
{{ file.companyName }}
|
||||||
</p>
|
</p>
|
||||||
<p class="text-sm text-gray-600">
|
<p class="text-sm text-gray-600">
|
||||||
{{ file.modified }}
|
{{ formatDate(file.modified) }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
@ -148,7 +148,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { setupDummyInstance } from 'dummy';
|
import { setupDummyInstance } from 'dummy';
|
||||||
import { ipcRenderer } from 'electron';
|
import { ipcRenderer } from 'electron';
|
||||||
import fs from 'fs';
|
|
||||||
import { t } from 'fyo';
|
import { t } from 'fyo';
|
||||||
import { ConfigKeys } from 'fyo/core/types';
|
import { ConfigKeys } from 'fyo/core/types';
|
||||||
import { addNewFile } from 'fyo/telemetry/helpers';
|
import { addNewFile } from 'fyo/telemetry/helpers';
|
||||||
@ -175,14 +174,17 @@ export default {
|
|||||||
files: [],
|
files: [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
mounted() {
|
async mounted() {
|
||||||
this.setFiles();
|
await this.setFiles();
|
||||||
|
|
||||||
if (fyo.store.isDevelopment) {
|
if (fyo.store.isDevelopment) {
|
||||||
window.ds = this;
|
window.ds = this;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
formatDate(isoDate) {
|
||||||
|
return DateTime.fromISO(isoDate).toRelative();
|
||||||
|
},
|
||||||
async deleteDb(i) {
|
async deleteDb(i) {
|
||||||
const file = this.files[i];
|
const file = this.files[i];
|
||||||
const vm = this;
|
const vm = this;
|
||||||
@ -195,7 +197,7 @@ export default {
|
|||||||
label: this.t`Yes`,
|
label: this.t`Yes`,
|
||||||
async action() {
|
async action() {
|
||||||
await deleteDb(file.dbPath);
|
await deleteDb(file.dbPath);
|
||||||
vm.setFiles();
|
await vm.setFiles();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -230,18 +232,14 @@ export default {
|
|||||||
fyo.config.get(ConfigKeys.Files, []),
|
fyo.config.get(ConfigKeys.Files, []),
|
||||||
filePath
|
filePath
|
||||||
);
|
);
|
||||||
|
|
||||||
fyo.purgeCache();
|
fyo.purgeCache();
|
||||||
this.setFiles();
|
await this.setFiles();
|
||||||
|
|
||||||
this.creatingDemo = false;
|
this.creatingDemo = false;
|
||||||
},
|
},
|
||||||
setFiles() {
|
async setFiles() {
|
||||||
this.files = setAndGetCleanedConfigFiles();
|
this.files = await ipcRenderer.invoke(IPC_ACTIONS.GET_DB_LIST);
|
||||||
|
|
||||||
for (const file of this.files) {
|
|
||||||
const stats = fs.statSync(file.dbPath);
|
|
||||||
file.modified = DateTime.fromJSDate(stats.mtime).toRelative();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async newDatabase() {
|
async newDatabase() {
|
||||||
if (this.creatingDemo) {
|
if (this.creatingDemo) {
|
||||||
@ -297,24 +295,4 @@ export default {
|
|||||||
FeatherIcon,
|
FeatherIcon,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function setAndGetCleanedConfigFiles() {
|
|
||||||
const files = fyo.config
|
|
||||||
.get('files', [])
|
|
||||||
.filter(({ dbPath }) => fs.existsSync(dbPath));
|
|
||||||
|
|
||||||
const deduper = [];
|
|
||||||
const deduped = files.filter(({ companyName, dbPath }) => {
|
|
||||||
const key = `${companyName}-${dbPath}`;
|
|
||||||
if (deduper.includes(key)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
deduper.push(key);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
fyo.config.set(ConfigKeys.Files, deduped);
|
|
||||||
return deduped;
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -26,6 +26,7 @@ export enum IPC_ACTIONS {
|
|||||||
GET_FILE = 'get-file',
|
GET_FILE = 'get-file',
|
||||||
GET_CREDS = 'get-creds',
|
GET_CREDS = 'get-creds',
|
||||||
GET_VERSION = 'get-version',
|
GET_VERSION = 'get-version',
|
||||||
|
GET_DB_LIST = 'get-db-list',
|
||||||
DELETE_FILE = 'delete-file',
|
DELETE_FILE = 'delete-file',
|
||||||
// Database messages
|
// Database messages
|
||||||
DB_CREATE = 'db-create',
|
DB_CREATE = 'db-create',
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* Properties of a schema which are to be translated,
|
||||||
|
* irrespective of nesting.
|
||||||
|
*/
|
||||||
export const schemaTranslateables = ['label', 'description', 'placeholder'];
|
export const schemaTranslateables = ['label', 'description', 'placeholder'];
|
||||||
|
|
||||||
export function getIndexFormat(inp: string | string[]) {
|
export function getIndexFormat(inp: string | string[]) {
|
||||||
// converts:
|
/**
|
||||||
// ['This is an ', ,' interpolated ',' string.'] and
|
* converts:
|
||||||
// 'This is an ${variableA} interpolated ${variableB} string.'
|
* ['This is an ', ,' interpolated ',' string.'] and
|
||||||
// to 'This is an ${0} interpolated ${1} string.'
|
* 'This is an ${variableA} interpolated ${variableB} string.'
|
||||||
|
* to 'This is an ${0} interpolated ${1} string.'
|
||||||
|
*/
|
||||||
|
|
||||||
let string: string | undefined = undefined;
|
let string: string | undefined = undefined;
|
||||||
let snippets: string[] | undefined = undefined;
|
let snippets: string[] | undefined = undefined;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user