-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci[minor]: Add script & GH action to validate deno and node deps in s…
…ync (#327) * ci[minor]: Add script & GH action to validate deno and node deps in sync * cr * cr
- Loading branch information
1 parent
3ba0a66
commit fd6233e
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters