# Copyright Kevin Deldycke <kevin@deldycke.com> and contributors.## This program is Free Software; you can redistribute it and/or# modify it under the terms of the GNU General Public License# as published by the Free Software Foundation; either version 2# of the License, or (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.from__future__importannotationsimportjsonfromtypingimportAny,Iteratorfromclick_extra.testingimportTArg,TNestedArgsfromextra_platformsimportALL_PLATFORMSfrommeta_package_manager.baseimportPackage,PackageManagerfrommeta_package_manager.capabilitiesimport(search_capabilities,version_not_implemented,)
[docs]classNPM(PackageManager):name="Node's npm"homepage_url="https://www.npmjs.com"platforms=ALL_PLATFORMSrequirement="4.0.0"pre_args=(# Operates in "global" mode, so that packages are installed into the# prefix folder instead of the current working directory."--global",# Suppress the progress bar."--no-progress",# Suppress the update notification when using an older version of npm than# the latest."--no-update-notifier",# Hide the message displayed at the end of each install that acknowledges# the number of dependencies looking for funding."--no-fund",# Disable sending of audit reports to the configured registries."--no-audit",)""" .. code-block:: shell-session ► npm --version 6.13.7 """
[docs]defrun_cli(self,*args:TArg|TNestedArgs,**kwargs:Any)->str:"""Like the common run_cli helper, but silence NPM's JSON output on error. NPM is prone to breakage if local node version is not in sync: .. code-block:: shell-session ► npm --global --no-progress --no-update-notifier --no-fund --no-audit \ --json outdated { "error": { "code": "ERR_OUT_OF_RANGE", "summary": "The value of \"err\" is out of range. Received 536870212", "detail": "" } } """output=super().run_cli(*args,**kwargs)# NPM fatal errors are reported both in <stderr> output and as JSON. So we# silence the errors in JSON so they get reported in CLI output (as# they're already featured in self.cli_errors) without raising error# (unless the --stop-on-error option is provided).if"--json"inargsandoutputandself.cli_errors:output=""returnoutput
[docs]defupgrade_all_cli(self)->tuple[str,...]:"""Generates the CLI to upgrade all packages (default) or only the one provided as parameter. .. code-block:: shell-session ► npm --global --no-progress --no-update-notifier --no-fund --no-audit \ upgrade """returnself.build_cli("update")
@version_not_implementeddefupgrade_one_cli(self,package_id:str,version:str|None=None,)->tuple[str,...]:"""Generates the CLI to upgrade the package provided as parameter. .. code-block:: shell-session ► npm --global --no-progress --no-update-notifier --no-fund --no-audit \ upgrade raven """returnself.build_cli("upgrade",f"{package_id}")
[docs]defremove(self,package_id:str)->str:"""Remove one package and one only. .. code-block:: shell-session ► npm --global --no-progress --no-update-notifier --no-fund --no-audit \ uninstall raven """returnself.run_cli("uninstall",package_id)