@graphprotocol/toolshed
Version:
A collection of tools and utilities for the Graph Protocol Typescript components
51 lines • 2.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveAddressBook = resolveAddressBook;
const path_1 = __importDefault(require("path"));
/**
* Resolves the absolute path to an address book file relative to an existing file.
*
* This function uses `require.resolve` (from the caller's context) on the provided
* `existingAddressBookPath` to locate a known file (e.g., an existing address book JSON file).
* Once located, it returns the absolute path to the desired `addressBookPath`, which is resolved
* relative to the directory of the existing file.
*
* If the existing file cannot be resolved, the function returns `undefined`.
*
* This is useful when:
* - You know the location of one file in a package or module.
* - You need the path to another file in the same directory (or nearby), whether or not it exists.
*
* ## Examples
*
* ```ts
* // Example 1: Resolve a different file in the same folder
* // Locates: <node_modules>/@graphprotocol/horizon/addresses.json
* // Returns: <node_modules>/@graphprotocol/horizon/addresses-hardhat.json
* resolveAddressBook(require, 'addresses.json', 'addresses-hardhat.json')
* ```
*
* ```ts
* // Example 2: Resolve the same file you use for lookup
* // Locates and returns: <node_modules>/@graphprotocol/address-book/horizon/addresses.json
* resolveAddressBook(require, '@graphprotocol/address-book/horizon/addresses.json')
* ```
*
* @param callerRequire - The `require` function from the calling module, used for resolution relative to the caller.
* @param existingAddressBookPath - A resolvable path to an existing file (relative to the caller), used as an anchor. Defaults to `"addresses.json"`.
* @param addressBookPath - The path (relative to the anchor's directory) to the file you want returned. Defaults to `"addresses.json"`.
* @returns The absolute path to the requested file, or `undefined` if the existing file cannot be resolved.
*/
function resolveAddressBook(callerRequire, existingAddressBookPath = 'addresses.json', addressBookPath = 'addresses.json') {
try {
const packageRoot = path_1.default.dirname(callerRequire.resolve(`${existingAddressBookPath}`));
return path_1.default.join(packageRoot, addressBookPath);
}
catch (_) {
return undefined;
}
}
//# sourceMappingURL=resolve.js.map