Skip to content

Commit

Permalink
fix: Actually ship the correct package.json files to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeris committed Oct 5, 2023
1 parent 1f269be commit f973cc1
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
1 change: 1 addition & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
],
"commit": false,
"linked": [["@discordkit/client", "@discordkit/core"]],
"ignore": ["webhook-action", "with-nextjs"],
"access": "public",
"baseBranch": "main"
}
6 changes: 6 additions & 0 deletions .changeset/quiet-cherries-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@discordkit/client": patch
"@discordkit/core": patch
---

Added a new versioning script to workaround `@changeset/cli`'s inability to correctly run `yarn npm publish`, which accidentally leaves workspace protocol dependency versions unchanged and published to npm. Shoutout to https://github.com/PrairieLearn/PrairieLearn/pull/7533 for the fix.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Announce new release to Discord Channel
uses: ./apps/webhook-action/action.yml
uses: ./apps/webhook-action
if: steps.changesets.outputs.published == 'true'
with:
webhook: ${{ secrets.DISCORD_WEBHOOK_ID }}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build:packages": "turbo run build",
"lint": "eslint \"./{apps,examples,packages,scripts}/**/*.{j,t}s?(x)\" --cache",
"format": "yarn lint --fix",
"release": "turbo run build build:cjs && changeset publish",
"release": "turbo run build build:cjs && tsx ./scripts/version.ts && changeset publish",
"test": "vitest --single-thread",
"version": "changeset version"
},
Expand Down Expand Up @@ -52,6 +52,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.5",
"tsx": "^3.13.0",
"turbo": "1.10.14",
"type-fest": "^4.3.3",
"typescript": "5.2.2",
Expand Down
84 changes: 84 additions & 0 deletions scripts/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Adapted from https://github.com/PrairieLearn/PrairieLearn/pull/7533
//
// Changesets doesn't currently support workspace versions:
// https://github.com/changesets/changesets/issues/432
// https://github.com/changesets/action/issues/246
// To work around that, we'll manually resolve any `workspace:` version ranges
// with this tool prior to publishing. If/when changesets adds native support
// for publishing with Yarn 3, we can remove this script.
// We'll only support the `workspace:^` range, which is the only one we
// generally want to use.

import cp from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import util from "node:util";

const DEPENDENCY_TYPES = [
`dependencies`,
`devDependencies`,
`peerDependencies`
];

const exec = util.promisify(cp.exec);

const rawWorkspaces = await exec(`yarn workspaces list --json`, {
encoding: `utf8`
});

const workspaces = rawWorkspaces.stdout
.trim()
.split(`\n`)
.map((line) => JSON.parse(line))
.filter((workspace) => workspace.location !== `.`);

// Get the version of each workspace package.
const workspaceVersions = new Map(
await Promise.all(
workspaces.map<Promise<[name: string, version: string]>>(
async (workspace) => {
const packageJsonPath = path.join(workspace.location, `package.json`);
const packageJson = JSON.parse(
await fs.readFile(packageJsonPath, `utf8`)
);
return [workspace.name, packageJson.version];
}
)
)
);

// Replace any `workspace:^` version ranges with the actual version.
await Promise.all(
workspaces.map(async (workspace) => {
const packageJsonPath = path.join(workspace.location, `package.json`);
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, `utf8`));

for (const dependencyType of DEPENDENCY_TYPES) {
const dependencies = Object.keys(packageJson[dependencyType] ?? {});
for (const dependency of dependencies) {
const dependencyVersion = packageJson[dependencyType][dependency];
if (dependencyVersion.startsWith(`workspace:`)) {
if (!dependencyVersion.startsWith(`workspace:^`)) {
throw new Error(
`Unsupported workspace version range: ${dependencyVersion}`
);
}

const realVersion = workspaceVersions.get(dependency);
if (!realVersion) {
throw new Error(
`Could not find version for workspace ${dependency}`
);
}

packageJson[dependencyType][dependency] = `^${realVersion}`;
}
}
}

await fs.writeFile(
packageJsonPath,
`${JSON.stringify(packageJson, null, 2)}\n`
);
})
);
41 changes: 38 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,13 @@ __metadata:
languageName: node
linkType: hard

"buffer-from@npm:^1.0.0":
version: 1.1.2
resolution: "buffer-from@npm:1.1.2"
checksum: 0448524a562b37d4d7ed9efd91685a5b77a50672c556ea254ac9a6d30e3403a517d8981f10e565db24e8339413b43c97ca2951f10e399c6125a0d8911f5679bb
languageName: node
linkType: hard

"buffer@npm:^5.5.0":
version: 5.7.1
resolution: "buffer@npm:5.7.1"
Expand Down Expand Up @@ -2787,6 +2794,7 @@ __metadata:
react: ^18.2.0
react-dom: ^18.2.0
rimraf: ^5.0.5
tsx: ^3.13.0
turbo: 1.10.14
type-fest: ^4.3.3
typescript: 5.2.2
Expand Down Expand Up @@ -3045,7 +3053,7 @@ __metadata:
languageName: node
linkType: hard

"esbuild@npm:^0.18.2":
"esbuild@npm:^0.18.2, esbuild@npm:~0.18.20":
version: 0.18.20
resolution: "esbuild@npm:0.18.20"
dependencies:
Expand Down Expand Up @@ -3872,7 +3880,7 @@ __metadata:
languageName: node
linkType: hard

"get-tsconfig@npm:^4.5.0":
"get-tsconfig@npm:^4.5.0, get-tsconfig@npm:^4.7.2":
version: 4.7.2
resolution: "get-tsconfig@npm:4.7.2"
dependencies:
Expand Down Expand Up @@ -6704,6 +6712,16 @@ __metadata:
languageName: node
linkType: hard

"source-map-support@npm:^0.5.21":
version: 0.5.21
resolution: "source-map-support@npm:0.5.21"
dependencies:
buffer-from: ^1.0.0
source-map: ^0.6.0
checksum: 43e98d700d79af1d36f859bdb7318e601dfc918c7ba2e98456118ebc4c4872b327773e5a1df09b0524e9e5063bb18f0934538eace60cca2710d1fa687645d137
languageName: node
linkType: hard

"source-map@npm:0.8.0-beta.0":
version: 0.8.0-beta.0
resolution: "source-map@npm:0.8.0-beta.0"
Expand All @@ -6713,7 +6731,7 @@ __metadata:
languageName: node
linkType: hard

"source-map@npm:^0.6.1":
"source-map@npm:^0.6.0, source-map@npm:^0.6.1":
version: 0.6.1
resolution: "source-map@npm:0.6.1"
checksum: 59ce8640cf3f3124f64ac289012c2b8bd377c238e316fb323ea22fbfe83da07d81e000071d7242cad7a23cd91c7de98e4df8830ec3f133cb6133a5f6e9f67bc2
Expand Down Expand Up @@ -7303,6 +7321,23 @@ __metadata:
languageName: node
linkType: hard

"tsx@npm:^3.13.0":
version: 3.13.0
resolution: "tsx@npm:3.13.0"
dependencies:
esbuild: ~0.18.20
fsevents: ~2.3.3
get-tsconfig: ^4.7.2
source-map-support: ^0.5.21
dependenciesMeta:
fsevents:
optional: true
bin:
tsx: dist/cli.mjs
checksum: ef858574e110d16a7ca9bbb3a5edeeafd37026ebed6cdd390c4707ad64005024c55fd3142a8f89732c79176196afa7d27073f586966ff0cae0126159ebd58be4
languageName: node
linkType: hard

"tty-table@npm:^4.1.5":
version: 4.2.2
resolution: "tty-table@npm:4.2.2"
Expand Down

0 comments on commit f973cc1

Please sign in to comment.