Skip to content

Commit 5fb3368

Browse files
authored
refactor: simplify node npm workflow conditionals (#458)
1 parent 1d4db50 commit 5fb3368

File tree

2 files changed

+33
-69
lines changed

2 files changed

+33
-69
lines changed

aws_lambda_builders/workflows/nodejs_npm/workflow.py

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,6 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
6060

6161
subprocess_npm = SubprocessNpm(osutils)
6262

63-
self.actions = self.actions_without_bundler(
64-
source_dir, artifacts_dir, scratch_dir, manifest_path, osutils, subprocess_npm
65-
)
66-
67-
def actions_without_bundler(self, source_dir, artifacts_dir, scratch_dir, manifest_path, osutils, subprocess_npm):
68-
"""
69-
Generate a list of Nodejs build actions without a bundler
70-
71-
:type source_dir: str
72-
:param source_dir: an existing (readable) directory containing source files
73-
74-
:type artifacts_dir: str
75-
:param artifacts_dir: an existing (writable) directory where to store the output.
76-
77-
:type scratch_dir: str
78-
:param scratch_dir: an existing (writable) directory for temporary files
79-
80-
:type manifest_path: str
81-
:param manifest_path: path to package.json of an NPM project with the source to pack
82-
83-
:type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
84-
:param osutils: An instance of OS Utilities for file manipulation
85-
86-
:type subprocess_npm: aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm
87-
:param subprocess_npm: An instance of the NPM process wrapper
88-
89-
:rtype: list
90-
:return: List of build actions to execute
91-
"""
9263
tar_dest_dir = osutils.joinpath(scratch_dir, "unpacked")
9364
tar_package_dir = osutils.joinpath(tar_dest_dir, "package")
9465
npm_pack = NodejsNpmPackAction(
@@ -97,48 +68,46 @@ def actions_without_bundler(self, source_dir, artifacts_dir, scratch_dir, manife
9768

9869
npm_copy_npmrc_and_lockfile = NodejsNpmrcAndLockfileCopyAction(tar_package_dir, source_dir, osutils=osutils)
9970

100-
actions = [
71+
self.actions = [
10172
npm_pack,
10273
npm_copy_npmrc_and_lockfile,
10374
CopySourceAction(tar_package_dir, artifacts_dir, excludes=self.EXCLUDED_FILES),
10475
]
10576

10677
if self.download_dependencies:
107-
# installed the dependencies into artifact folder
108-
install_action = NodejsNpmWorkflow.get_install_action(
109-
source_dir, artifacts_dir, subprocess_npm, osutils, self.options
78+
# install the dependencies into artifact folder
79+
self.actions.append(
80+
NodejsNpmWorkflow.get_install_action(source_dir, artifacts_dir, subprocess_npm, osutils, self.options)
81+
)
82+
83+
artifacts_cleanup_actions = [
84+
NodejsNpmrcCleanUpAction(artifacts_dir, osutils=osutils),
85+
NodejsNpmLockFileCleanUpAction(artifacts_dir, osutils=osutils),
86+
]
87+
88+
# if no dependencies dir, just cleanup artifacts and we're done
89+
if not self.dependencies_dir:
90+
self.actions += artifacts_cleanup_actions
91+
return
92+
93+
# if we downloaded dependencies, update dependencies_dir
94+
if self.download_dependencies:
95+
# clean up the dependencies folder first
96+
self.actions.append(CleanUpAction(self.dependencies_dir))
97+
# if combine_dependencies is set, we should keep dependencies and source code in the artifact folder
98+
# while copying the dependencies. Otherwise we should separate the dependencies and source code
99+
dependencies_dir_update_action = (
100+
CopyDependenciesAction if self.combine_dependencies else MoveDependenciesAction
110101
)
111-
actions.append(install_action)
112-
113-
# if dependencies folder exists, copy or move dependencies from artifact folder to dependencies folder
114-
# depends on the combine_dependencies flag
115-
if self.dependencies_dir:
116-
# clean up the dependencies folder first
117-
actions.append(CleanUpAction(self.dependencies_dir))
118-
# if combine_dependencies is set, we should keep dependencies and source code in the artifact folder
119-
# while copying the dependencies. Otherwise we should separate the dependencies and source code
120-
if self.combine_dependencies:
121-
actions.append(CopyDependenciesAction(source_dir, artifacts_dir, self.dependencies_dir))
122-
else:
123-
actions.append(MoveDependenciesAction(source_dir, artifacts_dir, self.dependencies_dir))
124-
else:
125-
# if dependencies folder exists and not download dependencies, simply copy the dependencies from the
126-
# dependencies folder to artifact folder
127-
if self.dependencies_dir and self.combine_dependencies:
128-
actions.append(CopySourceAction(self.dependencies_dir, artifacts_dir))
129-
else:
130-
LOG.info(
131-
"download_dependencies is False and dependencies_dir is None. Copying the source files into the "
132-
"artifacts directory. "
133-
)
134-
135-
actions.append(NodejsNpmrcCleanUpAction(artifacts_dir, osutils=osutils))
136-
actions.append(NodejsNpmLockFileCleanUpAction(artifacts_dir, osutils=osutils))
137-
138-
if self.dependencies_dir:
139-
actions.append(NodejsNpmLockFileCleanUpAction(self.dependencies_dir, osutils=osutils))
140-
141-
return actions
102+
self.actions.append(dependencies_dir_update_action(source_dir, artifacts_dir, self.dependencies_dir))
103+
# otherwise if we want to use the dependencies from dependencies_dir and we want to combine them,
104+
# then copy them into the artifacts dir
105+
elif self.combine_dependencies:
106+
self.actions.append(CopySourceAction(self.dependencies_dir, artifacts_dir))
107+
108+
# cleanup
109+
self.actions += artifacts_cleanup_actions
110+
self.actions.append(NodejsNpmLockFileCleanUpAction(self.dependencies_dir, osutils=osutils))
142111

143112
def get_resolvers(self):
144113
"""

tests/integration/workflows/nodejs_npm/test_nodejs_npm.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,6 @@ def test_builds_project_with_remote_dependencies_without_download_dependencies_w
249249
output_files = set(os.listdir(self.artifacts_dir))
250250
self.assertEqual(expected_files, output_files)
251251

252-
mock_info.assert_called_with(
253-
"download_dependencies is False and dependencies_dir is None. Copying the source files into the "
254-
"artifacts directory. "
255-
)
256-
257252
@parameterized.expand([("nodejs12.x",), ("nodejs14.x",), ("nodejs16.x",), ("nodejs18.x",)])
258253
def test_builds_project_without_combine_dependencies(self, runtime):
259254
source_dir = os.path.join(self.TEST_DATA_FOLDER, "npm-deps")

0 commit comments

Comments
 (0)