diff --git a/.editorconfig b/.editorconfig index c1b7c13..0176d42 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,5 +18,11 @@ indent_size = false [test/fast/Listing versions/Running "nvm ls --no-alias" does not call into nvm_alias] indent_size = false +[test/fast/Unit tests/mocks/**] +insert_final_newline = off + +[test/**/.urchin*] +insert_final_newline = off + [Makefile] indent_style = tab diff --git a/nvm.sh b/nvm.sh index 8683949..0e0dbdc 100644 --- a/nvm.sh +++ b/nvm.sh @@ -317,6 +317,15 @@ nvm_tree_contains_path() { [ "${pathdir}" = "${tree}" ] } +nvm_find_project_dir() { + local path_ + path_="${PWD}" + while [ "${path_}" != "" ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do + path_=${path_%/*} + done + nvm_echo "${path_}" +} + # Traverse up in directory tree to find containing folder nvm_find_up() { local path_ @@ -3654,7 +3663,7 @@ nvm() { nvm_normalize_version nvm_is_valid_version \ nvm_ensure_version_installed nvm_cache_dir \ nvm_version_path nvm_alias_path nvm_version_dir \ - nvm_find_nvmrc nvm_find_up nvm_tree_contains_path \ + nvm_find_nvmrc nvm_find_up nvm_find_project_dir nvm_tree_contains_path \ nvm_version_greater nvm_version_greater_than_or_equal_to \ nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \ nvm_has_system_node nvm_has_system_iojs \ diff --git a/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/bar/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/bar/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/package.json b/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested/node_modules/foo/bar/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested/node_modules/foo/bar/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-both/a/b/c/d/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/nested-both/a/b/c/d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-both/node_modules/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/nested-both/node_modules/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-both/package.json b/test/fast/Unit tests/mocks/project_dirs/nested-both/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-n_m/a/b/c/d/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/nested-n_m/a/b/c/d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-n_m/node_modules/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/nested-n_m/node_modules/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-pkg/a/b/c/d/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/nested-pkg/a/b/c/d/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/nested-pkg/package.json b/test/fast/Unit tests/mocks/project_dirs/nested-pkg/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/no-nesting-both/node_modules/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/no-nesting-both/node_modules/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/no-nesting-both/package.json b/test/fast/Unit tests/mocks/project_dirs/no-nesting-both/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/no-nesting-n_m/node_modules/.gitkeep b/test/fast/Unit tests/mocks/project_dirs/no-nesting-n_m/node_modules/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/mocks/project_dirs/no-nesting-pkg/package.json b/test/fast/Unit tests/mocks/project_dirs/no-nesting-pkg/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fast/Unit tests/nvm_find_project_dir b/test/fast/Unit tests/nvm_find_project_dir new file mode 100755 index 0000000..7dc46df --- /dev/null +++ b/test/fast/Unit tests/nvm_find_project_dir @@ -0,0 +1,33 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +set -ex + +TEST_DIR="$PWD/mocks/project_dirs" + +ACTUAL="$(PWD=$TEST_DIR/inside-n_m-nested/node_modules/foo/bar nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/inside-n_m-nested" ] || die "inside-n_m-nested: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/inside-n_m-nested-pkg/node_modules/foo/bar nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/inside-n_m-nested-pkg/node_modules/foo" ] || die "inside-n_m-nested-pkg: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/nested-both/a/b/c/d nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/nested-both" ] || die "nested-both: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/nested-pkg/a/b/c/d nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/nested-pkg" ] || die "nested-pkg: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/nested-n_m/a/b/c/d nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/nested-n_m" ] || die "nested-n_m: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/no-nesting-both nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/no-nesting-both" ] || die "no-nesting-both: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/no-nesting-n_m nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/no-nesting-n_m" ] || die "no-nesting-n_m: got ${ACTUAL}" + +ACTUAL="$(PWD=$TEST_DIR/no-nesting-pkg nvm_find_project_dir)" +[ "${ACTUAL}" = "$TEST_DIR/no-nesting-pkg" ] || die "no-nesting-pkg: got ${ACTUAL}"