Skip to content

Commit

Permalink
chore: Add publish tool
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Feb 3, 2025
1 parent 947b379 commit a601318
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 213 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ jobs:
npm run build
if: ${{ steps.release.outputs.releases_created == 'true' }}
#-----------------------------------------------------------------------------
# NOTE: This script currently doesn't do anything. It just outputs the
# release information to the console. We will do this for a few releases
# to make sure everything is working correctly before we switch to use this
# script exclusively.
#-----------------------------------------------------------------------------

- name: Publish using new script
run: node scripts/publish.js --dry-run
if: ${{ steps.release.outputs.releases_created == 'true' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
TWITTER_API_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
TWITTER_API_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
TWITTER_ACCESS_TOKEN_KEY: ${{ secrets.TWITTER_ACCESS_TOKEN_KEY }}
TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
MASTODON_HOST: ${{ secrets.MASTODON_HOST }}
BLUESKY_IDENTIFIER: ${{ vars.BLUESKY_IDENTIFIER }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
BLUESKY_HOST: ${{ vars.BLUESKY_HOST }}

#-----------------------------------------------------------------------------
# NOTE: Packages are released in order of dependency. The packages with the
# fewest internal dependencies are released first and the packages with the
Expand Down
92 changes: 5 additions & 87 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,98 +10,16 @@
//------------------------------------------------------------------------------

import { execSync } from "node:child_process";
import path from "node:path";
import fsp from "node:fs/promises";
import { fileURLToPath } from "node:url";

//-----------------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------------

const __filename = fileURLToPath(import.meta.url); // eslint-disable-line no-underscore-dangle -- convention
const __dirname = path.dirname(__filename); // eslint-disable-line no-underscore-dangle -- convention
const PACKAGES_DIR = path.resolve(__dirname, "..", "packages");
import {
getPackageDirs,
calculatePackageDependencies,
createBuildOrder,
} from "./shared.js";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Gets a list of directories in the packages directory.
* @returns {Promise<string[]>} A promise that resolves with an array of package directories.
*/
export async function getPackageDirs() {
const packageDirs = await fsp.readdir(PACKAGES_DIR);
return packageDirs.map(entry => `packages/${entry}`);
}

/**
* Calculates the dependencies between packages.
* @param {Array<string>} packageDirs An array of package directories.
* @returns {Map<string, Set<string>>} A map of package names to the set of dependencies.
*/
export async function calculatePackageDependencies(packageDirs) {
return new Map(
await Promise.all(
packageDirs.map(async packageDir => {
const packageJson = await fsp.readFile(
path.join(packageDir, "package.json"),
"utf8",
);
const pkg = JSON.parse(packageJson);
const dependencies = new Set();

if (pkg.dependencies) {
for (const dep of Object.keys(pkg.dependencies)) {
dependencies.add(dep);
}
}

if (pkg.devDependencies) {
for (const dep of Object.keys(pkg.devDependencies)) {
dependencies.add(dep);
}
}

return [
pkg.name,
{ name: pkg.name, dir: packageDir, dependencies },
];
}),
),
);
}

/**
* Creates an array of directories to be built in order to satisfy dependencies.
* @param {Map<string,{name:string,dir:string,dependencies:Set<string>}>} dependencies The
* dependencies between packages.
* @returns {Array<string>} An array of directories to be built in order.
*/
export function createBuildOrder(dependencies) {
const buildOrder = [];
const seen = new Set();

function visit(name) {
if (!seen.has(name)) {
seen.add(name);

// we only need to deal with dependencies in this monorepo
if (dependencies.has(name)) {
const { dependencies: deps, dir } = dependencies.get(name);
deps.forEach(visit);
buildOrder.push(dir);
}
}
}

dependencies.forEach((value, key) => {
visit(key);
});

return buildOrder;
}

/**
* Builds the packages in the correct order.
* @param {Array<string>} packageDirs An array of directories to build in order.
Expand Down
Loading

0 comments on commit a601318

Please # to comment.