Skip to content

Commit 217fa99

Browse files
committed
👷 优化ci构建与同步目录
1 parent b419c91 commit 217fa99

File tree

15 files changed

+117
-85
lines changed

15 files changed

+117
-85
lines changed

.github/workflows/build.yaml

+4-12
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,12 @@ jobs:
1111
runs-on: ubuntu-latest
1212
name: Build
1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v3
1515
- name: Use Node.js
16-
uses: actions/setup-node@v1
16+
uses: actions/setup-node@v3
1717
with:
18-
node-version: '16.x'
19-
20-
- name: Cache Node.js modules
21-
uses: actions/cache@v2
22-
with:
23-
path: ~/.npm
24-
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
25-
restore-keys: |
26-
${{ runner.OS }}-node-
27-
${{ runner.OS }}-
18+
node-version: 16
19+
cache: 'npm'
2820

2921
- name: Package with Node
3022
env:

.github/workflows/packageRelease.yml

+4-12
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,12 @@ jobs:
1111
runs-on: ubuntu-latest
1212

1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v3
1515
- name: Use Node.js
16-
uses: actions/setup-node@v1
16+
uses: actions/setup-node@v3
1717
with:
18-
node-version: "16.x"
19-
20-
- name: Cache Node.js modules
21-
uses: actions/cache@v2
22-
with:
23-
path: ~/.npm
24-
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
25-
restore-keys: |
26-
${{ runner.OS }}-node-
27-
${{ runner.OS }}-
18+
node-version: 16
19+
cache: 'npm'
2820

2921
- name: Package with Node
3022
env:

.github/workflows/test.yaml

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@ jobs:
1212
runs-on: ubuntu-latest
1313
name: Run tests
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: Use Node.js
17-
uses: actions/setup-node@v1
17+
uses: actions/setup-node@v3
1818
with:
19-
node-version: "16.x"
20-
21-
- name: Cache Node.js modules
22-
uses: actions/cache@v2
23-
with:
24-
path: ~/.npm
25-
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
26-
restore-keys: |
27-
${{ runner.OS }}-node-
28-
${{ runner.OS }}-
19+
node-version: 16
20+
cache: 'npm'
2921

3022
- name: Unit Test
3123
run: |

pkg/chrome-extension-mock/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Notifications from "./notifications";
44
import Runtime from "./runtime";
55
import MockTab from "./tab";
66
import WebRequest from "./web_reqeuest";
7+
import Storage from "./storage";
78

89
const chromeMock = {
910
tabs: new MockTab(),
@@ -12,6 +13,7 @@ const chromeMock = {
1213
notifications: new Notifications(),
1314
downloads: new Downloads(),
1415
cookies: new Cookies(),
16+
storage: new Storage(),
1517
};
1618
// @ts-ignore
1719
global.chrome = chromeMock;

pkg/chrome-extension-mock/storage.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default class Storage {
2+
sync = {
3+
get(callback: (data: any) => void) {
4+
callback({});
5+
},
6+
set() {},
7+
};
8+
9+
local = {};
10+
}

pkg/filesystem/filesystem.ts

+4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ export default interface FileSystem {
3131
verify(): Promise<void>;
3232
// 打开文件
3333
open(path: string): Promise<FileReader>;
34+
// 打开目录
35+
openDir(path: string): Promise<FileSystem>;
3436
// 创建文件
3537
create(path: string): Promise<FileWriter>;
38+
// 创建目录
39+
createDir(dir: string): Promise<void>;
3640
// 删除文件
3741
delete(path: string): Promise<void>;
3842
// 文件列表

pkg/filesystem/webdav/webdav.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import { WebDAVFileReader, WebDAVFileWriter } from "./rw";
55
export default class WebDAVFileSystem implements FileSystem {
66
client: WebDAVClient;
77

8+
basePath: string = "";
9+
810
constructor(
9-
authType: AuthType,
10-
url: string,
11-
username: string,
12-
password: string
11+
authType: AuthType | WebDAVClient,
12+
url?: string,
13+
username?: string,
14+
password?: string
1315
) {
14-
this.client = createClient(url, {
15-
authType,
16-
username,
17-
password,
18-
});
16+
if (typeof authType === "object") {
17+
this.client = authType;
18+
this.basePath = url || "";
19+
} else {
20+
this.client = createClient(url!, {
21+
authType,
22+
username,
23+
password,
24+
});
25+
}
1926
}
2027

2128
async verify(): Promise<void> {
@@ -24,20 +31,39 @@ export default class WebDAVFileSystem implements FileSystem {
2431
}
2532

2633
open(path: string): Promise<FileReader> {
27-
return Promise.resolve(new WebDAVFileReader(this.client, path));
34+
return Promise.resolve(
35+
new WebDAVFileReader(this.client, this.getPath(path))
36+
);
37+
}
38+
39+
openDir(path: string): Promise<FileSystem> {
40+
if (!path.endsWith("/")) {
41+
path += "/";
42+
}
43+
return Promise.resolve(new WebDAVFileSystem(this.client, path));
2844
}
2945

3046
create(path: string): Promise<FileWriter> {
31-
return Promise.resolve(new WebDAVFileWriter(this.client, path));
47+
return Promise.resolve(
48+
new WebDAVFileWriter(this.client, this.getPath(path))
49+
);
50+
}
51+
52+
createDir(path: string): Promise<void> {
53+
return this.client.createDirectory(this.getPath(path));
3254
}
3355

3456
async delete(path: string): Promise<void> {
35-
return this.client.deleteFile(path);
57+
return this.client.deleteFile(this.getPath(path));
58+
}
59+
60+
getPath(path: string): string {
61+
return this.basePath + path;
3662
}
3763

3864
async list(path?: string | undefined): Promise<File[]> {
3965
const dir = (await this.client.getDirectoryContents(
40-
path || ""
66+
this.getPath(path || "")
4167
)) as FileStat[];
4268
const ret: File[] = [];
4369
dir.forEach((item: FileStat) => {

pkg/filesystem/zip/zip.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import { ZipFileReader, ZipFileWriter } from "./rw";
99
export default class ZipFileSystem implements FileSystem {
1010
zip: JSZip;
1111

12+
basePath: string;
13+
1214
// zip为空时,创建一个空的zip
13-
constructor(zip?: JSZip) {
15+
constructor(zip?: JSZip, basePath?: string) {
1416
this.zip = zip || new JSZip();
17+
this.basePath = basePath || "";
1518
}
1619

1720
verify(): Promise<void> {
@@ -26,10 +29,18 @@ export default class ZipFileSystem implements FileSystem {
2629
return Promise.reject(new Error("File not found"));
2730
}
2831

32+
openDir(path: string): Promise<FileSystem> {
33+
return Promise.resolve(new ZipFileSystem(this.zip, path));
34+
}
35+
2936
create(path: string): Promise<FileWriter> {
3037
return Promise.resolve(new ZipFileWriter(this.zip, path));
3138
}
3239

40+
createDir(): Promise<void> {
41+
return Promise.resolve();
42+
}
43+
3344
delete(path: string): Promise<void> {
3445
this.zip.remove(path);
3546
return Promise.resolve();

src/app/service/script/event.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import CacheKey from "@App/pkg/utils/cache_key";
44
import Cache from "../../cache";
55
import {
66
Script,
7-
SCRIPT_RUN_STATUS_COMPLETE,
87
SCRIPT_STATUS_DISABLE,
98
SCRIPT_STATUS_ENABLE,
109
ScriptDAO,

src/app/service/synchronize/event.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ export default class SynchronizeEventListener {
9898
await this.manager.backup(fs);
9999
this.logger.info("backup to cloud");
100100
// 然后创建云端文件系统
101-
const cloudFs = await FileSystemFactory.create(type, params);
102-
// 云端文件系统写入文件
103-
const file = await cloudFs.create(
104-
`scriptcat-backup-${dayjs().format("YYYY-MM-DDTHH-mm-ss")}.zip`
105-
);
101+
let cloudFs = await FileSystemFactory.create(type, params);
106102
try {
103+
await cloudFs.createDir("ScriptCat");
104+
cloudFs = await cloudFs.openDir("ScriptCat");
105+
// 云端文件系统写入文件
106+
const file = await cloudFs.create(
107+
`scriptcat-backup-${dayjs().format("YYYY-MM-DDTHH-mm-ss")}.zip`
108+
);
107109
await file.write(
108110
await zip.generateAsync({
109111
type: "blob",

src/app/service/synchronize/manager.ts

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export default class SynchronizeManager extends Manager {
106106
config.filesystem,
107107
config.params[config.filesystem]
108108
);
109+
// 创建base目录
110+
await fs.createDir("ScriptCat");
111+
await fs.createDir("ScriptCat/sync");
112+
fs = await fs.openDir("ScriptCat/sync");
109113
} catch (e) {
110114
logger.error("create filesystem error", Logger.E(e), {
111115
type: config.filesystem,

src/pages/components/ScriptMenuList/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from "react";
22
import MessageInternal from "@App/app/message/internal";
33
import { MessageSender } from "@App/app/message/message";
4-
import { ScriptMenu, ScriptMenuItem } from "@App/runtime/background/runtime";
4+
import { ScriptMenu } from "@App/runtime/background/runtime";
55
import {
66
Button,
77
Collapse,

src/pages/options/routes/Setting.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import React, { useState } from "react";
2-
import {
3-
Button,
4-
Card,
5-
Checkbox,
6-
Message,
7-
Select,
8-
Space,
9-
} from "@arco-design/web-react";
2+
import { Button, Card, Checkbox, Message, Space } from "@arco-design/web-react";
103
import FileSystemParams from "@App/pages/components/FileSystemParams";
114
import { SystemConfig } from "@App/pkg/config/config";
125
import IoC from "@App/app/ioc";

src/pages/options/routes/Tools.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,22 @@ function Tools() {
117117
<Button
118118
type="primary"
119119
onClick={async () => {
120-
const fs = await FileSystemFactory.create(
120+
let fs = await FileSystemFactory.create(
121121
fileSystemType,
122122
fileSystemParams
123123
);
124-
const list = await fs.list();
125-
list.sort((a, b) => b.updatetime - a.updatetime);
126-
setBackupFileList(list);
124+
try {
125+
fs = await fs.openDir("ScriptCat");
126+
const list = await fs.list();
127+
list.sort((a, b) => b.updatetime - a.updatetime);
128+
if (list.length === 0) {
129+
Message.info("没有备份文件");
130+
return;
131+
}
132+
setBackupFileList(list);
133+
} catch (e) {
134+
Message.error(`获取备份文件失败: ${e}`);
135+
}
127136
}}
128137
>
129138
备份列表

src/runtime/background/runtime.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Logger from "@App/app/logger/logger";
66
import {
77
Script,
88
SCRIPT_RUN_STATUS,
9-
SCRIPT_RUN_STATUS_RUNNING,
109
SCRIPT_STATUS_ENABLE,
1110
SCRIPT_TYPE_NORMAL,
1211
ScriptDAO,
@@ -241,22 +240,19 @@ export default class Runtime extends Manager {
241240
runningScript.delete(script.id);
242241
});
243242

244-
Runtime.hook.addListener(
245-
"runStatus",
246-
async (scriptId: number, status: SCRIPT_RUN_STATUS) => {
247-
const script = await this.scriptDAO.findById(scriptId);
248-
if (!script) {
249-
return;
250-
}
251-
if (script.status !== SCRIPT_STATUS_ENABLE) {
252-
// 没开启并且不是运行中的脚本,删除
253-
runningScript.delete(scriptId);
254-
} else {
255-
// 否则进行一次更新
256-
runningScript.set(scriptId, script);
257-
}
243+
Runtime.hook.addListener("runStatus", async (scriptId: number) => {
244+
const script = await this.scriptDAO.findById(scriptId);
245+
if (!script) {
246+
return;
258247
}
259-
);
248+
if (script.status !== SCRIPT_STATUS_ENABLE) {
249+
// 没开启并且不是运行中的脚本,删除
250+
runningScript.delete(scriptId);
251+
} else {
252+
// 否则进行一次更新
253+
runningScript.set(scriptId, script);
254+
}
255+
});
260256

261257
// 给popup页面获取运行脚本,与菜单
262258
this.message.setHandler(

0 commit comments

Comments
 (0)