in aws_lambda_builders/workflows/nodejs_npm/workflow.py [0:0]
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
super(NodejsNpmWorkflow, self).__init__(
source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=runtime, **kwargs
)
if osutils is None:
osutils = OSUtils()
self.osutils = osutils
if not osutils.file_exists(manifest_path):
LOG.warning("package.json file not found. Continuing the build without dependencies.")
self.actions = [CopySourceAction(source_dir, artifacts_dir, excludes=self.EXCLUDED_FILES)]
return
subprocess_npm = SubprocessNpm(osutils)
tar_dest_dir = osutils.joinpath(scratch_dir, "unpacked")
tar_package_dir = osutils.joinpath(tar_dest_dir, "package")
# TODO: we should probably unpack straight into artifacts dir, rather than unpacking into tar_dest_dir and
# then copying into artifacts. Just make sure EXCLUDED_FILES are not included, or remove them.
npm_pack = NodejsNpmPackAction(
tar_dest_dir, scratch_dir, manifest_path, osutils=osutils, subprocess_npm=subprocess_npm
)
npm_copy_npmrc_and_lockfile = NodejsNpmrcAndLockfileCopyAction(tar_package_dir, source_dir, osutils=osutils)
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
self.actions = [
npm_pack,
npm_copy_npmrc_and_lockfile,
CopySourceAction(tar_package_dir, artifacts_dir, excludes=self.EXCLUDED_FILES),
]
if is_external_manifest:
# npm pack only copies source code if the manifest is in the same directory as the source code, we need to
# copy the source code if the customer specified a different manifest path
self.actions.append(CopySourceAction(self.source_dir, artifacts_dir, excludes=self.EXCLUDED_FILES))
if self.download_dependencies:
if is_building_in_source and not self.can_use_install_links(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=subprocess_npm,
osutils=osutils,
build_options=self.options,
is_building_in_source=is_building_in_source,
)
)
self.actions.append(
NodejsNpmTestAction(
install_dir=self.manifest_dir if is_building_in_source and is_external_manifest else self.build_dir,
subprocess_npm=subprocess_npm,
)
)
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)
)
if self.download_dependencies and is_building_in_source:
self.actions += self._actions_for_linking_source_dependencies_to_artifacts
# if no dependencies dir, just cleanup artifacts and we're done
if not self.dependencies_dir:
self.actions += self._actions_for_cleanup
return
# if we downloaded dependencies, update dependencies_dir
if self.download_dependencies:
self.actions += self._actions_for_updating_dependencies_dir
# otherwise if we want to use the dependencies from dependencies_dir and we want to combine them,
# then copy them into the artifacts dir
elif self.combine_dependencies:
self.actions.append(
CopySourceAction(self.dependencies_dir, artifacts_dir, maintain_symlinks=is_building_in_source)
)
self.actions += self._actions_for_cleanup