in aws_lambda_builders/workflows/nodejs_npm_esbuild/workflow.py [0:0]
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
super(NodejsNpmEsbuildWorkflow, self).__init__(
source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=runtime, **kwargs
)
self.osutils = osutils or OSUtils()
self.subprocess_npm = SubprocessNpm(self.osutils)
self.subprocess_esbuild = self._get_esbuild_subprocess()
self.manifest_dir = self.osutils.dirname(self.manifest_path)
is_building_in_source = self.build_dir == self.source_dir
is_external_manifest = self.manifest_dir != self.source_dir
bundler_config = self.get_build_properties()
if not self.osutils.file_exists(manifest_path):
LOG.warning("package.json file not found. Bundling source without installing dependencies.")
self.actions = [
EsbuildBundleAction(
working_directory=self.source_dir,
output_directory=self.artifacts_dir,
bundler_config=bundler_config,
osutils=self.osutils,
subprocess_esbuild=self.subprocess_esbuild,
manifest=self.manifest_path,
)
]
return
if not self.download_dependencies and not self.dependencies_dir:
# Invalid workflow, can't have no dependency dir and no installation
raise EsbuildExecutionError(
message="Lambda Builders was unable to find the location of the dependencies since a "
"dependencies directory was not provided and downloading dependencies is disabled."
)
# if we're building in the source directory, we don't have to copy the source code
self.actions = (
[]
if is_building_in_source
else [CopySourceAction(source_dir=self.source_dir, dest_dir=self.build_dir, excludes=self.EXCLUDED_FILES)]
)
if is_external_manifest and not is_building_in_source:
# copy the manifest file (package.json) to the build directory in case if the manifest file is not in the
# same directory as the source code, and customer is not building in source.
self.actions.append(
CopySourceAction(source_dir=self.manifest_dir, dest_dir=self.build_dir, excludes=self.EXCLUDED_FILES)
)
if self.download_dependencies:
if is_building_in_source and not NodejsNpmWorkflow.can_use_install_links(self.subprocess_npm):
LOG.warning(UNSUPPORTED_NPM_VERSION_MESSAGE)
is_building_in_source = False
self.build_dir = self._select_build_dir(build_in_source=False)
self.actions.append(
NodejsNpmWorkflow.get_install_action(
source_dir=source_dir,
# run npm install in the directory where the manifest (package.json) exists if customer is building
# in source, and manifest directory is different from source.
# This will let NPM find the local dependencies that are defined in the manifest file (they are
# usually defined as relative to the manifest location, and that is why we run `npm install` in the
# manifest directory instead of source directory).
# If customer is not building in source, so it is ok to run `npm install` in the build
# directory (the artifacts directory in this case), as the local dependencies are not supported.
install_dir=self.manifest_dir if is_building_in_source and is_external_manifest else self.build_dir,
subprocess_npm=self.subprocess_npm,
osutils=self.osutils,
build_options=self.options,
is_building_in_source=is_building_in_source,
)
)
if is_building_in_source and is_external_manifest:
# Since we run `npm install` in the manifest directory, so we need to link the node_modules directory in
# the source directory.
source_dependencies_path = os.path.join(self.source_dir, "node_modules")
manifest_dependencies_path = os.path.join(self.manifest_dir, "node_modules")
self.actions.append(
LinkSinglePathAction(source=manifest_dependencies_path, dest=source_dependencies_path)
)
bundle_action = EsbuildBundleAction(
working_directory=self.build_dir,
output_directory=self.artifacts_dir,
bundler_config=bundler_config,
osutils=self.osutils,
subprocess_esbuild=self.subprocess_esbuild,
manifest=self.manifest_path,
skip_deps=not self.combine_dependencies,
)
# If there's no dependencies_dir, just bundle and we're done.
# Same thing if we're building in the source directory (since the dependencies persist in
# the source directory, we don't want to move them or symlink them back to the source)
if not self.dependencies_dir or is_building_in_source:
self.actions.append(bundle_action)
return
if self.download_dependencies:
# if we downloaded dependencies, bundle and update dependencies_dir
self.actions += [
bundle_action,
CleanUpAction(self.dependencies_dir),
MoveDependenciesAction(self.source_dir, self.scratch_dir, self.dependencies_dir, self.manifest_dir),
]
else:
# if we're reusing dependencies, then we need to symlink them before bundling
self.actions += [
LinkSourceAction(self.dependencies_dir, self.scratch_dir),
bundle_action,
]