Skip to content

Commit f642165

Browse files
committed
doc: deploy github pages
1 parent 9a79211 commit f642165

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

config/config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ export default {
3737
dynamicImport: {},
3838
manifest: {},
3939
hash: true,
40-
publicPath: './',
4140
alias: {
4241
ahooks: process.cwd() + '/packages/hooks/src/index.ts',
4342
'@ahooksjs/use-url-state': process.cwd() + '/packages/use-url-state/src/index.ts',
4443
},
4544
resolve: {
4645
includes: ['docs', 'packages/hooks/src', 'packages/use-url-state'],
4746
},
47+
publicPath: '/',
4848
links: [
4949
{
5050
rel: 'stylesheet',
5151
href: 'https://unpkg.com/@alifd/[email protected]/dist/next-noreset.min.css',
5252
},
53-
{ rel: 'stylesheet', href: './style.css' },
53+
{ rel: 'stylesheet', href: '/style.css' },
5454
],
5555
navs: {
5656
'zh-CN': [
@@ -70,7 +70,7 @@ export default {
7070
],
7171
},
7272
{ title: '更新日志', path: 'https://github.com/alibaba/hooks/releases' },
73-
{ title: '国内镜像', path: 'https://ahooks.pages.dev' },
73+
{ title: '备用镜像', path: 'https://alibaba.github.io/hooks/' },
7474
{ title: 'GitHub', path: 'https://github.com/alibaba/hooks' },
7575
],
7676
'en-US': [
@@ -90,7 +90,7 @@ export default {
9090
],
9191
},
9292
{ title: 'Releases', path: 'https://github.com/alibaba/hooks/releases' },
93-
{ title: '国内镜像', path: 'https://ahooks.pages.dev' },
93+
{ title: '国内镜像', path: 'https://alibaba.github.io/hooks/' },
9494
{ title: 'GitHub', path: 'https://github.com/alibaba/hooks' },
9595
],
9696
},

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
"lint": "eslint --ignore-pattern **/__tests__/* --ignore-pattern **/demo/* \"packages/*/src/**/*.{ts,tsx}\" --cache",
2222
"pretty": "biome format --fix --staged",
2323
"build:doc": "set NODE_OPTIONS=--openssl-legacy-provider && dumi build",
24+
"build:doc-github": "node scripts/build-with-relative-paths.js",
2425
"pub:doc-surge": "surge ./dist --domain ahooks.js.org",
2526
"pub:doc-gitee": "cd ./dist && rm -rf .git && touch .spa && touch .nojekyll && git init && git remote add origin [email protected]:ahooks/ahooks.git && git add -A && git commit -m \"publish docs\" && git push origin main -f && echo https://gitee.com/ahooks/ahooks/pages",
26-
"pub:doc": "pnpm run build:doc && pnpm run pub:doc-surge && pnpm run pub:doc-gitee",
27+
"pub:doc": "pnpm run build:doc && pnpm run pub:doc-surge && pnpm run build:doc-github",
2728
"pub": "pnpm run build && pnpm -r --filter=./packages/* publish",
2829
"pub:beta": "pnpm run build && pnpm -r --filter=./packages/* publish --tag beta",
2930
"preinstall": "npx only-allow pnpm",

packages/hooks/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ahooks",
3-
"version": "3.8.2",
3+
"version": "3.8.4",
44
"description": "react hooks library",
55
"keywords": ["ahooks", "umi hooks", "react hooks"],
66
"main": "./lib/index.js",

scripts/build-with-relative-paths.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { execSync } = require('child_process');
4+
5+
const configPath = path.join(__dirname, '../config/config.ts');
6+
const backupPath = path.join(__dirname, '../config/config.ts.backup');
7+
8+
// 备份原配置
9+
fs.copyFileSync(configPath, backupPath);
10+
11+
try {
12+
// 读取配置文件
13+
let config = fs.readFileSync(configPath, 'utf8');
14+
15+
// 修改配置
16+
config = config.replace(
17+
/publicPath: ['"].*['"],/,
18+
"publicPath: '/hooks/',"
19+
);
20+
config = config.replace(
21+
/{ rel: 'stylesheet', href: '\/style\.css' }/,
22+
"{ rel: 'stylesheet', href: '/hooks/style.css' }"
23+
);
24+
config = config.replace(
25+
/logo: '\/logo\.svg',/,
26+
"logo: '/hooks/logo.svg',"
27+
);
28+
29+
// 写入修改后的配置
30+
fs.writeFileSync(configPath, config);
31+
32+
// 运行构建命令
33+
execSync('pnpm run build:doc', { stdio: 'inherit' });
34+
35+
// 进入 dist 目录
36+
process.chdir(path.join(__dirname, '../dist'));
37+
38+
// 初始化 git 仓库(如果不存在)
39+
try {
40+
execSync('git init', { stdio: 'inherit' });
41+
} catch (e) {
42+
// 如果已经初始化过,忽略错误
43+
}
44+
45+
// 添加所有文件
46+
execSync('git add .', { stdio: 'inherit' });
47+
48+
// 提交更改
49+
execSync('git commit -m "chore: update gh-pages"', { stdio: 'inherit' });
50+
51+
// 添加远程仓库(如果不存在)
52+
try {
53+
execSync('git remote add origin [email protected]:alibaba/hooks.git', { stdio: 'inherit' });
54+
} catch (e) {
55+
// 如果远程仓库已存在,忽略错误
56+
}
57+
58+
// 强制推送到 gh-pages 分支
59+
execSync('git push -f origin HEAD:gh-pages', { stdio: 'inherit' });
60+
61+
// 返回到项目根目录
62+
process.chdir(path.join(__dirname, '..'));
63+
64+
} finally {
65+
// 恢复原配置
66+
fs.copyFileSync(backupPath, configPath);
67+
fs.unlinkSync(backupPath);
68+
}

0 commit comments

Comments
 (0)