-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ability to load local files through cli option "require"
- Loading branch information
Showing
8 changed files
with
134 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import fs from "fs"; | ||
|
||
export const exists = async (path: string): Promise<boolean> => { | ||
try { | ||
await fs.promises.access(path); | ||
return true; | ||
} catch (_) { | ||
return false; | ||
} | ||
}; |
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,8 @@ | ||
import path from "path"; | ||
import { exists } from "./fs"; | ||
|
||
export const requireModule = async <T = unknown>(modulePath: string): Promise<T> => { | ||
const isModuleLocal = await exists(modulePath); | ||
|
||
return require(isModuleLocal ? path.resolve(modulePath) : modulePath); | ||
}; |
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,33 @@ | ||
import fs from "fs"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { exists } from "../../../src/utils/fs"; | ||
|
||
describe("utils/fs", () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.stub(fs.promises, "access"); | ||
}); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
describe("exists", () => { | ||
it("should return 'true' if file exists", async () => { | ||
const path = "./some-path.js"; | ||
(fs.promises.access as SinonStub).withArgs(path).resolves(); | ||
|
||
const isExists = await exists(path); | ||
|
||
assert.isTrue(isExists); | ||
}); | ||
|
||
it("should return 'false' if file doesn't exists", async () => { | ||
const path = "./some-path.js"; | ||
(fs.promises.access as SinonStub).withArgs(path).rejects(new Error("o.O")); | ||
|
||
const isExists = await exists(path); | ||
|
||
assert.isFalse(isExists); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import path from "path"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import proxyquire from "proxyquire"; | ||
import { requireModule as realRequireModule } from "../../../src/utils/module"; | ||
|
||
describe("utils/module", () => { | ||
let isNodeModuleRequired: SinonStub; | ||
let isLocalModuleRequired: SinonStub; | ||
let exists: SinonStub; | ||
let requireModule: typeof realRequireModule; | ||
|
||
const sandbox = sinon.createSandbox(); | ||
const nodeModulePath = "foo-module"; | ||
const relativeLocalModulePath = "./bar-module"; | ||
const absoluteLocalModulePath = path.resolve(relativeLocalModulePath); | ||
|
||
beforeEach(() => { | ||
isNodeModuleRequired = sinon.stub(); | ||
isLocalModuleRequired = sinon.stub(); | ||
exists = sinon.stub(); | ||
|
||
({ requireModule } = proxyquire.noCallThru().load("../../../src/utils/module", { | ||
"./fs": { exists }, | ||
[nodeModulePath]: isNodeModuleRequired, | ||
[absoluteLocalModulePath]: isLocalModuleRequired, | ||
})); | ||
}); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
describe("requireModule", () => { | ||
it("should require module from node-modules", async () => { | ||
exists.withArgs(nodeModulePath).resolves(false); | ||
const module = await requireModule<SinonStub>(nodeModulePath); | ||
|
||
module(); | ||
|
||
assert.calledOnce(isNodeModuleRequired); | ||
assert.notCalled(isLocalModuleRequired); | ||
}); | ||
|
||
it("should require module from node-modules", async () => { | ||
exists.withArgs(relativeLocalModulePath).resolves(true); | ||
const module = await requireModule<SinonStub>(relativeLocalModulePath); | ||
|
||
module(); | ||
|
||
assert.calledOnce(isLocalModuleRequired); | ||
assert.notCalled(isNodeModuleRequired); | ||
}); | ||
}); | ||
}); |
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