Skip to content

Commit 0ed8575

Browse files
committed
Update electron backend to store books under appData dir, this will persistent books storage, but need to change Angular part and add a mini web server to supply md and other files to angular.
1 parent ad0be9a commit 0ed8575

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed

electron/db.sqlite

-60 KB
Binary file not shown.

electron/package.json

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,93 @@
11
{
2-
"name": "Gitbook-Reader-CCNA60D",
3-
"version": "0.2.0",
4-
"description": "Gitbook 阅读器,60天通过CCNA考试 - ccna60d.xfoss.com",
5-
"main": "./build/index.js",
6-
"scripts": {
7-
"build": "tsc && cp -r ./src/resources/ ./build/",
8-
"electron:start": "npm run build && electron .",
9-
"electron:pack": "npm run build && electron-builder build --dir",
10-
"dist:win": "npm run build && electron-builder build --win",
11-
"dist:linux": "npm run build && electron-builder build --linux"
12-
},
13-
"dependencies": {
14-
"@capacitor-community/electron": "^1.3.2",
15-
"nodegit": "^0.27.0",
16-
"sqlite3": "^5.0.0",
17-
"typeorm": "^0.2.29"
18-
},
19-
"devDependencies": {
20-
"@types/filesystem": "^0.0.29",
21-
"@types/fs-extra": "^9.0.5",
22-
"@types/node": "^14.14.12",
23-
"@types/nodegit": "^0.26.12",
24-
"electron": "^10.1.7",
25-
"electron-builder": "^22.9.1",
26-
"electron-packager": "^15.2.0",
27-
"electron-rebuild": "^2.3.4",
28-
"typescript": "~4.0.3"
29-
},
30-
"keywords": [
31-
"capacitor",
32-
"electron"
2+
"name": "gitbook-reader-app",
3+
"version": "0.4.0",
4+
"description": "Gitbook 阅读器,60天通过CCNA考试 - ccna60d.xfoss.com",
5+
"main": "./build/index.js",
6+
"scripts": {
7+
"build": "tsc && cp -r ./src/resources/ ./build/",
8+
"electron:start": "npm run build && electron .",
9+
"electron:pack": "npm run build && electron-builder build --dir",
10+
"dist:win": "npm run build && electron-builder build --win",
11+
"dist:linux": "npm run build && electron-builder build --linux"
12+
},
13+
"dependencies": {
14+
"@capacitor-community/electron": "^1.3.2",
15+
"nodegit": "^0.27.0",
16+
"sqlite3": "^5.0.0",
17+
"typeorm": "^0.2.29"
18+
},
19+
"devDependencies": {
20+
"@types/filesystem": "^0.0.29",
21+
"@types/fs-extra": "^9.0.5",
22+
"@types/node": "^14.14.12",
23+
"@types/nodegit": "^0.26.12",
24+
"electron": "^10.1.7",
25+
"electron-builder": "^22.9.1",
26+
"electron-packager": "^15.2.0",
27+
"electron-rebuild": "^2.3.4",
28+
"typescript": "~4.0.3"
29+
},
30+
"keywords": [
31+
"capacitor",
32+
"electron"
33+
],
34+
"author": "Peng Hailin, [email protected]",
35+
"license": "MIT",
36+
"build": {
37+
"appId": "com.xfoss.ccna60d",
38+
"compression": "normal",
39+
"npmRebuild": false,
40+
"productName": "Gitbook-阅读器",
41+
"files": [
42+
"assets/*",
43+
"build/**/*",
44+
"preloader.js",
45+
"plugins/*",
46+
"capacitor.config.json",
47+
"app/**"
3348
],
34-
"author": "",
35-
"license": "MIT",
36-
"build": {
37-
"appId": "com.xfoss.ccna60d",
38-
"compression": "normal",
39-
"npmRebuild": false,
40-
"productName": "Gitbook-阅读器-CCNA60D",
41-
"files": [
42-
"assets/*",
43-
"build/**/*",
44-
"preloader.js",
45-
"plugins/*",
46-
"capacitor.config.json",
47-
"app/**"
48-
],
49-
"mac": {
50-
"category": "public.app-category.education",
51-
"target": "dmg",
52-
"icon": "appIcon.png"
53-
},
54-
"win": {
55-
"target": [
56-
{
57-
"target": "nsis",
58-
"arch": [
59-
"x64",
60-
"ia32"
61-
]
62-
},
63-
{
64-
"target": "portable",
65-
"arch": [
66-
"x64",
67-
"ia32"
68-
]
69-
}
70-
],
71-
"icon": "./assets/appIcon.png"
72-
},
73-
"nsis": {
74-
"artifactName": "GitbookReader-CCNA60d-${version}-setup.${ext}"
75-
},
76-
"portable": {
77-
"artifactName": "GitbookReader-CCNA60d-${version}-portable.${ext}"
49+
"mac": {
50+
"category": "public.app-category.education",
51+
"target": "dmg",
52+
"icon": "appIcon.png"
53+
},
54+
"win": {
55+
"target": [
56+
{
57+
"target": "nsis",
58+
"arch": [
59+
"x64",
60+
"ia32"
61+
]
7862
},
79-
"linux": {
80-
"target": "AppImage",
81-
"artifactName": "GitbookReader-CCNA60d-${version}.${ext}",
82-
"icon": "./assets/appIcon.png"
63+
{
64+
"target": "portable",
65+
"arch": [
66+
"x64",
67+
"ia32"
68+
]
8369
}
70+
],
71+
"icon": "./assets/appIcon.png"
72+
},
73+
"nsis": {
74+
"artifactName": "Gitbook-reader-${version}-setup.${ext}"
75+
},
76+
"portable": {
77+
"artifactName": "Gitbook-reader-${version}-portable.${ext}"
78+
},
79+
"linux": {
80+
"target": "AppImage",
81+
"artifactName": "Gitbook-reader-${version}.${ext}",
82+
"icon": "./assets/appIcon.png"
8483
}
84+
},
85+
"repository": {
86+
"type": "git",
87+
"url": "git+https://github.com/gnu4cn/gitbook-reader-app.git"
88+
},
89+
"bugs": {
90+
"url": "https://github.com/gnu4cn/gitbook-reader-app/issues"
91+
},
92+
"homepage": "https://github.com/gnu4cn/gitbook-reader-app#readme"
8593
}

electron/src/bookOps.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const Git = require('nodegit');
22

3-
import { remove } from 'fs-extra';
3+
import { remove, ensureDirSync } from 'fs-extra';
44
import { resolve } from 'path';
55

66
import { WebContents } from "electron";
77
import { updateItem } from './crud';
8+
import { join } from 'path';
89

910
import { IBookDownloading } from './vendor';
1011
import { Book } from './models';
1112
import { escapeFileNames } from './fsOps';
1213

13-
export const bookClone = async (book: Book, web: WebContents) => {
14-
const repoDir = `../app/assets/${book.website.uri}/${book.writer.name}/${book.name}`;
15-
const bookPath = resolve(__dirname, repoDir);
14+
export const bookClone = async (book: Book, web: WebContents, booksDir: string) => {
15+
const bookPath = join(booksDir, book.website.uri, book.writer.name, book.name);
1616

1717
const bookUri = `https://${book.website.uri}/${book.writer.name}/${book.name}.git`;
1818

@@ -43,6 +43,8 @@ export const bookClone = async (book: Book, web: WebContents) => {
4343
};
4444

4545
await remove(bookPath).then(async () => {
46+
await ensureDirSync(bookPath);
47+
4648
Git.Clone.clone(bookUri, bookPath, opts)
4749
.then(repo => {
4850
return repo.getMasterCommit();

electron/src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { app, Menu, BrowserWindow, ipcMain } from "electron";
22
import { createCapacitorElectronApp } from "@capacitor-community/electron";
33

4+
import { join } from 'path';
5+
46
import { loadingWindow, bookWindow } from './window.children';
57
import { bookClone } from './bookOps';
68
//import { createMenuTemplate } from './menu_template';
7-
8-
import { conn } from './crud';
9-
import { onGetItems, onAddItem, } from './ipc';
9+
import { conn } from './crud';
10+
import { onGetItems, onAddItem, } from './ipc';
1011
// The MainWindow object can be accessed via myCapacitorApp.getMainWindow()
1112

1213
const myCapacitorApp = createCapacitorElectronApp();
@@ -15,8 +16,11 @@ const winChildren: Array<Electron.BrowserWindow> = [];
1516
// initialization and is ready to create browser windows.
1617
// Some Electron APIs can only be used after this event occurs.
1718
app.on("ready", () => {
19+
1820
myCapacitorApp.init();
1921

22+
const booksDir = join(app.getPath('appData'), 'gbr_books');
23+
2024
const mainWindow = myCapacitorApp.getMainWindow();
2125
const webContents = mainWindow.webContents;
2226

@@ -42,7 +46,7 @@ app.on("ready", () => {
4246
});
4347

4448
ipcMain.on('download-book', (event, book) => {
45-
bookClone(book, webContents);
49+
bookClone(book, webContents, booksDir);
4650
});
4751

4852
//const menuTemplate = createMenuTemplate(app);
@@ -66,6 +70,7 @@ app.on("window-all-closed", async () => {
6670
return 0
6771
});
6872

73+
6974
app.on("activate", function () {
7075
// On OS X it's common to re-create a window in the app when the
7176
// dock icon is clicked and there are no other windows open.

electron/src/window.children.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function bookWindow(parentWin: Electron.BrowserWindow, loadingWin: Electr
88
let bookWindow: Electron.BrowserWindow;
99

1010
bookWindow = new BrowserWindow({
11-
width: 1024,
11+
width: 1366,
1212
height: 768,
1313
center: true,
1414
darkTheme: true,
@@ -27,7 +27,7 @@ export function bookWindow(parentWin: Electron.BrowserWindow, loadingWin: Electr
2727
const bookUrl = `capacitor-electron://-/#/${book.website.uri}/${book.writer.name}/${book.name}?commit=${book.commit}`;
2828
bookWindow.loadURL(bookUrl);
2929

30-
webContents.openDevTools();
30+
//webContents.openDevTools();
3131

3232
ipcMain.on('book-loading', () =>{
3333
loadingWin.show();

0 commit comments

Comments
 (0)