-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
binding: upgrade ruby implementation, add error on upgrading deps
- Loading branch information
Showing
4 changed files
with
43 additions
and
1,225 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
import { tryreturn } from "rcompat/async"; | ||
import { to } from "rcompat/object"; | ||
import { packager } from "rcompat/meta"; | ||
import { packager, manifest } from "rcompat/meta"; | ||
import { File } from "rcompat/fs"; | ||
import errors from "../errors.js"; | ||
|
||
const { MissingDependencies } = errors; | ||
const MODULE_NOT_FOUND = "ERR_MODULE_NOT_FOUND"; | ||
// this isn't a complete semver comparison, but should work for most cases | ||
const semver = (target, is) => { | ||
if (target.startsWith("^")) { | ||
return semver(Number(target.slice(1), is)); | ||
} | ||
if (is.startsWith("^")) { | ||
return semver(target, Number(is.slice(1))); | ||
} | ||
|
||
return is >= target; | ||
}; | ||
|
||
export default async (dependencies, from) => { | ||
const modules = Object.keys(dependencies); | ||
const compare_dependencies = (target, current) => | ||
to(target).filter(([key, value]) => !semver(value, current[key])); | ||
|
||
const results = await Promise.all(modules.map(module => | ||
tryreturn(_ => import(module)) | ||
.orelse(({ code }) => code === MODULE_NOT_FOUND ? module : {}))); | ||
const find_dependencies = (target, current) => | ||
to(target).filter(([key]) => !current[key]); | ||
|
||
const { MissingDependencies, UpgradeDependencies } = errors; | ||
|
||
export default async (target_dependencies, from) => { | ||
const { dependencies } = await (await File.root()).join(manifest).json(); | ||
|
||
const versions = find_dependencies(target_dependencies, dependencies); | ||
if (versions.length > 0) { | ||
const keys = versions.map(([key]) => key); | ||
const to_upgrade = versions.map(([key, value]) => `${key}@${value}`); | ||
const install = `${packager} install ${to_upgrade.join(" ")}`; | ||
MissingDependencies.throw(keys.join(", "), from, install); | ||
} | ||
|
||
const errored = results.filter(result => typeof result === "string"); | ||
const versions = to(dependencies) | ||
.filter(([dependency]) => errored.includes(dependency)) | ||
.map(([key, value]) => `${key}@${value}`); | ||
if (errored.length > 0) { | ||
const install = module => `${packager} install ${module.join(" ")}`; | ||
MissingDependencies.throw(errored.join(", "), from, install(versions)); | ||
const upgradeable = compare_dependencies(target_dependencies, dependencies); | ||
if (upgradeable.length > 0) { | ||
const keys = upgradeable.map(([key]) => key); | ||
const to_upgrade = upgradeable.map(([key, value]) => `${key}@${value}`); | ||
const install = `${packager} install ${to_upgrade.join(" ")}`; | ||
UpgradeDependencies.throw(keys.join(", "), from, install); | ||
} | ||
return results.filter(result => typeof result !== "string"); | ||
}; |
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
Oops, something went wrong.