GitXplorerGitXplorer
d

caller-dirname

public
0 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
1aab3610dbfd36a24c0d69a39a9c96d7a7d31893

Adjust rollup config to prefer builtins

ddetrohutt committed 7 years ago
Verified
23eff63c02bdff083b143411e2afb64f7075e1cd

Prefer .mjs files when building esm bundle

ddetrohutt committed 7 years ago
Verified
65a48dfdf2d95b917c6d8925e7f834a9cf1a57e6

Change order of CHANGELOG to newest version at top

ddetrohutt committed 7 years ago
Verified
4fb0557111ad6ede44667b4fff52c69e20b039ea

v2.0.0

ddetrohutt committed 7 years ago
Verified
14a84446088263199c5224fc288ac6aeb9dbbd67

Change default export to named export 'callerDirname'

ddetrohutt committed 7 years ago
Verified
30d3cef8f4da5256600e3a6814c869cdd4f05c71

v1.0.1

ddetrohutt committed 7 years ago

README

The README file for this repository.

caller-dirname

Get the directory name of a caller function's containing file.


Installation

$ npm i caller-dirname

Usage

/home/detrohutt/test/caller-dirname/callee.js

import { callerDirname } from 'caller-dirname';

export default () => callerDirname();

/home/detrohutt/test/caller-dirname/deeper/caller.js

import thisDirname from '../callee.js';

console.log(thisDirname()); /* /home/detrohutt/test/caller-dirname/deeper */

Options

Option Default Description
depth 1 Depth sets the position in the call stack at which you want to retrieve the directory name. With the default depth of 1 (1st caller) you get the dirname of the function that directly calls callerDirname() (we'll call it fn1). With a depth of 2 (2nd caller), you'd instead get the dirname of the function that called fn1, etc.
Example
import { callerDirname } from 'caller-dirname';

export const thirdCallerDir = () => callerDirname({ depth: 3 });

Motivation

I needed this for another repo I'm working on. Most of the code is taken from caller-path. The main difference is this returns the dirname only, rather than the full path with filename and extension. Also, it's published both as a CommonJS module and as an ES Module and includes TypeScript typings.