From f4deccd10f50d6d8984224f32a74d69c7bc59e4d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 2 Nov 2024 17:59:14 -0400 Subject: [PATCH] create docs-preview-create workflow (#776) * create docs-preview-create workflow * maybe it's as simple as this? --- .github/workflows/docs-preview-create.yml | 26 +++++++++++++++++ apps/svelte.dev/scripts/sync-docs/index.ts | 34 +++++++++++++++------- apps/svelte.dev/scripts/sync-docs/utils.ts | 4 +++ 3 files changed, 54 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 000000000..9b12c2a58 --- /dev/null +++ b/.github/workflows/docs-preview-create.yml @@ -0,0 +1,26 @@ +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: 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 }}" + + - 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 }} diff --git a/apps/svelte.dev/scripts/sync-docs/index.ts b/apps/svelte.dev/scripts/sync-docs/index.ts index c2cccb82c..44b6d3486 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 774bb2fe7..93871e874 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`); }