Skip to content

Commit e2ea1bc

Browse files
committed
Add connectivity check before init-app (#104)
* add conn check * (add) conncheck on info update ci update ci temporarily mute macos test
1 parent b40e71a commit e2ea1bc

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ jobs:
4040
args: zip -qq -r ./eup.zip lib scripts vscode package.json
4141

4242
- name: Upload tarball
43-
uses: actions/upload-artifact@v3
43+
uses: actions/upload-artifact@v4
4444
with:
4545
name: ${{ steps.build.outputs.tarball }}
4646
path: ${{ steps.build.outputs.tarball }}
4747

4848
- name: Upload EUP package
49-
uses: actions/upload-artifact@v3
49+
uses: actions/upload-artifact@v4
5050
with:
5151
name: eup.zip
5252
path: eup.zip
@@ -60,7 +60,7 @@ jobs:
6060

6161
strategy:
6262
matrix:
63-
os: [windows-latest, ubuntu-latest, macos-latest]
63+
os: [windows-latest, ubuntu-latest]
6464
node: ['16']
6565

6666
steps:
@@ -81,7 +81,7 @@ jobs:
8181
run: yarn --ignore-scripts --registry=https://registry-lpm.listenai.com
8282

8383
- name: Download tarball
84-
uses: actions/download-artifact@v3
84+
uses: actions/download-artifact@v4
8585
with:
8686
name: ${{ needs.build.outputs.tarball }}
8787

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lisa-plugin/zephyr",
3-
"version": "2.0.0",
3+
"version": "2.1.0-beta.1",
44
"description": "LISA plugin for Zephyr",
55
"main": "./lib/main.js",
66
"engines": {

src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import AppProject from "./models/appProject";
1515
import { resolve, dirname } from "path";
1616
import * as Sentry from "@sentry/node";
1717
import { join } from "path";
18+
import { connCheck } from "./utils/connCheck";
1819
Sentry.init({
1920
dsn: "http://[email protected]/106",
2021
tracesSampleRate: 1.0,
@@ -70,6 +71,15 @@ export async function env(): Promise<Record<string, string>> {
7071
});
7172
}
7273

74+
try {
75+
await connCheck();
76+
} catch (e) {
77+
const errmsg = (e as Error).message;
78+
Object.assign(variables, {
79+
res: redChar(errmsg)
80+
});
81+
}
82+
7383
return {
7484
env: envShow,
7585
west: westShow,

src/tasks/project.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import {flashRun, IFlashOpts} from "../utils/flash";
2424
import {IImage} from "@tool/lpk/lib/manifest";
2525
import {toNumber} from "lodash";
26+
import { connCheck } from "../utils/connCheck";
2627

2728
async function getAppFlashAddr(buildDir: string): Promise<number> {
2829
const hasLoadOffset = await getKconfig(buildDir, 'CONFIG_HAS_FLASH_LOAD_OFFSET');
@@ -106,6 +107,8 @@ export default ({ application, cmd, cli }: LisaType) => {
106107
job("init-app", {
107108
title: "初始化项目",
108109
async task(ctx, task) {
110+
await connCheck(task);
111+
109112
task.title = "";
110113
const targetDir = workspace() || process.cwd();
111114
const app = new AppProject(targetDir, task);
@@ -117,6 +120,8 @@ export default ({ application, cmd, cli }: LisaType) => {
117120
job("update", {
118121
title: "更新提货单projects",
119122
async task(ctx, task) {
123+
await connCheck(task);
124+
120125
task.title = "";
121126
const targetDir = workspace() || process.cwd();
122127
const app = new AppProject(targetDir, task);

src/utils/connCheck.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import got, { Response } from 'got';
2+
3+
interface ConnCheckRawResult {
4+
url: string;
5+
ok: boolean;
6+
}
7+
8+
export async function connCheck(task?: any): Promise<void> {
9+
// set proxy?
10+
if (task) {
11+
task.title = "正在检查网络状况...";
12+
}
13+
14+
const urlBase: Record<string, string> = {
15+
"https://cdn.iflyos.cn/public/cskTools/conntest.txt": "聆思资源服务",
16+
"https://pypi.tuna.tsinghua.edu.cn/simple/numpy": "PyPI 软件仓库(TUNA 镜像站)",
17+
"https://registry.npmmirror.com": "NPMMirror 镜像站"
18+
};
19+
20+
const urls = Object.keys(urlBase);
21+
const checkResult = await parallelHttpsCheck(urls);
22+
const failedUrls = checkResult.filter(result => !result.ok);
23+
if (failedUrls.length > 0) {
24+
const failedItems = failedUrls.map(result => urlBase[result.url]).join(', ');
25+
throw new Error(`资源访问失败,请检查网络连接是否正常。item = ${failedItems}`);
26+
}
27+
}
28+
29+
async function parallelHttpsCheck(urls: string[]): Promise<ConnCheckRawResult[]> {
30+
const results = await Promise.allSettled(
31+
urls.map(url =>
32+
got(url, {
33+
timeout: 3000,
34+
headers: {
35+
'User-Agent': 'Mozilla/5.0 (LISA zephyr plugin/2.1.0)'
36+
},
37+
throwHttpErrors: true
38+
}).then((response: Response) => {
39+
const status = response.statusCode;
40+
return {
41+
url,
42+
ok: (status >= 200 && status < 310) || status === 401,
43+
};
44+
}).catch(() => ({
45+
url,
46+
ok: false
47+
}))
48+
)
49+
);
50+
51+
return results.map(result => {
52+
if (result.status === 'fulfilled') {
53+
return result.value;
54+
}
55+
return {
56+
url: urls[results.indexOf(result)],
57+
ok: false,
58+
};
59+
});
60+
}

0 commit comments

Comments
 (0)