Skip to content

Commit 9fa64a9

Browse files
committed
Use content hash vs. freshness in publish registry
1 parent 8331ebe commit 9fa64a9

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
run: yarn lint
3737
- name: test
3838
run: yarn test
39+
publish-registry-dry-run:
40+
needs: build_and_test
41+
uses: ./.github/workflows/publish-registry.yml
42+
with:
43+
dry-run: true
3944
publish_alpha:
4045
name: publish alpha release
4146
runs-on: ubuntu-latest

.github/workflows/publish-registry.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ on:
33
schedule:
44
# https://crontab.guru/#0_0_*_*_0
55
- cron: 0 0 * * 0
6+
workflow_call:
7+
inputs:
8+
dry-run:
9+
type: boolean
610
workflow_dispatch:
11+
inputs:
12+
dry-run:
13+
type: boolean
714
jobs:
815
publish-registry:
916
runs-on: ubuntu-latest
@@ -21,8 +28,8 @@ jobs:
2128
path: packages/utils/cache/
2229
key: cache-${{ github.run_id }}
2330
restore-keys: cache-
24-
- name: Publish registry
25-
run: yarn workspace @definitelytyped/publisher publish-registry
31+
- name: Publish registry${{ (inputs || github.event.inputs).dry-run && ' dry run' || '' }}
32+
run: yarn workspace @definitelytyped/publisher publish-registry${{ (inputs || github.event.inputs).dry-run && ' --dry' || '' }}
2633
env:
2734
NPM_TOKEN: ${{ secrets.NPM_RETAG_TOKEN }}
2835
- if: always()

packages/publisher/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"fstream": "^1.0.12",
2121
"hh-mm-ss": "^1.2.0",
2222
"longjohn": "^0.2.11",
23+
"npm-pick-manifest": "^7.0.1",
2324
"pacote": "^13.6.1",
2425
"semver": "^7.3.7",
2526
"source-map-support": "^0.4.0",

packages/publisher/src/publish-registry.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import assert = require("assert");
22
import process from "process";
33
import { emptyDir } from "fs-extra";
4+
// @ts-expect-error
5+
import pickManifest from "npm-pick-manifest";
46
import * as yargs from "yargs";
57

68
import { defaultLocalOptions, defaultRemoteOptions } from "./lib/common";
@@ -230,10 +232,7 @@ async function generateRegistry(typings: readonly TypingsData[]): Promise<Regist
230232
return {
231233
entries: Object.fromEntries(
232234
await Promise.all(
233-
typings.map(async (typing) => [
234-
typing.name,
235-
filterTags((await pacote.packument(typing.fullNpmName, { cache: cacheDir }))["dist-tags"]),
236-
])
235+
typings.map(async (typing) => [typing.name, filterTags((await revalidate(typing))["dist-tags"])])
237236
)
238237
),
239238
};
@@ -245,6 +244,27 @@ async function generateRegistry(typings: readonly TypingsData[]): Promise<Regist
245244
}
246245
}
247246

247+
/** Fetch packument from the cache or the network, depending on whether the types have changed or not. */
248+
async function revalidate(typing: TypingsData) {
249+
const offline = await pacote
250+
.packument(typing.fullNpmName, { cache: cacheDir, fullMetadata: true, offline: true })
251+
.catch((reason) => {
252+
if (reason.code !== "ENOTCACHED") throw reason;
253+
return undefined;
254+
});
255+
try {
256+
if (
257+
offline &&
258+
pickManifest(offline, `~${typing.major}.${typing.minor}`).typesPublisherContentHash === typing.contentHash
259+
)
260+
return offline;
261+
// @ts-expect-error
262+
} catch (reason: {}) {
263+
if (reason.code !== "ETARGET") throw reason;
264+
}
265+
return pacote.packument(typing.fullNpmName, { cache: cacheDir, fullMetadata: true, preferOnline: true });
266+
}
267+
248268
interface ProcessedNpmInfo {
249269
readonly latestVersion: string;
250270
readonly maxVersion: string;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6952,7 +6952,7 @@ npm-pick-manifest@^6.0.0, npm-pick-manifest@^6.1.1:
69526952
npm-package-arg "^8.1.2"
69536953
semver "^7.3.4"
69546954

6955-
npm-pick-manifest@^7.0.0:
6955+
npm-pick-manifest@^7.0.0, npm-pick-manifest@^7.0.1:
69566956
version "7.0.1"
69576957
resolved "https://registry.yarnpkg.com/npm-pick-manifest/-/npm-pick-manifest-7.0.1.tgz#76dda30a7cd6b99be822217a935c2f5eacdaca4c"
69586958
integrity sha512-IA8+tuv8KujbsbLQvselW2XQgmXWS47t3CB0ZrzsRZ82DbDfkcFunOaPm4X7qNuhMfq+FmV7hQT4iFVpHqV7mg==

0 commit comments

Comments
 (0)