Browse Source

[New] add `nvm_find_project_dir` helper

Jordan Harband 5 years ago
parent
commit
cf92956e5d
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
  1. 6
      .editorconfig
  2. 11
      nvm.sh
  3. 0
      test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/bar/.gitkeep
  4. 0
      test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/package.json
  5. 0
      test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested/node_modules/foo/bar/.gitkeep
  6. 0
      test/fast/Unit tests/mocks/project_dirs/nested-both/a/b/c/d/.gitkeep
  7. 0
      test/fast/Unit tests/mocks/project_dirs/nested-both/node_modules/.gitkeep
  8. 0
      test/fast/Unit tests/mocks/project_dirs/nested-both/package.json
  9. 0
      test/fast/Unit tests/mocks/project_dirs/nested-n_m/a/b/c/d/.gitkeep
  10. 0
      test/fast/Unit tests/mocks/project_dirs/nested-n_m/node_modules/.gitkeep
  11. 0
      test/fast/Unit tests/mocks/project_dirs/nested-pkg/a/b/c/d/.gitkeep
  12. 0
      test/fast/Unit tests/mocks/project_dirs/nested-pkg/package.json
  13. 0
      test/fast/Unit tests/mocks/project_dirs/no-nesting-both/node_modules/.gitkeep
  14. 0
      test/fast/Unit tests/mocks/project_dirs/no-nesting-both/package.json
  15. 0
      test/fast/Unit tests/mocks/project_dirs/no-nesting-n_m/node_modules/.gitkeep
  16. 0
      test/fast/Unit tests/mocks/project_dirs/no-nesting-pkg/package.json
  17. 33
      test/fast/Unit tests/nvm_find_project_dir

6
.editorconfig

@ -18,5 +18,11 @@ indent_size = false @@ -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

11
nvm.sh

@ -317,6 +317,15 @@ nvm_tree_contains_path() { @@ -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() { @@ -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 \

0
test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/bar/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested-pkg/node_modules/foo/package.json generated vendored

0
test/fast/Unit tests/mocks/project_dirs/inside-n_m-nested/node_modules/foo/bar/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/nested-both/a/b/c/d/.gitkeep

0
test/fast/Unit tests/mocks/project_dirs/nested-both/node_modules/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/nested-both/package.json

0
test/fast/Unit tests/mocks/project_dirs/nested-n_m/a/b/c/d/.gitkeep

0
test/fast/Unit tests/mocks/project_dirs/nested-n_m/node_modules/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/nested-pkg/a/b/c/d/.gitkeep

0
test/fast/Unit tests/mocks/project_dirs/nested-pkg/package.json

0
test/fast/Unit tests/mocks/project_dirs/no-nesting-both/node_modules/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/no-nesting-both/package.json

0
test/fast/Unit tests/mocks/project_dirs/no-nesting-n_m/node_modules/.gitkeep generated vendored

0
test/fast/Unit tests/mocks/project_dirs/no-nesting-pkg/package.json

33
test/fast/Unit tests/nvm_find_project_dir

@ -0,0 +1,33 @@ @@ -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}"
Loading…
Cancel
Save