Skip to content

Commit d7e0ca1

Browse files
committed
Add support for GitHub Pages
1 parent dbcaf1f commit d7e0ca1

File tree

3 files changed

+107
-23
lines changed

3 files changed

+107
-23
lines changed

src/index.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ type Config = {
1818
githubUrl: string
1919
githubIssues: string
2020
githubRepository: string
21+
githubPagesOrigin: string
22+
docsBase: string
2123
includeDocs: boolean
24+
includeGithubPages: boolean
2225
includePlayground: boolean
2326
includeExamples: boolean
2427
}
@@ -27,9 +30,10 @@ async function init() {
2730
const cwd = process.cwd()
2831

2932
let result: {
30-
packageName?: string
31-
githubPath?: string
33+
packageName: string
34+
githubPath: string
3235
includeDocs: boolean
36+
includeGithubPages?: boolean
3337
includePlayground: boolean
3438
includeExamples: boolean
3539
} = {}
@@ -54,6 +58,13 @@ async function init() {
5458
initial: true,
5559
active: 'Yes',
5660
inactive: 'No'
61+
}, {
62+
name: 'includeGithubPages',
63+
type: (prev) => prev ? 'toggle' : null,
64+
message: 'Include GitHub Pages config for documentation?',
65+
initial: false,
66+
active: 'Yes',
67+
inactive: 'No'
5768
}, {
5869
name: 'includePlayground',
5970
type: 'toggle',
@@ -102,9 +113,12 @@ async function init() {
102113
const globalVariableName = projectName.replace(/ /g, '')
103114
const targetDirName = unscopedPackageName
104115

116+
const [githubUserName, githubRepoName] = (githubPath || '/').split('/')
105117
const githubUrl = githubPath ? `https://github.com/${githubPath}` : ''
106118
const githubIssues = githubPath ? `${githubUrl}/issues` : ''
107119
const githubRepository = githubPath ? `git+${githubUrl}.git` : ''
120+
const githubPagesOrigin = githubUserName && result.includeGithubPages ? `https://${githubUserName}.github.io` : ''
121+
const docsBase = githubRepoName && result.includeGithubPages ? `/${githubRepoName}/` : '/'
108122

109123
const targetDirPath = path.join(cwd, targetDirName)
110124

@@ -130,7 +144,10 @@ async function init() {
130144
githubUrl,
131145
githubIssues,
132146
githubRepository,
147+
githubPagesOrigin,
148+
docsBase,
133149
includeDocs: result.includeDocs,
150+
includeGithubPages: !!result.includeGithubPages,
134151
includePlayground: result.includePlayground,
135152
includeExamples: result.includeExamples
136153
}
@@ -141,6 +158,10 @@ async function init() {
141158
copyTemplate('vitepress', config)
142159
}
143160

161+
if (config.includeGithubPages) {
162+
copyTemplate('gh-pages', config)
163+
}
164+
144165
if (config.includePlayground) {
145166
copyTemplate('playground', config)
146167
}
@@ -162,10 +183,14 @@ function copyTemplate(templateName: string, config: Config) {
162183
}
163184

164185
for (const dir of dirs) {
165-
copyFiles('', {
166-
...config,
167-
templateDirPath: path.join(config.templateDirPath, templateName, dir)
168-
})
186+
const templateDirPath = path.join(config.templateDirPath, templateName, dir)
187+
188+
if (fs.existsSync(templateDirPath)) {
189+
copyFiles('', {
190+
...config,
191+
templateDirPath
192+
})
193+
}
169194
}
170195
}
171196

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Simple workflow for deploying static content to GitHub Pages
2+
name: Deploy to GitHub Pages
3+
4+
on:
5+
# Runs on pushes targeting the default branch
6+
push:
7+
branches: ['main']
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow only one concurrent deployment
19+
concurrency:
20+
group: 'pages'
21+
cancel-in-progress: false
22+
23+
jobs:
24+
# Single deploy job since we're just deploying
25+
deploy:
26+
environment:
27+
name: github-pages
28+
url: ${{ steps.deployment.outputs.page_url }}
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@v4
33+
- name: Install pnpm
34+
uses: pnpm/action-setup@v4
35+
with:
36+
version: 9
37+
- name: Set up Node
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: 20
41+
cache: 'pnpm'
42+
- name: Install dependencies
43+
run: pnpm install
44+
- name: Build
45+
run: pnpm run docs:build
46+
- name: Setup Pages
47+
uses: actions/configure-pages@v4
48+
- name: Upload artifact
49+
uses: actions/upload-pages-artifact@v3
50+
with:
51+
# Upload docs dist directory
52+
path: './packages/docs/dist'
53+
- name: Deploy to GitHub Pages
54+
id: deployment
55+
uses: actions/deploy-pages@v4

src/template/vitepress/config/packages/docs/.vitepress/config.mts renamed to src/template/vitepress/config/packages/docs/.vitepress/config.mts.ejs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@ import { defineConfigWithTheme } from 'vitepress'
55
export default ({ mode }: { mode: string }) => defineConfigWithTheme({
66
srcDir: './src',
77
outDir: './dist',
8-
base: '/@unscopedPackageName@',
9-
title: '@scopedPackageName@',
8+
base: '<%- config.docsBase %>',
9+
title: '<%- config.scopedPackageName %>',
1010
lang: 'en-US',
1111
description: 'Description',
1212
cleanUrls: true,
1313

14-
// sitemap: {
15-
// hostname: 'https://???.github.io'
16-
// },
14+
<%_ if (config.githubPagesOrigin) { _%>
15+
sitemap: {
16+
hostname: '<%- config.githubPagesOrigin %><%- config.docsBase %>'
17+
},
18+
19+
transformHead({ page}) {
20+
if (page !== '404.md') {
21+
const canonicalUrl = `<%- config.githubPagesOrigin %><%- config.docsBase %>${page}`
22+
.replace(/index\.md$/, '')
23+
.replace(/\.md$/, '')
1724
18-
// transformHead({ page}) {
19-
// if (page !== '404.md') {
20-
// const canonicalUrl = `https://???.github.io/${page}`
21-
// .replace(/index\.md$/, '')
22-
// .replace(/\.md$/, '')
23-
//
24-
// return [['link', { rel: 'canonical', href: canonicalUrl }]]
25-
// }
26-
// },
25+
return [['link', { rel: 'canonical', href: canonicalUrl }]]
26+
}
27+
},
2728
29+
<%_ } _%>
2830
vite: {
2931
resolve: {
3032
alias: {
31-
'@scopedPackageName@': resolve(__dirname, '../../@projectName@/src/index.ts')
33+
'<%- config.scopedPackageName %>': resolve(__dirname, '../../<%- config.shortUnscopedPackageName %>/src/index.ts')
3234
}
3335
},
3436

@@ -40,7 +42,7 @@ export default ({ mode }: { mode: string }) => defineConfigWithTheme({
4042
fs: {
4143
allow: [
4244
'..',
43-
'../../@projectName@/src'
45+
'../../<%- config.shortUnscopedPackageName %>/src'
4446
]
4547
}
4648
}
@@ -55,10 +57,12 @@ export default ({ mode }: { mode: string }) => defineConfigWithTheme({
5557
{ text: 'Guide', link: '/introduction' }
5658
],
5759

60+
<%_ if (config.githubUrl) { _%>
5861
socialLinks: [
59-
{ icon: 'github', link: '@githubUrl@' }
62+
{ icon: 'github', link: '<%- config.githubUrl %>' }
6063
],
6164
65+
<%_ } _%>
6266
sidebar: [
6367
{
6468
text: 'Getting started',

0 commit comments

Comments
 (0)