Skip to content

Commit c42a10a

Browse files
committed
🔧chore(feature): 添加lighthouse命令
1 parent 8c73522 commit c42a10a

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ npm i -g code-genius
1818

1919
## 终端命令
2020

21-
| 命令 | 参数 | 默认值 | 功能描述 |
22-
| -------- | ------------------------------------------------------------------- | --------- | --------------------------------------------------- |
23-
| commit | --no-emoji | true | 生成 angualr 规范的提交信息 |
24-
| verify | -- | -- | 校验 COMMIT_EDITMSG 中的信息是否符合 Angualr 规范 |
25-
| clear | --pattern \<pattern\> | './dist/' | 运行 rimraf 删除不再需要的文件或文件夹 |
26-
| hooks | -- | -- | 新增或修改 simple-git-hooks 配置后需要重新初始化 |
27-
| depcheck | -- | -- | 运行 npm-check 检查过时的、不正确的和未使用的依赖项 |
28-
| run | -- | -- | 列出可以运行的全部脚本 |
29-
| registry | -- | -- | 切换 NPM 镜像地址 |
30-
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
31-
| format | --pattern \<pattern\> | ./src' | 运行 prettier 格式化代码风格 |
32-
| create | -- | -- | 运行 npm create 快速创建基础项目 |
33-
| template | -n, --project-name \<project-name\>, -f, --framework \<framework\>, | -- | 快速创建 CodeGenius 基础项目 |
21+
| 命令 | 参数 | 默认值 | 功能描述 |
22+
| ---------- | ------------------------------------------------------------------- | --------- | --------------------------------------------------- |
23+
| commit | --no-emoji | true | 生成 angualr 规范的提交信息 |
24+
| verify | -- | -- | 校验 COMMIT_EDITMSG 中的信息是否符合 Angualr 规范 |
25+
| clear | --pattern \<pattern\> | './dist/' | 运行 rimraf 删除不再需要的文件或文件夹 |
26+
| hooks | -- | -- | 新增或修改 simple-git-hooks 配置后需要重新初始化 |
27+
| depcheck | -- | -- | 运行 npm-check 检查过时的、不正确的和未使用的依赖项 |
28+
| run | -- | -- | 列出可以运行的全部脚本 |
29+
| registry | -- | -- | 切换 NPM 镜像地址 |
30+
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
31+
| format | --pattern \<pattern\> | ./src' | 运行 prettier 格式化代码风格 |
32+
| create | -- | -- | 运行 npm create 快速创建基础项目 |
33+
| template | -n, --project-name \<project-name\>, -f, --framework \<framework\>, | -- | 快速创建 CodeGenius 基础项目 |
34+
| lighthouse | --url \<url\> | -- | 运行 lighthouse 分析及收集 Web 应用的性能指标 |
3435

3536
## 执照
3637

src/command/lighthouse.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { CAC } from "cac";
2+
3+
import fs from "fs-extra";
4+
5+
import { execCommand, loggerInfo } from "@/shared/index";
6+
import { ACTIVATION } from "@/shared/config";
7+
8+
async function getReportfile(url: string) {
9+
const _url = new URL(url);
10+
const files = await fs.readdirSync(process.cwd());
11+
return files.filter(
12+
(f) => f.startsWith(_url.hostname) && f.endsWith("report.html"),
13+
);
14+
}
15+
16+
export const lighthouse = async (url: string) => {
17+
if (ACTIVATION) {
18+
loggerInfo("lighthouse 参数信息: \n");
19+
console.table(url);
20+
}
21+
22+
const histories = await getReportfile(url);
23+
for (const history of histories) {
24+
await fs.removeSync(history);
25+
}
26+
27+
await execCommand(
28+
"npx",
29+
[
30+
"lighthouse",
31+
url,
32+
"--output=html",
33+
`--output-path=./${new URL(url).hostname}-report.html`,
34+
"--view",
35+
],
36+
{
37+
stdio: "inherit",
38+
},
39+
);
40+
};
41+
42+
export default function lighthouseInstaller(cli: CAC) {
43+
return {
44+
name: "lighthouseInstaller",
45+
setup: () => {
46+
cli
47+
.command("lighthouse", "运行 lighthouse 分析及收集 Web 应用的性能指标")
48+
.option("--url <url>", "Web 应用地址")
49+
.action(async (options) => {
50+
if (options.url) {
51+
await lighthouse(options.url);
52+
}
53+
});
54+
},
55+
};
56+
}

src/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import npmRegistryInstaller from "./command/npm-registry";
1111
import eslintFixInstaller from "./command/eslint-fix";
1212
import prettierFormatInstaller from "./command/prettier-format";
1313
import templateInstaller from "./command/template";
14+
import lighthouseInstaller from "./command/lighthouse";
1415

1516
export function cmdInstaller(cli: CAC) {
1617
gitCommitInstaller(cli).setup();
@@ -24,4 +25,5 @@ export function cmdInstaller(cli: CAC) {
2425
prettierFormatInstaller(cli).setup();
2526
createProjectInstaller(cli).setup();
2627
templateInstaller(cli).setup();
28+
lighthouseInstaller(cli).setup();
2729
}

0 commit comments

Comments
 (0)