├── .gitignore ├── .npmrc ├── src ├── index.js └── am-i-a-dependency-spec.js ├── .travis.yml ├── next-update-travis.sh ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=http://registry.npmjs.org/ 2 | save-exact=true 3 | progress=false 4 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const path = require('path') 4 | 5 | function amIaDependency () { 6 | const cwd = process.cwd() 7 | const parts = cwd.split(path.sep) 8 | const parentFolder = parts[parts.length - 2] 9 | const scopedParentFodler = parts[parts.length - 3] 10 | return parentFolder === 'node_modules' || scopedParentFodler === 'node_modules' 11 | } 12 | 13 | module.exports = amIaDependency 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: 3 | directories: 4 | - node_modules 5 | notifications: 6 | email: true 7 | node_js: 8 | - '4' 9 | - '6' 10 | before_install: 11 | - npm install -g npm@3 12 | before_script: 13 | - npm prune 14 | script: 15 | - ./next-update-travis.sh 16 | - npm test 17 | after_success: 18 | - TRAVIS_JOB_NUMBER=WORKAROUND.1 $(npm bin)/if-node-version ">=6" npm run semantic-release 19 | branches: 20 | except: 21 | - /^v\d+\.\d+\.\d+$/ 22 | -------------------------------------------------------------------------------- /src/am-i-a-dependency-spec.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const la = require('lazy-ass') 4 | const is = require('check-more-types') 5 | const chdir = require('chdir-promise') 6 | const fromFolder = require('path').join.bind(null, __dirname) 7 | 8 | /* global describe, it, beforeEach, afterEach */ 9 | const amIADependency = require('.') 10 | 11 | describe('am-i-a-dependency', () => { 12 | it('is a function', () => { 13 | la(is.fn(amIADependency)) 14 | }) 15 | 16 | it('determines not a dependency', () => { 17 | la(!amIADependency()) 18 | }) 19 | 20 | describe('from node_modules', () => { 21 | beforeEach(() => { 22 | const mochaFolder = fromFolder('..', 'node_modules', 'mocha') 23 | return chdir.to(mochaFolder) 24 | }) 25 | 26 | afterEach(chdir.back) 27 | 28 | it('determines when is a dependency', () => { 29 | la(amIADependency(), 'should be a dependency when in folder', 30 | process.cwd()) 31 | }) 32 | }) 33 | 34 | describe('from @scoped node_modules', () => { 35 | beforeEach(() => { 36 | const semanticReleaseFolder = fromFolder('..', 'node_modules', '@semantic-release', 'error') 37 | return chdir.to(semanticReleaseFolder) 38 | }) 39 | 40 | afterEach(chdir.back) 41 | 42 | it('determines when is a dependency', () => { 43 | la(amIADependency(), 'should be a dependency when in folder', 44 | process.cwd()) 45 | }) 46 | }) 47 | }) 48 | -------------------------------------------------------------------------------- /next-update-travis.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | if [ "$TRAVIS_EVENT_TYPE" = "cron" ]; then 6 | if [ "$GH_TOKEN" = "" ]; then 7 | echo "" 8 | echo "⛔️ Cannot find environment variable GH_TOKEN ⛔️" 9 | echo "Please set it up for this script to be able" 10 | echo "to push results to GitHub" 11 | echo "ℹ️ The best way is to use semantic-release to set it up" 12 | echo "" 13 | echo " https://github.com/semantic-release/semantic-release" 14 | echo "" 15 | echo "npm i -g semantic-release-cli" 16 | echo "semantic-release-cli setup" 17 | echo "" 18 | exit 1 19 | fi 20 | 21 | echo "Upgrading dependencies using next-update" 22 | npm i -g next-update 23 | 24 | # you can edit options to allow only some updates 25 | # --allow major | minor | patch 26 | # --latest true | false 27 | # see all options by installing next-update 28 | # and running next-update -h 29 | next-update --allow minor --latest false 30 | 31 | git status 32 | # if package.json is modified we have 33 | # new upgrades 34 | if git diff --name-only | grep package.json > /dev/null; then 35 | echo "There are new versions of dependencies 💪" 36 | git add package.json 37 | echo "----------- package.json diff -------------" 38 | git diff --staged 39 | echo "-------------------------------------------" 40 | git config --global user.email "next-update@ci.com" 41 | git config --global user.name "next-update" 42 | git commit -m "chore(deps): upgrade dependencies using next-update" 43 | # push back to GitHub using token 44 | git remote remove origin 45 | # TODO read origin from package.json 46 | # or use github api module github 47 | # like in https://github.com/semantic-release/semantic-release/blob/caribou/src/post.js 48 | git remote add origin https://next-update:$GH_TOKEN@github.com/bahmutov/am-i-a-dependency.git 49 | git push origin HEAD:master 50 | else 51 | echo "No new versions found ✋" 52 | fi 53 | else 54 | echo "Not a cron job, normal test" 55 | fi 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "am-i-a-dependency", 3 | "description": "Check if the package is being installed by another package", 4 | "version": "0.0.0-development", 5 | "author": "Gleb Bahmutov ", 6 | "bugs": "https://github.com/bahmutov/am-i-a-dependency/issues", 7 | "config": { 8 | "pre-git": { 9 | "commit-msg": "simple", 10 | "pre-commit": [ 11 | "npm run deps", 12 | "npm test", 13 | "git add src/*.js", 14 | "npm run ban" 15 | ], 16 | "pre-push": [ 17 | "npm run secure", 18 | "npm run license", 19 | "npm run ban -- --all", 20 | "npm run size" 21 | ], 22 | "post-commit": [], 23 | "post-merge": [] 24 | } 25 | }, 26 | "engines": { 27 | "node": ">=4" 28 | }, 29 | "files": [ 30 | "src/*.js", 31 | "!src/*-spec.js" 32 | ], 33 | "homepage": "https://github.com/bahmutov/am-i-a-dependency#readme", 34 | "keywords": [ 35 | "check", 36 | "npm", 37 | "postinstall", 38 | "utility" 39 | ], 40 | "license": "MIT", 41 | "main": "src/", 42 | "publishConfig": { 43 | "registry": "http://registry.npmjs.org/" 44 | }, 45 | "repository": { 46 | "type": "git", 47 | "url": "https://github.com/bahmutov/am-i-a-dependency.git" 48 | }, 49 | "scripts": { 50 | "ban": "ban", 51 | "deps": "deps-ok && dependency-check --no-dev .", 52 | "issues": "git-issues", 53 | "license": "license-checker --production --onlyunknown --csv", 54 | "lint": "standard --verbose --fix src/*.js", 55 | "pretest": "npm run lint", 56 | "secure": "nsp check", 57 | "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", 58 | "test": "npm run unit", 59 | "unit": "mocha src/*-spec.js", 60 | "semantic-release": "semantic-release pre && npm publish && semantic-release post" 61 | }, 62 | "devDependencies": { 63 | "ban-sensitive-files": "1.9.2", 64 | "chdir-promise": "0.6.2", 65 | "check-more-types": "2.24.0", 66 | "dependency-check": "2.10.1", 67 | "deps-ok": "1.4.1", 68 | "git-issues": "1.3.1", 69 | "github-post-release": "1.13.1", 70 | "if-node-version": "1.1.1", 71 | "lazy-ass": "1.6.0", 72 | "license-checker": "13.1.0", 73 | "mocha": "3.5.3", 74 | "next-update-travis": "1.7.1", 75 | "nsp": "2.8.1", 76 | "pre-git": "3.17.1", 77 | "semantic-release": "^6.3.6", 78 | "standard": "10.0.3" 79 | }, 80 | "release": { 81 | "generateNotes": "github-post-release", 82 | "analyzeCommits": "simple-commit-message" 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # am-i-a-dependency 2 | 3 | > Check if the package is being installed by another package 4 | 5 | [![NPM][npm-icon] ][npm-url] 6 | 7 | [![Build status][ci-image] ][ci-url] 8 | [![semantic-release][semantic-image] ][semantic-url] 9 | [![js-standard-style][standard-image]][standard-url] 10 | [![next-update-travis badge][nut-badge]][nut-readme] 11 | 12 | ## Problem 13 | 14 | Often you want to run a post install script that does something. Yet, this 15 | `postinstall` script will run *even in the module itself*. Thus we need a 16 | simple way to determine if we are installing our dependencies or installing 17 | the current module as a dependency of some other module. This can be 18 | explained by a difference in commands below 19 | 20 | ``` 21 | npm i -> we are installing our dependencies 22 | npm i foo -> module "foo" is being installed as a dependency 23 | ``` 24 | 25 | If module `foo` has a `postinstall` script, it can use this module 26 | `am-i-a-dependency` to determine if it should be executed. 27 | 28 | ## Install 29 | 30 | Requires [Node](https://nodejs.org/en/) version 4 or above. 31 | 32 | ```sh 33 | npm install --save am-i-a-dependency 34 | ``` 35 | ## Use 36 | 37 | Imagine a `postinstall` script 38 | 39 | ```json 40 | { 41 | "scripts": { 42 | "postinstall": "node something.js" 43 | } 44 | } 45 | ``` 46 | 47 | Then inside `something.js` we can check 48 | 49 | ```js 50 | // something.js 51 | const amDependency = require('am-i-a-dependency')() 52 | if (amDependency) { 53 | // yes, do something interesting 54 | // someone is executing "npm install foo" 55 | } else { 56 | // we are inside "foo" itself, so nothing to do 57 | } 58 | ``` 59 | 60 | ### Small print 61 | 62 | Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017 63 | 64 | * [@bahmutov](https://twitter.com/bahmutov) 65 | * [glebbahmutov.com](https://glebbahmutov.com) 66 | * [blog](https://glebbahmutov.com/blog) 67 | 68 | License: MIT - do anything with the code, but don't blame me if it does not work. 69 | 70 | Support: if you find any problems with this module, email / tweet / 71 | [open issue](https://github.com/bahmutov/am-i-a-dependency/issues) on Github 72 | 73 | ## MIT License 74 | 75 | Copyright (c) 2017 Gleb Bahmutov <gleb.bahmutov@gmail.com> 76 | 77 | Permission is hereby granted, free of charge, to any person 78 | obtaining a copy of this software and associated documentation 79 | files (the "Software"), to deal in the Software without 80 | restriction, including without limitation the rights to use, 81 | copy, modify, merge, publish, distribute, sublicense, and/or sell 82 | copies of the Software, and to permit persons to whom the 83 | Software is furnished to do so, subject to the following 84 | conditions: 85 | 86 | The above copyright notice and this permission notice shall be 87 | included in all copies or substantial portions of the Software. 88 | 89 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 90 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 91 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 92 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 93 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 94 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 95 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 96 | OTHER DEALINGS IN THE SOFTWARE. 97 | 98 | [npm-icon]: https://nodei.co/npm/am-i-a-dependency.svg?downloads=true 99 | [npm-url]: https://npmjs.org/package/am-i-a-dependency 100 | [ci-image]: https://travis-ci.org/bahmutov/am-i-a-dependency.svg?branch=master 101 | [ci-url]: https://travis-ci.org/bahmutov/am-i-a-dependency 102 | [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg 103 | [semantic-url]: https://github.com/semantic-release/semantic-release 104 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg 105 | [standard-url]: http://standardjs.com/ 106 | [nut-badge]: https://img.shields.io/badge/next--update--travis-monthly-green.svg 107 | [nut-readme]: https://github.com/bahmutov/next-update-travis#readme 108 | --------------------------------------------------------------------------------