pax_global_header00006660000000000000000000000064134620527610014520gustar00rootroot0000000000000052 comment=1923bf8f724f7f2e77a2c893116285a8b0a47598 parent-module-2.0.0/000077500000000000000000000000001346205276100142735ustar00rootroot00000000000000parent-module-2.0.0/.editorconfig000066400000000000000000000002571346205276100167540ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 parent-module-2.0.0/.gitattributes000066400000000000000000000000231346205276100171610ustar00rootroot00000000000000* text=auto eol=lf parent-module-2.0.0/.gitignore000066400000000000000000000000271346205276100162620ustar00rootroot00000000000000node_modules yarn.lock parent-module-2.0.0/.npmrc000066400000000000000000000000231346205276100154060ustar00rootroot00000000000000package-lock=false parent-module-2.0.0/.travis.yml000066400000000000000000000000651346205276100164050ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' parent-module-2.0.0/fixtures/000077500000000000000000000000001346205276100161445ustar00rootroot00000000000000parent-module-2.0.0/fixtures/filepath/000077500000000000000000000000001346205276100177405ustar00rootroot00000000000000parent-module-2.0.0/fixtures/filepath/1.js000066400000000000000000000000401346205276100204300ustar00rootroot00000000000000'use strict'; require('./2')(); parent-module-2.0.0/fixtures/filepath/2.js000066400000000000000000000001071346205276100204350ustar00rootroot00000000000000'use strict'; module.exports = () => { require('./3')(__filename); }; parent-module-2.0.0/fixtures/filepath/3.js000066400000000000000000000001761346205276100204440ustar00rootroot00000000000000'use strict'; const parentModule = require('../..'); module.exports = filepath => { console.log(parentModule(filepath)); }; parent-module-2.0.0/fixtures/main/000077500000000000000000000000001346205276100170705ustar00rootroot00000000000000parent-module-2.0.0/fixtures/main/1.js000066400000000000000000000000401346205276100175600ustar00rootroot00000000000000'use strict'; require('./2')(); parent-module-2.0.0/fixtures/main/2.js000066400000000000000000000001601346205276100175640ustar00rootroot00000000000000'use strict'; const parentModule = require('../..'); module.exports = () => { console.log(parentModule()); }; parent-module-2.0.0/index.d.ts000066400000000000000000000012161346205276100161740ustar00rootroot00000000000000/** Get the path of the parent module. @param filePath - File path of the module of which to get the parent path. Useful if you want it to work [multiple module levels down](https://github.com/sindresorhus/parent-module/tree/master/fixtures/filepath). Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename) @example ``` // bar.ts const parentModule = require('parent-module'); export default () => { console.log(parentModule()); //=> '/Users/sindresorhus/dev/unicorn/foo.ts' }; // foo.ts import bar from './bar'; bar(); ``` */ declare function parentModule(filePath?: string): string | undefined; export = parentModule; parent-module-2.0.0/index.js000066400000000000000000000012201346205276100157330ustar00rootroot00000000000000'use strict'; const callsites = require('callsites'); module.exports = filePath => { const stacks = callsites(); if (!filePath) { return stacks[2].getFileName(); } let hasSeenValue = false; // Skip the first stack as it's this function stacks.shift(); for (const stack of stacks) { const parentFilePath = stack.getFileName(); if (typeof parentFilePath !== 'string') { continue; } if (parentFilePath === filePath) { hasSeenValue = true; continue; } // Skip native modules if (parentFilePath === 'module.js') { continue; } if (hasSeenValue && parentFilePath !== filePath) { return parentFilePath; } } }; parent-module-2.0.0/index.test-d.ts000066400000000000000000000002521346205276100171500ustar00rootroot00000000000000import {expectType} from 'tsd'; import parentModule = require('.'); expectType(parentModule()); expectType(parentModule('foo')); parent-module-2.0.0/license000066400000000000000000000021251346205276100156400ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. parent-module-2.0.0/package.json000066400000000000000000000013511346205276100165610ustar00rootroot00000000000000{ "name": "parent-module", "version": "2.0.0", "description": "Get the path of the parent module", "license": "MIT", "repository": "sindresorhus/parent-module", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "parent", "module", "package", "caller", "calling", "module", "path", "callsites", "callsite", "stacktrace", "stack", "trace", "function", "file" ], "dependencies": { "callsites": "^3.1.0" }, "devDependencies": { "ava": "^1.4.1", "execa": "^1.0.0", "tsd": "^0.7.2", "xo": "^0.24.0" } } parent-module-2.0.0/readme.md000066400000000000000000000026651346205276100160630ustar00rootroot00000000000000# parent-module [![Build Status](https://travis-ci.org/sindresorhus/parent-module.svg?branch=master)](https://travis-ci.org/sindresorhus/parent-module) > Get the path of the parent module Node.js exposes `module.parent`, but it only gives you the first cached parent, which is not necessarily the actual parent. ## Install ``` $ npm install parent-module ``` ## Usage ```js // bar.js const parentModule = require('parent-module'); module.exports = () => { console.log(parentModule()); //=> '/Users/sindresorhus/dev/unicorn/foo.js' }; ``` ```js // foo.js const bar = require('./bar'); bar(); ``` ## API ### parentModule([filePath]) By default, it will return the path of the immediate parent. #### filePath Type: `string`
Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename) File path of the module of which to get the parent path. Useful if you want it to work [multiple module levels down](https://github.com/sindresorhus/parent-module/tree/master/fixtures/filepath). ## Tip Combine it with [`read-pkg-up`](https://github.com/sindresorhus/read-pkg-up) to read the package.json of the parent module. ```js const path = require('path'); const readPkgUp = require('read-pkg-up'); const parentModule = require('parent-module'); console.log(readPkgUp.sync({cwd: path.dirname(parentModule())}).pkg); //=> {name: 'chalk', version: '1.0.0', …} ``` ## License MIT © [Sindre Sorhus](https://sindresorhus.com) parent-module-2.0.0/test.js000066400000000000000000000005471346205276100156160ustar00rootroot00000000000000import path from 'path'; import test from 'ava'; import execa from 'execa'; test('main', async t => { const {stdout} = await execa('node', ['./fixtures/main/1.js']); t.is(path.basename(stdout), '1.js'); }); test('filepath option', async t => { const {stdout} = await execa('node', ['./fixtures/filepath/1.js']); t.is(path.basename(stdout), '1.js'); });