pax_global_header00006660000000000000000000000064146474352170014527gustar00rootroot0000000000000052 comment=ff3fb1541a40b629a22d2d21a4e1cf4605c6bb5a sindresorhus-import-local-be710f7/000077500000000000000000000000001464743521700172745ustar00rootroot00000000000000sindresorhus-import-local-be710f7/.editorconfig000066400000000000000000000002571464743521700217550ustar00rootroot00000000000000root = 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 sindresorhus-import-local-be710f7/.gitattributes000066400000000000000000000000231464743521700221620ustar00rootroot00000000000000* text=auto eol=lf sindresorhus-import-local-be710f7/.github/000077500000000000000000000000001464743521700206345ustar00rootroot00000000000000sindresorhus-import-local-be710f7/.github/security.md000066400000000000000000000002631464743521700230260ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. sindresorhus-import-local-be710f7/.github/workflows/000077500000000000000000000000001464743521700226715ustar00rootroot00000000000000sindresorhus-import-local-be710f7/.github/workflows/main.yml000066400000000000000000000007021464743521700243370ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 - 8 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test sindresorhus-import-local-be710f7/.gitignore000066400000000000000000000000271464743521700212630ustar00rootroot00000000000000node_modules yarn.lock sindresorhus-import-local-be710f7/.npmrc000066400000000000000000000000231464743521700204070ustar00rootroot00000000000000package-lock=false sindresorhus-import-local-be710f7/fixtures/000077500000000000000000000000001464743521700211455ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/5-nested/000077500000000000000000000000001464743521700225715ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/5-nested/foo/000077500000000000000000000000001464743521700233545ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/5-nested/foo/index.js000066400000000000000000000000221464743521700250130ustar00rootroot00000000000000require('sample');sindresorhus-import-local-be710f7/fixtures/5-nested/node_modules/000077500000000000000000000000001464743521700252465ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/5-nested/node_modules/sample/000077500000000000000000000000001464743521700265275ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/5-nested/node_modules/sample/index.js000066400000000000000000000002501464743521700301710ustar00rootroot00000000000000const importLocal = require('import-local'); if (importLocal(__filename)) { console.log('Using local version of this package'); } else { console.log('Called'); } sindresorhus-import-local-be710f7/fixtures/5-nested/node_modules/sample/package.json000066400000000000000000000000301464743521700310060ustar00rootroot00000000000000{ "name": "sample" }sindresorhus-import-local-be710f7/fixtures/cli.js000077500000000000000000000001761464743521700222610ustar00rootroot00000000000000#!/usr/bin/env node 'use strict'; const importLocal = require('..'); if (importLocal(__filename)) { console.log('local'); } sindresorhus-import-local-be710f7/fixtures/global/000077500000000000000000000000001464743521700224055ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/global/package.json000066400000000000000000000000361464743521700246720ustar00rootroot00000000000000{ "name": "fixture-global" } sindresorhus-import-local-be710f7/fixtures/local/000077500000000000000000000000001464743521700222375ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/local/package.json000066400000000000000000000000351464743521700245230ustar00rootroot00000000000000{ "name": "fixture-local" } sindresorhus-import-local-be710f7/fixtures/prefer-local/000077500000000000000000000000001464743521700235205ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/prefer-local/cli.js000077500000000000000000000002011464743521700246210ustar00rootroot00000000000000#!/usr/bin/env node 'use strict'; const importLocal = require('../..'); if (importLocal(__filename)) { console.log('local'); } sindresorhus-import-local-be710f7/fixtures/prefer-local/nested/000077500000000000000000000000001464743521700250025ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/prefer-local/nested/node_modules/000077500000000000000000000000001464743521700274575ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/prefer-local/nested/node_modules/some-pkg/000077500000000000000000000000001464743521700312015ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/prefer-local/nested/node_modules/some-pkg/cli.js000066400000000000000000000000001464743521700322740ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/prefer-local/nested/node_modules/some-pkg/package.json000066400000000000000000000000301464743521700334600ustar00rootroot00000000000000{ "name": "some-pkg" } sindresorhus-import-local-be710f7/fixtures/prefer-local/package.json000066400000000000000000000000301464743521700257770ustar00rootroot00000000000000{ "name": "some-pkg" } sindresorhus-import-local-be710f7/fixtures/yarn-alias/000077500000000000000000000000001464743521700232055ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/yarn-alias/index.js000066400000000000000000000000651464743521700246530ustar00rootroot00000000000000require('some-pkg'); require('some-pkg-with-alias'); sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/000077500000000000000000000000001464743521700256625ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg-with-alias/000077500000000000000000000000001464743521700314445ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg-with-alias/index.js000077500000000000000000000001461464743521700331150ustar00rootroot00000000000000const importLocal = require('import-local'); if (importLocal(__filename)) { console.log('local'); } sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg-with-alias/package.json000066400000000000000000000001131464743521700337250ustar00rootroot00000000000000{ "name": "some-pkg", "version": "1.0.0", "main": "index.js" } sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg/000077500000000000000000000000001464743521700274045ustar00rootroot00000000000000sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg/index.js000077500000000000000000000001461464743521700310550ustar00rootroot00000000000000const importLocal = require('import-local'); if (importLocal(__filename)) { console.log('local'); } sindresorhus-import-local-be710f7/fixtures/yarn-alias/node_modules/some-pkg/package.json000066400000000000000000000001131464743521700316650ustar00rootroot00000000000000{ "name": "some-pkg", "version": "1.0.0", "main": "index.js" } sindresorhus-import-local-be710f7/index.d.ts000066400000000000000000000007231464743521700211770ustar00rootroot00000000000000/** Let a globally installed package use a locally installed version of itself if available. @param filePath - The absolute file path to the main file of the package. @example ``` import importLocal from 'import-local'; if (importLocal(import.meta.url)) { console.log('Using local version of this package'); } else { // Code for both global and local version here… } ``` */ export default function importLocal(filePath: string): boolean | undefined | unknown; sindresorhus-import-local-be710f7/index.js000066400000000000000000000025501464743521700207430ustar00rootroot00000000000000'use strict'; const path = require('path'); const {fileURLToPath} = require('url'); const resolveCwd = require('resolve-cwd'); const pkgDir = require('pkg-dir'); module.exports = filename => { const normalizedFilename = filename.startsWith('file://') ? fileURLToPath(filename) : filename; const globalDir = pkgDir.sync(path.dirname(normalizedFilename)); const relativePath = path.relative(globalDir, normalizedFilename); const pkg = require(path.join(globalDir, 'package.json')); const localFile = resolveCwd.silent(path.join(pkg.name, relativePath)); const localNodeModules = path.join(process.cwd(), 'node_modules'); const filenameInLocalNodeModules = !path.relative(localNodeModules, normalizedFilename).startsWith('..') && // On Windows, if `localNodeModules` and `normalizedFilename` are on different partitions, `path.relative()` returns the value of `normalizedFilename`, resulting in `filenameInLocalNodeModules` incorrectly becoming `true`. path.parse(localNodeModules).root === path.parse(normalizedFilename).root; // Use `path.relative()` to detect local package installation, // because __filename's case is inconsistent on Windows // Can use `===` when targeting Node.js 8 // See https://github.com/nodejs/node/issues/6624 return !filenameInLocalNodeModules && localFile && path.relative(localFile, normalizedFilename) !== '' && require(localFile); }; sindresorhus-import-local-be710f7/license000066400000000000000000000021351464743521700206420ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://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. sindresorhus-import-local-be710f7/package.json000066400000000000000000000017051464743521700215650ustar00rootroot00000000000000{ "name": "import-local", "version": "3.2.0", "description": "Let a globally installed package use a locally installed version of itself if available", "license": "MIT", "repository": "sindresorhus/import-local", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "bin": { "import-local-fixture": "fixtures/cli.js" }, "sideEffects": false, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js", "index.d.ts", "fixtures/cli.js" ], "keywords": [ "import", "local", "require", "resolve", "global", "version", "prefer", "cli" ], "dependencies": { "pkg-dir": "^4.2.0", "resolve-cwd": "^3.0.0" }, "devDependencies": { "ava": "2.1.0", "cpy": "^7.0.1", "del": "^4.1.1", "execa": "^2.0.1", "xo": "^0.24.0" }, "xo": { "ignores": [ "fixtures" ] } } sindresorhus-import-local-be710f7/readme.md000066400000000000000000000012371464743521700210560ustar00rootroot00000000000000# import-local > Let a globally installed package use a locally installed version of itself if available Useful for CLI tools that want to defer to the user's locally installed version when available, but still work if it's not installed locally. For example, [AVA](https://avajs.dev) and [XO](https://github.com/xojs/xo) uses this method. ## Install ```sh npm install import-local ``` ## Usage ```js import importLocal from 'import-local'; if (importLocal(import.meta.url)) { console.log('Using local version of this package'); } else { // Code for both global and local version here… } ``` You can also pass in `__filename` when used in a CommonJS context. sindresorhus-import-local-be710f7/test.js000066400000000000000000000041461464743521700206160ustar00rootroot00000000000000import path from 'path'; import test from 'ava'; import execa from 'execa'; import cpy from 'cpy'; import del from 'del'; test('local', async t => { await cpy( ['package.json', 'index.js', 'fixtures/cli.js'], path.join(__dirname, 'fixtures/local/node_modules/import-local'), {parents: true} ); const {stdout} = await execa('import-local-fixture', { preferLocal: false, cwd: path.join(__dirname, 'fixtures/local') }); t.is(stdout, 'local'); await del(path.join(__dirname, 'fixtures/local/node_modules')); }); test('invoking global prefers local', async t => { await cpy( ['package.json', 'index.js'], path.join(__dirname, 'fixtures/prefer-local/node_modules/import-local'), {parents: true} ); const {stdout} = await execa( 'node', [path.join(__dirname, 'fixtures/prefer-local/cli.js')], { preferLocal: false, cwd: path.join(__dirname, 'fixtures/prefer-local/nested') } ); t.is(stdout, 'local'); await del(path.join(__dirname, 'fixtures/prefer-local/node_modules')); }); test('global', async t => { const {stdout} = await execa('import-local-fixture', { preferLocal: false, cwd: path.join(__dirname, 'fixtures/global') }); t.is(stdout, ''); }); test('treats aliased package as local installation', async t => { await cpy( ['package.json', 'index.js'], path.join(__dirname, 'fixtures/yarn-alias/node_modules/import-local'), {parents: true} ); const {stdout} = await execa( 'node', [path.join(__dirname, 'fixtures/yarn-alias/index.js')], { preferLocal: false, cwd: path.join(__dirname, 'fixtures/yarn-alias') } ); t.is(stdout, ''); }); // https://github.com/sindresorhus/import-local/pull/5 test('import from node_modules in a parent directory', async t => { await cpy( ['package.json', 'index.js', 'fixtures/cli.js'], path.join(__dirname, 'fixtures/5-nested/node_modules/import-local'), {parents: true} ); const {stdout} = await execa( 'node', ['index.js'], { preferLocal: false, cwd: path.join(__dirname, 'fixtures/5-nested/foo') } ); t.is(stdout, 'Called'); await del(path.join(__dirname, 'fixtures/5-nested/node_modules/import-local')); });