pax_global_header00006660000000000000000000000064137604054110014513gustar00rootroot0000000000000052 comment=8460f9c82eebb167e224a7ee492bd234f917122d require-at-1.0.6/000077500000000000000000000000001376040541100135755ustar00rootroot00000000000000require-at-1.0.6/.gitignore000066400000000000000000000000461376040541100155650ustar00rootroot00000000000000/node_modules coverage .idea *-lock.* require-at-1.0.6/.istanbul.yml000066400000000000000000000004651376040541100162240ustar00rootroot00000000000000instrumentation: root: . extensions: - .js default-excludes: true include-all-sources: true excludes: - create-require.js check: global: statements: 100 lines: 100 branches: 100 functions: 100 each: statements: 100 lines: 100 branches: 100 functions: 100 require-at-1.0.6/.travis.yml000066400000000000000000000002661376040541100157120ustar00rootroot00000000000000language: node_js node_js: - v10 - v12 - v14 script: fun coverage notifications: email: - joel123@gmail.com install: fyn --pg none before_install: - npm install -g fyn require-at-1.0.6/README.md000066400000000000000000000055651376040541100150670ustar00rootroot00000000000000[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url] # require-at Allow you to call `require` or `require.resolve` pretending that you are at another directory. ## Purpose Given the directory structure below with two NodeJS apps: app1 |-+ foo | +-- index.js | +--+ node_modules | +--+ x | + ... app2 |-+ bar | +-- index.js | +--+ node_modules | +--+ y | + ... When you call `require("x")` in `/app1/foo/index.js`, NodeJS will search and find module `x` there. Now from the same file, if you want to resolve the module `y` under the directory `/app2/bar`, you have to use an absolute or relative path directly pointing to `y`, and you may have to do some searching, probably re-implementing Node's module searching algorithm if you don't know exactly where `y` could be. However, in the file `/app2/bar/index.js`, it can just do `require("y")` and Node would automatically find the module for it, because that file is at the location where `y` is under. What if from the file `/app1/foo/index.js`, you can call `require` as if you were at the directory `/app2/bar`, then you would be able to utilize Node's module searching automatically. To achieve this, most other implementations choose to re-implement Node's module searching algorithm. This module's approach is to tap into Node's `module` and let it do the work. ## Install $ npm install require-at --save ## Usage A single function is exported. ##### `requireAt(dir, [request])` - If you call it with just `dir`, then it returns a `require` function that's been binded to the directory `dir`. You can use it to load any module as if you are at `dir`. - You can also call `require.resolve` with the same effect. - If you call it with `dir` and a `request`, then it will load and return the module `request` as if at `dir`. ##### Example ```js const requireAt = require("require-at"); // get back a require binded to /another/dir const requireAtAnother = requireAt("/another/dir/"); const modXPath = requireAtAnother.resolve("modX"); const modX = requireAtAnother("modX"); // load modY at /another/yet/dir directly const modY = requireAt("/another/yet/dir", "modY"); ``` ## License Apache-2.0 © [Joel Chen](https://github.com/jchip) [travis-image]: https://travis-ci.org/jchip/require-at.svg?branch=master [travis-url]: https://travis-ci.org/jchip/require-at [npm-image]: https://badge.fury.io/js/require-at.svg [npm-url]: https://npmjs.org/package/require-at [daviddm-image]: https://david-dm.org/jchip/require-at/status.svg [daviddm-url]: https://david-dm.org/jchip/require-at [daviddm-dev-image]: https://david-dm.org/jchip/require-at/dev-status.svg [daviddm-dev-url]: https://david-dm.org/jchip/require-at?type=dev require-at-1.0.6/create-require.js000066400000000000000000000024431376040541100170530ustar00rootroot00000000000000"use strict"; const Module = require("module"); // use eval to avoid tripping bundlers const xrequire = eval("require"); const createRequireFromPath = Module.createRequire || Module.createRequireFromPath || ((filename, dir) => { // https://github.com/nodejs/node/blob/1ae0511b942c01c6e0adff98643d350a395bf101/lib/internal/modules/cjs/loader.js#L748 // https://github.com/nodejs/node/blob/1ae0511b942c01c6e0adff98643d350a395bf101/lib/internal/modules/cjs/helpers.js#L16 const m = new Module(filename); m.filename = filename; m.paths = Module._nodeModulePaths(dir); // don't name this require to avoid tripping bundlers function _require(request) { // can't use m.require because there's an internal requireDepth thing // in the native Module implementation return xrequire(resolve(request)); } function resolve(request, options) { return Module._resolveFilename(request, m, false, options); } _require.resolve = resolve; function paths(request) { return Module._resolveLookupPaths(request, m, true); } resolve.paths = paths; _require.main = process.mainModule; _require.extensions = Module._extensions; _require.cache = Module._cache; return _require; }); module.exports = createRequireFromPath; require-at-1.0.6/package.json000066400000000000000000000012271376040541100160650ustar00rootroot00000000000000{ "name": "require-at", "version": "1.0.6", "main": "require-at.js", "description": "Call require pretending your are at another directory", "scripts": { "test": "mocha test/spec", "coverage": "istanbul cover -- _mocha test/spec && istanbul check-coverage" }, "keywords": [], "author": "Joel Chen", "license": "Apache-2.0", "repository": { "type": "git", "url": "https://github.com/jchip/require-at" }, "files": [ "create-require.js", "require-at.js" ], "dependencies": {}, "devDependencies": { "chai": "^3.5.0", "istanbul": "^0.4.5", "mocha": "^3.2.0" }, "engines": { "node": ">=4" } } require-at-1.0.6/require-at.js000066400000000000000000000017131376040541100162130ustar00rootroot00000000000000"use strict"; const Path = require("path"); const Fs = require("fs"); const createRequireFromPath = require("./create-require"); const cache = new Map(); function requireAt(dir, request) { const makeIt = (xdir, checked) => { let xRequire = requireAt.cache && requireAt.cache.get(xdir); if (!xRequire) { let stat; try { stat = Fs.statSync(xdir); } catch (e) { throw new Error(`require-at: stat '${xdir}' failed: ${e.message}`); } if (!stat || !stat.isDirectory()) { if (checked) throw new Error(`require-at: not a directory: '${dir}'`); return makeIt(Path.dirname(xdir), true); } xRequire = createRequireFromPath(Path.join(xdir, "._require-at_"), xdir); requireAt.cache && requireAt.cache.set(xdir, xRequire); } return request ? xRequire(request) : xRequire; }; return makeIt(Path.resolve(dir), false); } requireAt.cache = cache; module.exports = requireAt; require-at-1.0.6/test/000077500000000000000000000000001376040541100145545ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/000077500000000000000000000000001376040541100164255ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/app/000077500000000000000000000000001376040541100172055ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/app/bar.js000066400000000000000000000000001376040541100202750ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/app/node_modules/000077500000000000000000000000001376040541100216625ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/app/node_modules/foo/000077500000000000000000000000001376040541100224455ustar00rootroot00000000000000require-at-1.0.6/test/fixtures/app/node_modules/foo/index.js000066400000000000000000000000301376040541100241030ustar00rootroot00000000000000module.exports = "foo"; require-at-1.0.6/test/spec/000077500000000000000000000000001376040541100155065ustar00rootroot00000000000000require-at-1.0.6/test/spec/require-at.spec.js000066400000000000000000000027711376040541100210620ustar00rootroot00000000000000const Path = require("path"); const requireAt = require("../.."); const chai = require("chai"); const Fs = require("fs"); describe("require-at", function() { it("should be able to require at another dir as relative dir", () => { const appRequire = requireAt("test/fixtures/app"); const foo = appRequire("foo"); chai.expect(appRequire.resolve("foo")).contains("test/fixtures/app/node_modules/foo/index.js"); chai.expect(foo).to.equal("foo"); chai.expect(appRequire.resolve.paths("foo")).to.be.an.array; }); it("should be able to require at another dir as absolute dir", () => { const appRequire = requireAt(Path.resolve("test/fixtures/app")); const foo = appRequire("foo"); chai.expect(foo).to.equal("foo"); }); it("should throw if directory doesn't exist", () => { chai.expect(() => requireAt("foo/fixtures/app")).to.throw(); }); it("should throw if 2nd dir check is not a directory", () => { const ss = Fs.statSync; Fs.statSync = () => { return { isDirectory() { return false; } }; }; chai.expect(() => requireAt("foo/fixtures/app")).to.throw("not a directory"); Fs.statSync = ss; }); it("should require a file directly if passed", () => { const foo = requireAt("test/fixtures/app", "foo"); chai.expect(foo).to.equal("foo"); }); it("should auto dirname on a file that's passed", () => { const foo = requireAt("test/fixtures/app/bar.js", "foo"); chai.expect(foo).to.equal("foo"); }); });