Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(ci): install again when npm skips a platform package

spacesdrive60c969c1b79e
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f24c505..76d83b8 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -21,7 +21,14 @@ jobs:
cache-dependency-path: package-lock.json
- name: Install dependencies
- run: npm ci
+ # npm skips an optional package it fails to download, so a missing platform binary gets one more install
+ run: |
+ npm ci
+ if ! node scripts/check-platform-packages.ts; then
+ echo "::warning::npm skipped a platform package; installing again"
+ npm ci
+ node scripts/check-platform-packages.ts
+ fi
- name: Type check
run: npm run typecheck
diff --git a/.github/workflows/deploy-backend.yml b/.github/workflows/deploy-backend.yml
index b50ce8c..4b1f00a 100644
--- a/.github/workflows/deploy-backend.yml
+++ b/.github/workflows/deploy-backend.yml
@@ -27,7 +27,14 @@ jobs:
# one install at the root covers every workspace, including wrangler
- name: Install dependencies
- run: npm ci
+ # npm skips an optional package it fails to download, so a missing platform binary gets one more install
+ run: |
+ npm ci
+ if ! node scripts/check-platform-packages.ts; then
+ echo "::warning::npm skipped a platform package; installing again"
+ npm ci
+ node scripts/check-platform-packages.ts
+ fi
# deploy only code that type-checks; Wrangler and Vite strip types without checking them
- name: Type check
diff --git a/.github/workflows/deploy-frontend.yml b/.github/workflows/deploy-frontend.yml
index 1383dc7..8275274 100644
--- a/.github/workflows/deploy-frontend.yml
+++ b/.github/workflows/deploy-frontend.yml
@@ -27,7 +27,14 @@ jobs:
# one install at the root covers every workspace, including wrangler
- name: Install dependencies
- run: npm ci
+ # npm skips an optional package it fails to download, so a missing platform binary gets one more install
+ run: |
+ npm ci
+ if ! node scripts/check-platform-packages.ts; then
+ echo "::warning::npm skipped a platform package; installing again"
+ npm ci
+ node scripts/check-platform-packages.ts
+ fi
# deploy only code that type-checks; Wrangler and Vite strip types without checking them
- name: Type check
diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml
index 4be6ee2..c1a8b18 100644
--- a/.github/workflows/deploy-site.yml
+++ b/.github/workflows/deploy-site.yml
@@ -26,7 +26,14 @@ jobs:
# one install at the root covers every workspace, including wrangler
- name: Install dependencies
- run: npm ci
+ # npm skips an optional package it fails to download, so a missing platform binary gets one more install
+ run: |
+ npm ci
+ if ! node scripts/check-platform-packages.ts; then
+ echo "::warning::npm skipped a platform package; installing again"
+ npm ci
+ node scripts/check-platform-packages.ts
+ fi
- name: Type check
run: npm run typecheck --workspace site
diff --git a/scripts/check-platform-packages.ts b/scripts/check-platform-packages.ts
new file mode 100644
index 0000000..5583622
--- /dev/null
+++ b/scripts/check-platform-packages.ts
@@ -0,0 +1,60 @@
+#!/usr/bin/env node
+// Checks that npm installed every optional package the lockfile lists for this machine. npm skips an optional
+// package it fails to download without failing the install, so a platform binary (workerd, esbuild, rollup) can
+// be missing after a successful `npm ci`. Exits 1 and names the missing packages so the workflow can install again.
+
+import { existsSync, readFileSync } from 'node:fs';
+
+interface LockPackage {
+ optional?: boolean;
+ os?: string[];
+ cpu?: string[];
+ libc?: string[];
+}
+
+interface Lockfile {
+ packages: Record<string, LockPackage>;
+}
+
+type Libc = 'glibc' | 'musl' | null;
+
+function machineLibc(): Libc {
+ if (process.platform !== 'linux') return null;
+ const header = (process.report.getReport() as { header?: { glibcVersionRuntime?: string } }).header;
+ return header?.glibcVersionRuntime ? 'glibc' : 'musl';
+}
+
+// the lockfile does not always record libc, so the package name tells the glibc and musl builds apart
+function packageLibc(path: string, entry: LockPackage): Libc {
+ if (entry.libc?.length) return entry.libc.includes('musl') ? 'musl' : 'glibc';
+ if (/musl/.test(path)) return 'musl';
+ if (/-gnu(?:$|[/-])/.test(path)) return 'glibc';
+ return null;
+}
+
+function matches(list: string[] | undefined, value: string): boolean {
+ if (!list?.length) return true;
+ if (list.includes(`!${value}`)) return false;
+ return list.some(item => !item.startsWith('!')) ? list.includes(value) : true;
+}
+
+const lockfile = JSON.parse(readFileSync('package-lock.json', 'utf8')) as Lockfile;
+const libc = machineLibc();
+
+const expected = Object.entries(lockfile.packages).filter(([path, entry]) => {
+ if (!path || !entry.optional || (!entry.os && !entry.cpu)) return false;
+ if (!matches(entry.os, process.platform) || !matches(entry.cpu, process.arch)) return false;
+ const wanted = packageLibc(path, entry);
+ return wanted === null || libc === null || wanted === libc;
+});
+
+const missing = expected.map(([path]) => path).filter(path => !existsSync(`${path}/package.json`));
+
+if (missing.length > 0) {
+ console.error(`Missing ${missing.length} of ${expected.length} platform packages for ${process.platform}-${process.arch}${libc ? ` (${libc})` : ''}:`);
+ for (const path of missing) console.error(` ${path.replace(/^.*node_modules\//, '')}`);
+ console.error('npm skipped them during the install; run `npm ci` again.');
+ process.exit(1);
+}
+
+console.log(`All ${expected.length} platform packages for ${process.platform}-${process.arch}${libc ? ` (${libc})` : ''} are installed.`);