File nodejs.spec of Package nodejs22

%define realname node
%define realver  22.14.0
%define srcext   tar.xz

# See: deps/npm/package.json
%define npm_ver  10.9.2
# See: deps/corepack/package.json
%define corepack_ver 0.31.0

%define node_gen %(VER='%{realver}'; echo ${VER%%%%.*})
%define npm_gen  %(VER='%{npm_ver}'; echo ${VER%%%%.*})

%if 0%{?rhel} == 8
%define python3_pkg platform-python
%define python3     %{_libexecdir}/platform-python
%else
%define python3_pkg python3
%define python3     %{_bindir}/python3
%endif

%bcond_with    system_httpparser
%bcond_with    system_libuv
%bcond_with    system_nghttp2
%bcond_with    system_nghttp3
%bcond_with    system_ngtcp2
%bcond_without system_openssl
%bcond_with    system_uvwasi
%bcond_with    system_zlib
%bcond_with    system_brotli
%bcond_with    system_cares
%bcond_with    system_sqlite

# turn off the generation of debuginfo rpm  (RH9) ??
%global debug_package %{nil}

# Common info
Name:          nodejs%{node_gen}
Version:       %{realver}
%if 0%{?rhel} >= 7
%define        epoch 1
Epoch:         %{epoch}
%endif
Release:       %{?extraver:0.}1%{?dist}
License:       MIT
Group:         Development/Languages/NodeJS
URL:           https://nodejs.org
Summary:       JavaScript runtime built on Chrome's V8 JavaScript engine

# Install-time parameters
%if 0%{?suse_version}
Requires:      update-alternatives
%else
Requires:      chkconfig
%endif
Provides:      nodejs = %{?epoch:%{epoch}:}%{version}-%{release}
Obsoletes:     nodejs < %{?epoch:%{epoch}:}%{version}-%{release}
Provides:      nodejs(engine) = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:      nodejs(abi) = %{node_gen}.0
# openSUSE/SLES specific
Provides:      nodejs-common = %{?epoch:%{epoch}:}%{version}-%{release}
Obsoletes:     nodejs-common < %{?epoch:%{epoch}:}%{version}-%{release}

# Build-time parameters
BuildRequires: xz
BuildRequires: coreutils
BuildRequires: %{python3_pkg}%{?suse_version:-base} %{python3_pkg}-setuptools
BuildRequires: pkg-config
BuildRequires: gcc-c++
BuildRequires: ninja
### BuildRequires: http-parser-devel
%if %{with system_httpparser}
BuildRequires: http-parser-devel
%endif
%if %{with system_libuv}
BuildRequires: libuv-devel >= 1.49.2
%endif
%if %{with system_nghttp2}
BuildRequires: pkgconfig(libnghttp2) >= 1.64.0
%endif
%if %{with system_nghttp3}
BuildRequires: pkgconfig(libnghttp3) >= 1.6.0
%endif
%if %{with system_ngtcp2}
BuildRequires: pkgconfig(libngtcp2) >= 1.10.0
%endif
%if %{with system_openssl}
BuildRequires: openssl-devel >= 3.0.15
%endif
%if %{with system_uvwasi}
BuildRequires: pkgconfig(libuvwasi) >= 0.0.21
%endif
%if %{with system_zlib}
BuildRequires: zlib-devel >= 1.3.0.1
%endif
%if %{with system_brotli}
BuildRequires: pkgconfig(libbrotlidec) >= 1.1.0 pkgconfig(libbrotlienc) >= 1.1.0
%endif
%if %{with system_cares}
BuildRequires: pkgconfig(libcares) >= 1.34.4
%endif
%if %{with system_sqlite}
BuildRequires: pkgconfig(sqlite3) >= 3.47.2
%endif
%if %{with system_icu}
BuildRequires: pkgconfig(icu-i18n) >= 73
%endif
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
BuildRoot:     %{_tmppath}/%{name}-root
Source:        https://nodejs.org/dist/v%{realver}/node-v%{realver}.%{srcext}
# Script to set MANPATH
Source11:      npm-manpath.sh

%description
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build
scalable network applications.

Node.js is similar in design to, and influenced by, systems like Ruby's Event
Machine and Python's Twisted. Node.js takes the event model a bit further.
It presents an event loop as a runtime construct instead of as a library.

%package devel
Group:         Development/Languages/NodeJS
Summary:       Node.js development stuff
Requires:      %{name} = %{?epoch:%{epoch}:}%{version}-%{release}
%if 0%{?suse_version}
Requires:      update-alternatives
%else
Requires:      chkconfig
%endif
Provides:      nodejs-devel = %{?epoch:%{epoch}:}%{version}-%{release}
BuildArch:     noarch

%description devel
Node.js development headers and libraries.

%package -n npm
Version:       %{npm_ver}
%if 0%{?rhel} >= 7
%define        epoch 1
Epoch:         %{epoch}
%endif
License:       Artistic-2.0
Group:         Development/Languages/NodeJS
Summary:       Node.js package manager
BuildArch:     noarch
# See `engines` in deps/npm/package.json
Requires:      nodejs(engine) >= 12
Provides:      nodejs-npm = %{npm_ver}-%{release}
Provides:      npm(npm) = %{npm_ver}-%{release}
Provides:      npm%{node_gen} = %{?epoch:%{epoch}:}%{realver}-%{release}
Obsoletes:     npm%{node_gen} < %{?epoch:%{epoch}:}%{realver}-%{release}
Provides:      npm%{npm_gen} = %{npm_ver}
Obsoletes:     npm%{npm_gen} < %{npm_ver}
# openSUSE/SLES specific
Conflicts:     nodejs-common < %{?epoch:%{epoch}:}%{realver}-%{release}

%description -n npm
npm is the package manager for the Node JavaScript platform. It puts modules in
place so that node can find them, and manages dependency conflicts intelligently.

It is extremely configurable to support a wide variety of use cases. Most
commonly, it is used to publish, discover, install, and develop node programs.

%package -n corepack
Version:       %{corepack_ver}
License:       MIT
Group:         Development/Languages/NodeJS
Summary:       Package acting as bridge between Node projects and their package managers
BuildArch:     noarch
Requires:      nodejs(engine)
Provides:      nodejs-corepack = %{corepack_ver}-%{release}
Provides:      npm(corepack) = %{corepack_ver}-%{release}

%description -n corepack
Corepack is a zero-runtime-dependency Node.js script that acts as a bridge
between Node.js projects and the package managers they are intended to be used
with during development. In practical terms, Corepack will let you use Yarn and
pnpm without having to install them - just like what currently happens with npm,
which is shipped by Node.js by default.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-v%{realver}%{?extraver}
# Change node_modules directory
%{__sed} -i 's|lib/node_modules|share/node_modules|' tools/install.py

%build
_CFLAGS='%{optflags}'
_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro'
export CFLAGS="$_CFLAGS"
export CXXFLAGS="$_CFLAGS"
export LDFLAGS="$_LDFLAGS"
%{python3} configure.py \
 --prefix=%{_prefix} \
 %{?gcc_lto:--enable-lto} \
 --openssl-use-def-ca-store \
 --ninja \
 --verbose \
 \
%if %{with system_httpparser}
 --shared-http-parser \
%endif
%if %{with system_libuv}
 --shared-libuv \
%endif
%if %{with system_nghttp2}
 --shared-nghttp2 \
%endif
%if %{with system_nghttp3}
 --shared-nghttp3 \
%endif
%if %{with system_ngtcp2}
 --shared-ngtcp2 \
%endif
%if %{with system_openssl}
 --shared-openssl \
%endif
%if %{with system_uvwasi}
 --shared-uvwasi \
%endif
%if %{with system_zlib}
 --shared-zlib \
%endif
%if %{with system_brotli}
 --shared-brotli \
%endif
%if %{with system_cares}
 --shared-cares \
%endif
%if %{with system_sqlite}
 --shared-sqlite \
%endif
%if %{with system_icu}
 --with-intl=system-icu \
%endif
 --download=none
%{__make} %{?_smp_mflags} \
 CFLAGS="$_CFLAGS" \
 CXXFLAGS="$_CFLAGS" \
 LDFLAGS="$_LDFLAGS"

%install
%{__make} install DESTDIR=%{buildroot}
# Prepare alternatives symlinks
%{__mv} %{buildroot}%{_bindir}/node              %{buildroot}%{_bindir}/node%{node_gen}
%{__mv} %{buildroot}%{_mandir}/man1/node.1       %{buildroot}%{_mandir}/man1/node%{node_gen}.1
%{__mv} %{buildroot}%{_includedir}/node          %{buildroot}%{_includedir}/node%{node_gen}
%{__mv} %{buildroot}%{_datadir}/doc/node         %{buildroot}%{_datadir}/node%{node_gen}
%{__mv} %{buildroot}%{_datadir}/node_modules/npm %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}
%{__ln_s} %{_datadir}/node_modules/npm%{?npm_gen}/bin/npm-cli.js %{buildroot}%{_bindir}/npm%{?npm_gen}
%{__ln_s} %{_datadir}/node_modules/npm%{?npm_gen}/bin/npx-cli.js %{buildroot}%{_bindir}/npx%{?npm_gen}
%{__install} -d %{buildroot}%{_sysconfdir}/alternatives
touch %{buildroot}%{_sysconfdir}/alternatives/node
touch %{buildroot}%{_sysconfdir}/alternatives/node.1.gz
touch %{buildroot}%{_sysconfdir}/alternatives/node-includes
touch %{buildroot}%{_sysconfdir}/alternatives/npm
touch %{buildroot}%{_sysconfdir}/alternatives/npx
touch %{buildroot}%{_sysconfdir}/alternatives/npm-mod
%{__ln_s} %{_sysconfdir}/alternatives/node          %{buildroot}%{_bindir}/node
%{__ln_s} %{_sysconfdir}/alternatives/node.1.gz     %{buildroot}%{_mandir}/man1/node.1.gz
%{__ln_s} %{_sysconfdir}/alternatives/node-includes %{buildroot}%{_includedir}/node
%{__ln_s} -fn %{_sysconfdir}/alternatives/npm       %{buildroot}%{_bindir}/npm
%{__ln_s} -fn %{_sysconfdir}/alternatives/npx       %{buildroot}%{_bindir}/npx
%{__ln_s} %{_sysconfdir}/alternatives/npm-mod       %{buildroot}%{_datadir}/node_modules/npm
# Install script setting MANPATH for npm manpages
%{__install} -d %{buildroot}%{_sysconfdir}/profile.d
%{__install} -m0644 %{S:11} %{buildroot}%{_sysconfdir}/profile.d/npm-manpath.sh
(
  cd %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}/
  # Fix shebang
  find . -type f -executable \
  | while read -r f; do
    %{__sed} -i "$f" \
      -e '1 s|^#!.* node$|#!%{_bindir}/node|' \
      -e '1 s|^#!.* python$|#!%{_bindir}/python|' \
      -e '1 s|^#!.* bash$|#!/bin/bash|' \
      -e '1 s|^#!.* sh$|#!/bin/sh|'
  done
  # Remove non-needed files
  %{__rm} -vf \
    node_modules/form-data/README.md.bak
)
# Config directory
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/nodejs
# Fix shebang
find %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}/node_modules/node-gyp/gyp/ -name \*.py \
  | while read FILE; do
      %{__sed} -ri '1 s|#!.+$|#!%{python3}|' $FILE
    done
# Handle file duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{_datadir}
%endif

%clean
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}

%files
%defattr(-,root,root)
%license LICENSE
%doc CHANGELOG.md GOVERNANCE.md README.md SECURITY.md
%{_bindir}/node%{node_gen}
%{_bindir}/node
%{_mandir}/man1/node.1.gz
%{_datadir}/node%{node_gen}/
%dir %{_sysconfdir}/nodejs
%dir %{_datadir}/node_modules
%doc %{_mandir}/man1/node%{node_gen}.1.gz
%ghost %{_sysconfdir}/alternatives/node
%ghost %{_sysconfdir}/alternatives/node.1.gz

%files devel
%defattr(-,root,root)
%doc CHANGELOG.md GOVERNANCE.md LICENSE README.md SECURITY.md
%doc CODE_OF_CONDUCT.md CONTRIBUTING.md doc/api
%{_includedir}/node%{node_gen}
%{_includedir}/node
%ghost %{_sysconfdir}/alternatives/node-includes

%files -n npm
%defattr(-,root,root)
%license deps/npm/LICENSE
%doc deps/npm/README.md
%config %{_sysconfdir}/profile.d/npm-manpath.sh
%{_bindir}/npm
%{_bindir}/npx
%{_datadir}/node_modules/npm
%{_bindir}/npm%{?npm_gen}
%{_bindir}/npx%{?npm_gen}
%{_datadir}/node_modules/npm%{?npm_gen}/
%ghost %{_sysconfdir}/alternatives/npm
%ghost %{_sysconfdir}/alternatives/npx
%ghost %{_sysconfdir}/alternatives/npm-mod

%files -n corepack
%defattr(-,root,root)
%license deps/corepack/LICENSE.md
%doc deps/corepack/README.md
%{_bindir}/corepack
%{_datadir}/node_modules/corepack/

%post
update-alternatives \
    --install %{_bindir}/node           node      %{_bindir}/node%{node_gen} %{node_gen} \
    --slave   %{_mandir}/man1/node.1.gz node.1.gz %{_mandir}/man1/node%{node_gen}.1.gz

%postun
if [ $1 -eq 0 ] ; then
    update-alternatives --remove node %{_bindir}/node%{node_gen}
fi

%post devel
update-alternatives \
    --install %{_includedir}/node node-includes %{_includedir}/node%{node_gen} %{node_gen}

%postun devel
if [ $1 -eq 0 ] ; then
    update-alternatives --remove node-includes %{_includedir}/node%{node_gen}
fi

%post -n npm
update-alternatives \
    --install %{_bindir}/npm               npm     %{_bindir}/npm%{?npm_gen} %{npm_gen} \
    --slave   %{_bindir}/npx               npx     %{_bindir}/npx%{?npm_gen} \
    --slave   %{_datadir}/node_modules/npm npm-mod %{_datadir}/node_modules/npm%{?npm_gen}

%postun -n npm
if [ $1 -eq 0 ] ; then
    update-alternatives --remove npm %{_datadir}/node_modules/npm%{?npm_gen}
fi

%changelog
openSUSE Build Service is sponsored by