From f574f382a5ab958eaa858c248dcefd431ace183e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 2 Nov 2024 17:06:29 -0400 Subject: [PATCH 1/2] create docs-preview-create workflow --- .github/workflows/docs-preview-create.yml | 27 +++++++++++++++++ apps/svelte.dev/scripts/sync-docs/index.ts | 34 +++++++++++++++------- apps/svelte.dev/scripts/sync-docs/utils.ts | 4 +++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/docs-preview-create.yml diff --git a/.github/workflows/docs-preview-create.yml b/.github/workflows/docs-preview-create.yml new file mode 100644 index 0000000000..f6e7c3c207 --- /dev/null +++ b/.github/workflows/docs-preview-create.yml @@ -0,0 +1,27 @@ +name: Docs preview create + +on: + repository_dispatch: + types: [docs-preview-create] + +jobs: + Sync: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: pnpm/action-setup@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: pnpm + - run: pnpm install --frozen-lockfile + + - name: Sync + run: cd apps/svelte.dev && pnpm sync-docs --owner="${{ github.event.client_payload.owner }}" -p "${{ github.event.client_payload.package }}#${{ github.event.client_payload.branch }}"" + + - name: Create branch + uses: peterjgrainger/action-create-branch@v3.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + branch: refs/heads/sync/${{ github.event.client_payload.package }}/${{ github.event.client_payload.owner }}/${{ github.event.client_payload.branch }} diff --git a/apps/svelte.dev/scripts/sync-docs/index.ts b/apps/svelte.dev/scripts/sync-docs/index.ts index c2cccb82c6..44b6d3486f 100644 --- a/apps/svelte.dev/scripts/sync-docs/index.ts +++ b/apps/svelte.dev/scripts/sync-docs/index.ts @@ -30,6 +30,10 @@ const parsed = parseArgs({ pull: { type: 'boolean', short: 'p' + }, + owner: { + type: 'string', + default: 'sveltejs' } }, strict: true, @@ -40,11 +44,23 @@ const dirname = fileURLToPath(new URL('.', import.meta.url)); const REPOS = path.join(dirname, '../../repos'); const DOCS = path.join(dirname, '../../content/docs'); +const branches = {}; + +for (const option of parsed.positionals) { + const [name, ...rest] = option.split('#'); + + if (branches[name]) { + throw new Error(`Duplicate branches for ${name}`); + } + + branches[name] = rest.join('#') || 'main'; +} + const packages: Package[] = [ { name: 'svelte', - repo: 'sveltejs/svelte', - branch: 'main', + repo: `${parsed.values.owner}/svelte`, + branch: branches['svelte'] ?? 'main', pkg: 'packages/svelte', docs: 'documentation/docs', types: 'types', @@ -67,8 +83,8 @@ const packages: Package[] = [ }, { name: 'kit', - repo: 'sveltejs/kit', - branch: 'main', + repo: `${parsed.values.owner}/kit`, + branch: branches['kit'] ?? 'main', pkg: 'packages/kit', docs: 'documentation/docs', types: 'types', @@ -127,15 +143,15 @@ const packages: Package[] = [ }, { name: 'cli', - repo: 'sveltejs/cli', - branch: 'main', + repo: `${parsed.values.owner}/cli`, + branch: branches['cli'] ?? 'main', pkg: 'packages/cli', docs: 'documentation/docs', types: null } ]; -const unknown = parsed.positionals.filter((name) => !packages.some((pkg) => pkg.name === name)); +const unknown = Object.keys(branches).filter((name) => !packages.some((pkg) => pkg.name === name)); if (unknown.length > 0) { throw new Error( @@ -144,9 +160,7 @@ if (unknown.length > 0) { } const filtered = - parsed.positionals.length === 0 - ? packages - : packages.filter((pkg) => parsed.positionals.includes(pkg.name)); + parsed.positionals.length === 0 ? packages : packages.filter((pkg) => !!branches[pkg.name]); /** * Depending on your setup, this will either clone the Svelte and SvelteKit repositories diff --git a/apps/svelte.dev/scripts/sync-docs/utils.ts b/apps/svelte.dev/scripts/sync-docs/utils.ts index 774bb2fe75..93871e874f 100644 --- a/apps/svelte.dev/scripts/sync-docs/utils.ts +++ b/apps/svelte.dev/scripts/sync-docs/utils.ts @@ -8,6 +8,10 @@ export async function clone_repo(repo: string, name: string, branch: string, cwd if (fs.existsSync(dir)) { const opts = { cwd: dir }; + if (!repo.startsWith('sveltejs/')) { + console.warn('Ignoring --owner flag for already-cloned repo'); + } + if (execSync('git status -s', opts).toString() !== '') { throw new Error(`${name} repo is dirty — aborting`); } From 84bce841b49763cf5a5c3573e8e4b09f824b65b2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 2 Nov 2024 17:50:25 -0400 Subject: [PATCH 2/2] maybe it's as simple as this? --- .github/workflows/docs-preview-create.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/docs-preview-create.yml b/.github/workflows/docs-preview-create.yml index f6e7c3c207..9b12c2a589 100644 --- a/.github/workflows/docs-preview-create.yml +++ b/.github/workflows/docs-preview-create.yml @@ -16,12 +16,11 @@ jobs: cache: pnpm - run: pnpm install --frozen-lockfile + - name: Checkout + run: git checkout -B refs/heads/sync/${{ github.event.client_payload.package }}/${{ github.event.client_payload.owner }}/${{ github.event.client_payload.branch }} + - name: Sync - run: cd apps/svelte.dev && pnpm sync-docs --owner="${{ github.event.client_payload.owner }}" -p "${{ github.event.client_payload.package }}#${{ github.event.client_payload.branch }}"" + run: cd apps/svelte.dev && pnpm sync-docs --owner="${{ github.event.client_payload.owner }}" -p "${{ github.event.client_payload.package }}#${{ github.event.client_payload.branch }}" - - name: Create branch - uses: peterjgrainger/action-create-branch@v3.0.0 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - branch: refs/heads/sync/${{ github.event.client_payload.package }}/${{ github.event.client_payload.owner }}/${{ github.event.client_payload.branch }} + - name: Push + run: git add -A && git commit -m "sync docs" && git push -u origin sync/${{ github.event.client_payload.package }}/${{ github.event.client_payload.owner }}/${{ github.event.client_payload.branch }}