Skip to content

Commit

Permalink
ci[minor]: Add script & GH action to validate deno and node deps in s…
Browse files Browse the repository at this point in the history
…ync (#327)

* ci[minor]: Add script & GH action to validate deno and node deps in sync

* cr

* cr
  • Loading branch information
bracesproul authored Aug 16, 2024
1 parent 3ba0a66 commit fd6233e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/validate-new-notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,28 @@ on:
paths:
- 'docs/docs/**'
- 'examples/**'
- 'deno.json'
workflow_dispatch:

env:
NODE_VERSION: "22.4.1"

jobs:
validate-dep-files:
name: Validate Deno and Node dependencies in sync
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'yarn'
- name: Install dependencies
run: yarn install --immutable
- name: Validate
run: yarn tsx --experimental-wasm-modules ./scripts/validate_deps_sync.ts

jobs:
validate-new-notebooks:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"esm-hook": "^0.1.4",
"readline": "^1.3.0",
"release-it": "^17.6.0",
"semver": "^7.5.4",
"semver": "^7.6.3",
"tslab": "^1.0.21",
"tsx": "^4.7.0",
"turbo": "canary",
Expand Down
64 changes: 64 additions & 0 deletions scripts/validate_deps_sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import fs from "fs";
import semver from "semver";

type DenoJson = {
imports: Record<string, string>;
};

type PackageJson = {
// Only adding the fields we care about
devDependencies: Record<string, string>;
};

function main() {
const denoJson: DenoJson = JSON.parse(fs.readFileSync("deno.json", "utf-8"));
const packageJson: PackageJson = JSON.parse(
fs.readFileSync("examples/package.json", "utf-8")
);

// Parse the dependency names and versions from the deno.json file
const denoDeps = Object.entries(denoJson.imports).map(([name, version]) => {
let depName = name.endsWith("/") ? name.slice(0, -1) : name;
let depVersion = version
.replace(/^npm:\/?(.*)/g, "$1")
.replace(depName, "");
depVersion = depVersion.replace(/@.*$/, "").replace(/\/$/, "");
if (!depVersion || depVersion === "") {
depVersion = "latest";
}

// `latest` is not a valid semver, do not validate it
if (depVersion !== "latest" && !semver.valid(depVersion)) {
throw new Error(`Invalid version for ${depName}: ${depVersion}`);
}
return { name: depName, version: depVersion };
});

// Match the dependencies to those in the `package.json` file, and
// use the `semver` package to verify the versions are compatible
denoDeps.forEach((denoDep) => {
if (!(denoDep.name in packageJson.devDependencies)) {
throw new Error(
`Dependency ${denoDep.name} is not in the package.json file`
);
}

const packageVersion = packageJson.devDependencies[denoDep.name];
if (denoDep.version === "latest") {
// If the deno version is latest, we can not validate it. Assume it is correct
return;
}
const cleanedPackageJsonVersion =
semver.clean(packageVersion) ?? packageVersion;
if (
cleanedPackageJsonVersion !== denoDep.version &&
!semver.gte(cleanedPackageJsonVersion, denoDep.version)
) {
throw new Error(
`Version mismatch for ${denoDep.name}: package.json version ${cleanedPackageJsonVersion} is less than deno.json version ${denoDep.version}`
);
}
});
}

main();
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8779,7 +8779,7 @@ __metadata:
esm-hook: ^0.1.4
readline: ^1.3.0
release-it: ^17.6.0
semver: ^7.5.4
semver: ^7.6.3
tslab: ^1.0.21
tsx: ^4.7.0
turbo: canary
Expand Down

0 comments on commit fd6233e

Please sign in to comment.