|
15 | 15 | ) |
16 | 16 | from aws_lambda_builders.path_resolver import PathResolver |
17 | 17 | from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability |
18 | | - |
19 | | -from .actions import ( |
| 18 | +from aws_lambda_builders.workflows.nodejs_npm.actions import ( |
20 | 19 | NodejsNpmCIAction, |
21 | 20 | NodejsNpmInstallAction, |
22 | 21 | NodejsNpmLockFileCleanUpAction, |
23 | 22 | NodejsNpmPackAction, |
24 | 23 | NodejsNpmrcAndLockfileCopyAction, |
25 | 24 | NodejsNpmrcCleanUpAction, |
26 | 25 | ) |
27 | | -from .npm import SubprocessNpm |
28 | | -from .utils import OSUtils |
| 26 | +from aws_lambda_builders.workflows.nodejs_npm.npm import SubprocessNpm |
| 27 | +from aws_lambda_builders.workflows.nodejs_npm.utils import OSUtils |
29 | 28 |
|
30 | 29 | LOG = logging.getLogger(__name__) |
31 | 30 |
|
| 31 | +# npm>=8.8.0 supports --install-links |
| 32 | +MINIMUM_NPM_VERSION_INSTALL_LINKS = (8, 8) |
| 33 | +UNSUPPORTED_NPM_VERSION_MESSAGE = ( |
| 34 | + "Building in source was enabled, however the " |
| 35 | + "currently installed npm version does not support " |
| 36 | + "--install-links. Please ensure that the npm " |
| 37 | + "version is at least 8.8.0. Switching to build " |
| 38 | + f"in outside of the source directory.{os.linesep}" |
| 39 | + "https://docs.npmjs.com/cli/v8/using-npm/changelog#v880-2022-04-27" |
| 40 | +) |
| 41 | + |
32 | 42 |
|
33 | 43 | class NodejsNpmWorkflow(BaseWorkflow): |
34 | 44 |
|
@@ -89,6 +99,12 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim |
89 | 99 | self.actions.append(CopySourceAction(self.source_dir, artifacts_dir, excludes=self.EXCLUDED_FILES)) |
90 | 100 |
|
91 | 101 | if self.download_dependencies: |
| 102 | + if is_building_in_source and not self.can_use_install_links(subprocess_npm): |
| 103 | + LOG.warning(UNSUPPORTED_NPM_VERSION_MESSAGE) |
| 104 | + |
| 105 | + is_building_in_source = False |
| 106 | + self.build_dir = self._select_build_dir(build_in_source=False) |
| 107 | + |
92 | 108 | self.actions.append( |
93 | 109 | NodejsNpmWorkflow.get_install_action( |
94 | 110 | source_dir=source_dir, |
@@ -235,3 +251,40 @@ def get_install_action( |
235 | 251 | return NodejsNpmInstallAction( |
236 | 252 | install_dir=install_dir, subprocess_npm=subprocess_npm, install_links=install_links |
237 | 253 | ) |
| 254 | + |
| 255 | + @staticmethod |
| 256 | + def can_use_install_links(npm_process: SubprocessNpm) -> bool: |
| 257 | + """ |
| 258 | + Checks the version of npm that is currently installed to determine |
| 259 | + whether or not --install-links can be used |
| 260 | +
|
| 261 | + Parameters |
| 262 | + ---------- |
| 263 | + npm_process: SubprocessNpm |
| 264 | + Object containing helper methods to call the npm process |
| 265 | +
|
| 266 | + Returns |
| 267 | + ------- |
| 268 | + bool |
| 269 | + True if the current npm version meets the minimum for --install-links |
| 270 | + """ |
| 271 | + try: |
| 272 | + current_version = npm_process.run(["--version"]) |
| 273 | + |
| 274 | + LOG.debug(f"Currently installed version of npm is: {current_version}") |
| 275 | + |
| 276 | + current_version = current_version.split(".") |
| 277 | + |
| 278 | + major_version = int(current_version[0]) |
| 279 | + minor_version = int(current_version[1]) |
| 280 | + except (ValueError, IndexError): |
| 281 | + LOG.debug(f"Failed to parse {current_version} output from npm for --install-links validation") |
| 282 | + return False |
| 283 | + |
| 284 | + is_older_major_version = major_version < MINIMUM_NPM_VERSION_INSTALL_LINKS[0] |
| 285 | + is_older_patch_version = ( |
| 286 | + major_version == MINIMUM_NPM_VERSION_INSTALL_LINKS[0] |
| 287 | + and minor_version < MINIMUM_NPM_VERSION_INSTALL_LINKS[1] |
| 288 | + ) |
| 289 | + |
| 290 | + return not (is_older_major_version or is_older_patch_version) |
0 commit comments