-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
81 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as t from "tap"; | ||
import { Hooks } from "./Wrapper"; | ||
|
||
t.test("package throws error if name is empty", async (t) => { | ||
const hooks = new Hooks(); | ||
|
||
t.throws(() => hooks.package("")); | ||
}); | ||
|
||
t.test("withVersion throws error if version is empty", async (t) => { | ||
const hooks = new Hooks(); | ||
const subject = hooks.package("package"); | ||
|
||
t.throws(() => subject.withVersion("")); | ||
}); | ||
|
||
t.test("method throws error if name is empty", async (t) => { | ||
const hooks = new Hooks(); | ||
const subject = hooks | ||
.package("package") | ||
.withVersion("^1.0.0") | ||
.subject((exports) => exports); | ||
|
||
t.throws(() => subject.method("", () => {})); | ||
}); |
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,30 @@ | ||
import * as t from "tap"; | ||
import { wrap } from "./wrap"; | ||
import { Hooks } from "./Wrapper"; | ||
|
||
t.test("it ignores if package is not installed", async (t) => { | ||
const hooks = new Hooks(); | ||
hooks.package("unknown").withVersion("^1.0.0"); | ||
t.same(wrap(hooks), {}); | ||
}); | ||
|
||
t.test("it ignores if packages have empty selectors", async (t) => { | ||
const hooks = new Hooks(); | ||
hooks.package("shimmer").withVersion("^1.0.0"); | ||
t.same(wrap(hooks), {}); | ||
}); | ||
|
||
t.test("it ignores unknown selectors", async (t) => { | ||
const hooks = new Hooks(); | ||
hooks | ||
.package("shimmer") | ||
.withVersion("^1.0.0") | ||
.subject((exports) => exports.doesNotExist) | ||
.method("method", () => {}); | ||
t.same(wrap(hooks), { | ||
shimmer: { | ||
version: "1.2.1", | ||
supported: true, | ||
}, | ||
}); | ||
}); |
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,72 @@ | ||
import { Hook } from "require-in-the-middle"; | ||
import { wrap as shim } from "shimmer"; | ||
import { getPackageVersion } from "../helpers/getPackageVersion"; | ||
import { satisfiesVersion } from "../helpers/satisfiesVersion"; | ||
import { Hooks, Method } from "./Wrapper"; | ||
|
||
function wrapMethod(subject: unknown, method: Method) { | ||
// @ts-expect-error We don't now the type of the subject | ||
shim(subject, method.getName(), function wrap(original: Function) { | ||
return function wrap() { | ||
// eslint-disable-next-line prefer-rest-params | ||
const args = Array.from(arguments); | ||
// @ts-expect-error We don't now the type of this | ||
const updatedArgs = method.getInterceptor()(args, this); | ||
|
||
return original.apply( | ||
// @ts-expect-error We don't now the type of this | ||
this, | ||
// eslint-disable-next-line prefer-rest-params | ||
Array.isArray(updatedArgs) ? updatedArgs : arguments | ||
); | ||
}; | ||
}); | ||
} | ||
|
||
export function wrap(hooks: Hooks) { | ||
const wrapped: Record<string, { version: string; supported: boolean }> = {}; | ||
|
||
hooks.getPackages().forEach((pkg) => { | ||
const version = getPackageVersion(pkg.getName()); | ||
|
||
if (!version) { | ||
return; | ||
} | ||
|
||
const selectors = pkg | ||
.getVersions() | ||
.map((versioned) => { | ||
if (!satisfiesVersion(versioned.getRange(), version)) { | ||
return []; | ||
} | ||
|
||
return versioned.getSelectors(); | ||
}) | ||
.flat(); | ||
|
||
if (selectors.length === 0) { | ||
return; | ||
} | ||
|
||
wrapped[pkg.getName()] = { | ||
version, | ||
supported: true, | ||
}; | ||
|
||
new Hook([pkg.getName()], (exports) => { | ||
selectors.forEach((selector) => { | ||
const subject = selector.getSelector()(exports); | ||
|
||
if (!subject) { | ||
return; | ||
} | ||
|
||
selector.getMethods().forEach((method) => wrapMethod(subject, method)); | ||
}); | ||
|
||
return exports; | ||
}); | ||
}); | ||
|
||
return wrapped; | ||
} |
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