Skip to content

Commit 42f315b

Browse files
torresxb1calavera
authored andcommitted
refactor: esbuild refactor for readability (aws#433)
1 parent 07edae9 commit 42f315b

File tree

5 files changed

+235
-236
lines changed

5 files changed

+235
-236
lines changed

aws_lambda_builders/workflows/nodejs_npm_esbuild/actions.py

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
LOG = logging.getLogger(__name__)
1313

1414
EXTERNAL_KEY = "external"
15+
# minimum esbuild version required to use "--external"
16+
MINIMUM_VERSION_FOR_EXTERNAL = "0.14.13"
1517

1618

1719
class EsbuildBundleAction(BaseAction):
@@ -27,40 +29,36 @@ class EsbuildBundleAction(BaseAction):
2729

2830
def __init__(
2931
self,
30-
scratch_dir: str,
31-
artifacts_dir: str,
32+
working_directory: str,
33+
output_directory: str,
3234
bundler_config: Dict[str, Any],
3335
osutils: OSUtils,
3436
subprocess_esbuild: SubprocessEsbuild,
3537
manifest: str,
3638
skip_deps=False,
3739
):
3840
"""
39-
:type scratch_dir: str
40-
:param scratch_dir: an existing (writable) directory for temporary files
41-
42-
:type artifacts_dir: str
43-
:param artifacts_dir: an existing (writable) directory where to store the output.
41+
Parameters
42+
----------
43+
working_directory : str
44+
directory where esbuild is executed
45+
output_directory : str
46+
an existing (writable) directory where to store the output.
4447
Note that the actual result will be in the 'package' subdirectory here.
45-
46-
:type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
47-
:param osutils: An instance of OS Utilities for file manipulation
48-
49-
:type subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
50-
:param subprocess_esbuild: An instance of the Esbuild process wrapper
51-
52-
:type skip_deps: bool
53-
:param skip_deps: if dependencies should be omitted from bundling
54-
55-
:type bundler_config: Dict[str,Any]
56-
:param bundler_config: the bundler configuration
57-
58-
:type manifest: str
59-
:param manifest: path to package.json file contents to read
48+
bundler_config : Dict[str, Any]
49+
the bundle configuration
50+
osutils : OSUtils
51+
An instance of OS Utilities for file manipulation
52+
subprocess_esbuild : SubprocessEsbuild
53+
An instance of the Esbuild process wrapper
54+
manifest : str
55+
path to package.json file contents to read
56+
skip_deps : bool, optional
57+
if dependencies should be omitted from bundling, by default False
6058
"""
6159
super(EsbuildBundleAction, self).__init__()
62-
self._scratch_dir = scratch_dir
63-
self._artifacts_dir = artifacts_dir
60+
self._working_directory = working_directory
61+
self._output_directory = output_directory
6462
self._bundler_config = bundler_config
6563
self._osutils = osutils
6664
self._subprocess_esbuild = subprocess_esbuild
@@ -71,13 +69,21 @@ def execute(self) -> None:
7169
"""
7270
Runs the action.
7371
74-
:raises lambda_builders.actions.ActionFailedError: when esbuild packaging fails
72+
Raises
73+
------
74+
ActionFailedError
75+
when esbuild packaging fails
7576
"""
7677
esbuild_command = EsbuildCommandBuilder(
77-
self._scratch_dir, self._artifacts_dir, self._bundler_config, self._osutils, self._manifest
78+
self._working_directory, self._output_directory, self._bundler_config, self._osutils, self._manifest
7879
)
7980

8081
if self._should_bundle_deps_externally():
82+
check_minimum_esbuild_version(
83+
minimum_version_required=MINIMUM_VERSION_FOR_EXTERNAL,
84+
working_directory=self._working_directory,
85+
subprocess_esbuild=self._subprocess_esbuild,
86+
)
8187
esbuild_command.build_with_no_dependencies()
8288
if EXTERNAL_KEY in self._bundler_config:
8389
# Already marking everything as external,
@@ -89,7 +95,7 @@ def execute(self) -> None:
8995
)
9096

9197
try:
92-
self._subprocess_esbuild.run(args, cwd=self._scratch_dir)
98+
self._subprocess_esbuild.run(args, cwd=self._working_directory)
9399
except EsbuildExecutionError as ex:
94100
raise ActionFailedError(str(ex))
95101

@@ -103,65 +109,62 @@ def _should_bundle_deps_externally(self) -> bool:
103109
return self._skip_deps or "./node_modules/*" in self._bundler_config.get(EXTERNAL_KEY, [])
104110

105111

106-
class EsbuildCheckVersionAction(BaseAction):
107-
"""
108-
A Lambda Builder Action that verifies that esbuild is a version supported by sam accelerate
112+
def check_minimum_esbuild_version(
113+
minimum_version_required: str, working_directory: str, subprocess_esbuild: SubprocessEsbuild
114+
):
109115
"""
116+
Checks esbuild version against a minimum version required.
110117
111-
NAME = "EsbuildCheckVersion"
112-
DESCRIPTION = "Checking esbuild version"
113-
PURPOSE = Purpose.COMPILE_SOURCE
118+
Parameters
119+
----------
120+
minimum_version_required: str
121+
minimum esbuild version required for check to pass
114122
115-
MIN_VERSION = "0.14.13"
123+
working_directory: str
124+
directory where esbuild is executed
116125
117-
def __init__(self, scratch_dir, subprocess_esbuild):
118-
"""
119-
:type scratch_dir: str
120-
:param scratch_dir: temporary directory where esbuild is executed
126+
subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
127+
An instance of the Esbuild process wrapper
121128
122-
:type subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
123-
:param subprocess_esbuild: An instance of the Esbuild process wrapper
124-
"""
125-
super().__init__()
126-
self.scratch_dir = scratch_dir
127-
self.subprocess_esbuild = subprocess_esbuild
129+
Raises
130+
----------
131+
lambda_builders.actions.ActionFailedError
132+
when esbuild version checking fails
133+
"""
134+
args = ["--version"]
128135

129-
def execute(self):
130-
"""
131-
Runs the action.
136+
try:
137+
version = subprocess_esbuild.run(args, cwd=working_directory)
138+
except EsbuildExecutionError as ex:
139+
raise ActionFailedError(str(ex))
132140

133-
:raises lambda_builders.actions.ActionFailedError: when esbuild version checking fails
134-
"""
135-
args = ["--version"]
141+
LOG.debug("Found esbuild with version: %s", version)
136142

137-
try:
138-
version = self.subprocess_esbuild.run(args, cwd=self.scratch_dir)
139-
except EsbuildExecutionError as ex:
140-
raise ActionFailedError(str(ex))
143+
try:
144+
check_version = _get_version_tuple(minimum_version_required)
145+
esbuild_version = _get_version_tuple(version)
141146

142-
LOG.debug("Found esbuild with version: %s", version)
147+
if esbuild_version < check_version:
148+
raise ActionFailedError(
149+
f"Unsupported esbuild version. To use a dependency layer, the esbuild version must be at "
150+
f"least {minimum_version_required}. Version found: {version}"
151+
)
152+
except (TypeError, ValueError) as ex:
153+
raise ActionFailedError(f"Unable to parse esbuild version: {str(ex)}")
143154

144-
try:
145-
check_version = EsbuildCheckVersionAction._get_version_tuple(self.MIN_VERSION)
146-
esbuild_version = EsbuildCheckVersionAction._get_version_tuple(version)
147-
148-
if esbuild_version < check_version:
149-
raise ActionFailedError(
150-
f"Unsupported esbuild version. To use a dependency layer, the esbuild version must be at "
151-
f"least {self.MIN_VERSION}. Version found: {version}"
152-
)
153-
except (TypeError, ValueError) as ex:
154-
raise ActionFailedError(f"Unable to parse esbuild version: {str(ex)}")
155-
156-
@staticmethod
157-
def _get_version_tuple(version_string):
158-
"""
159-
Get an integer tuple representation of the version for comparison
160155

161-
:type version_string: str
162-
:param version_string: string containing the esbuild version
156+
def _get_version_tuple(version_string: str):
157+
"""
158+
Get an integer tuple representation of the version for comparison
163159
164-
:rtype: tuple
165-
:return: version tuple used for comparison
166-
"""
167-
return tuple(map(int, version_string.split(".")))
160+
Parameters
161+
----------
162+
version_string: str
163+
string containing the esbuild version
164+
165+
Returns
166+
----------
167+
tuple
168+
version tuple used for comparison
169+
"""
170+
return tuple(map(int, version_string.split(".")))

0 commit comments

Comments
 (0)