Skip to content

Commit

Permalink
chore: workflow and script for pkg.pr.new
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 17, 2024
1 parent 0ba4a74 commit 3f76838
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/preview-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Publish Preview Release
on:
pull_request:

jobs:
preview-release:
if: github.repository == 'sveltejs/cli'
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- name: checkout code repository
uses: actions/checkout@v4

- uses: pnpm/action-setup@v4

- name: setup node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- name: Run changed-files
id: changed-files
uses: tj-actions/changed-files@v44
with:
separator: ' '
dir_names: 'true'
dir_names_max_depth: '2' # truncates the path to adders/X
files: |
packages/**
- name: install dependencies
run: pnpm install

- name: build
run: pnpm build

- name: publish preview
if: ${{ steps.changed-files.outputs.all_changed_files_count > 0 }}
env:
CHANGED_DIRS: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
node scripts/get-deps-to-publish.js
61 changes: 61 additions & 0 deletions scripts/get-deps-to-publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* This tool is used by the pr ci to determine the packages that need to be published to the pkg-pr-new registry.
* In order to avoid situations where only @svelte-cli/core would be published, because it's the only modified package,
* this tool will also determine the dependent packages and also publish those.
* PR: https://github.com/svelte-add/svelte-add/pull/408
*/

// @ts-check
import { execSync } from 'node:child_process';
import { relative, join } from 'node:path';
import { existsSync } from 'node:fs';

if (!process.env.CHANGED_DIRS) throw new Error('CHANGED_DIRS is missing');

const json = execSync(`pnpm -r list --only-projects --json`).toString('utf8');
const repoPackages =
/** @type {Array<import("../packages/core/utils/common.ts").Package & { path: string, private: boolean, peerDependencies?: Record<string, string> }>} */ (
JSON.parse(json)
);

const modifiedDirs = process.env.CHANGED_DIRS.split(' ')
.map((dir) => (dir.startsWith('packages/adders') ? 'packages/adders' : dir))
.filter((dir) => existsSync(join(dir, 'package.json')));
const packagesToPublish = new Set(modifiedDirs);

// keep looping until we've acquired all dependents
let prev = 0;
while (packagesToPublish.size !== prev) {
prev = packagesToPublish.size;
for (const pkg of packagesToPublish) {
const dependents = getDependents(pkg);
dependents.forEach((dep) => packagesToPublish.add(dep));
}
}

// publishes packages to pkg-pr-new
const paths = Array.from(packagesToPublish)
// remove all private packages
.filter((dir) => repoPackages.find((pkg) => pkg.path.endsWith(dir))?.private === false)
.join(' ');

execSync(`pnpm dlx [email protected] publish --pnpm ${paths}`, { stdio: 'inherit' });

/**
* Finds all dependents and returns their relative paths.
* @param {string} path
* @return {string[]}
*/
function getDependents(path) {
const pkg = repoPackages.find((pkg) => pkg.path.endsWith(path));
if (!pkg) throw new Error(`package ${path} doesn't exist in this repo`);

const dependents = repoPackages.filter(
(dep) =>
!dep.private &&
(dep.dependencies?.[pkg.name] ||
dep.devDependencies?.[pkg.name] ||
dep.peerDependencies?.[pkg.name]),
);
return dependents.map((dep) => relative('.', dep.path));
}

0 comments on commit 3f76838

Please sign in to comment.