Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: workflow and script for pkg.pr.new #4

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 packages/package-name
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
64 changes: 64 additions & 0 deletions scripts/get-deps-to-publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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(' ').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(' ');

if (paths) {
console.log(`publishing ${paths}`);
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));
}