|
| 1 | +# |
| 2 | +# CMake Find NPM Package Manager by Parra Studios |
| 3 | +# CMake script to find NPM Package Manager. |
| 4 | +# |
| 5 | +# Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]> |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +# Find NPM executable and paths |
| 21 | +# |
| 22 | +# NPM_FOUND - True if npm was found |
| 23 | +# NPM_GLOBAL_DIR - The global node_modules directory |
| 24 | +# NPM_EXECUTABLE - The path to the npm executable |
| 25 | +# NPM_VERSION - The version number |
| 26 | + |
| 27 | +set(NPM_ROOT /usr/bin CACHE STRING "NPM directory") |
| 28 | + |
| 29 | +find_program(NPM_EXECUTABLE |
| 30 | + NAMES npm |
| 31 | + HINTS /usr $ENV{NPM_ROOT}/npm ${NPM_ROOT}/npm |
| 32 | +) |
| 33 | + |
| 34 | +if(NOT NPM_EXECUTABLE) |
| 35 | + if(NPM_FIND_REQUIRED) |
| 36 | + message(FATAL_ERROR "NPM was not found") |
| 37 | + endif() |
| 38 | + |
| 39 | + return() |
| 40 | +endif() |
| 41 | + |
| 42 | +# Get NPM version |
| 43 | +execute_process(COMMAND ${NPM_EXECUTABLE} -v |
| 44 | + OUTPUT_VARIABLE NPM_VERSION |
| 45 | + ERROR_VARIABLE NPM_VERSION_ERROR |
| 46 | + RESULT_VARIABLE NPM_VERSION_CODE |
| 47 | +) |
| 48 | + |
| 49 | +if(NPM_VERSION_CODE) |
| 50 | + if(NPM_FIND_REQUIRED) |
| 51 | + message(FATAL_ERROR "${NPM_EXECUTABLE} -v failed:\n${NPM_VERSION_CODE}") |
| 52 | + endif() |
| 53 | +endif() |
| 54 | + |
| 55 | +if(NPM_VERSION) |
| 56 | + string(STRIP ${NPM_VERSION} NPM_VERSION) |
| 57 | +endif() |
| 58 | + |
| 59 | +# Get global node_modules location |
| 60 | +execute_process(COMMAND ${NPM_EXECUTABLE} root -g |
| 61 | + OUTPUT_VARIABLE NPM_GLOBAL_DIR |
| 62 | + ERROR_VARIABLE NPM_GLOBAL_DIR_ERROR |
| 63 | + RESULT_VARIABLE NPM_GLOBAL_DIR_CODE |
| 64 | +) |
| 65 | + |
| 66 | +if(NPM_GLOBAL_DIR) |
| 67 | + string(STRIP ${NPM_GLOBAL_DIR} NPM_GLOBAL_DIR) |
| 68 | +endif() |
| 69 | + |
| 70 | +if(NPM_GLOBAL_DIR_CODE) |
| 71 | + if(NPM_FIND_REQUIRED) |
| 72 | + message(FATAL_ERROR "${NPM_EXECUTABLE} root -g failed:\n${NPM_GLOBAL_DIR_ERROR}") |
| 73 | + endif() |
| 74 | +endif() |
| 75 | + |
| 76 | +include(FindPackageHandleStandardArgs) |
| 77 | + |
| 78 | +find_package_handle_standard_args(NPM |
| 79 | + FOUND_VAR NPM_FOUND |
| 80 | + REQUIRED_VARS NPM_EXECUTABLE |
| 81 | + VERSION_VAR NPM_VERSION |
| 82 | +) |
| 83 | + |
| 84 | +mark_as_advanced(NPM_FOUND NPM_GLOBAL_DIR NPM_EXECUTABLE NPM_VERSION) |
0 commit comments