File nodejs-basedpyright.spec of Package nodejs-basedpyright

#
# spec file for package basedpyright
#
# Copyright (c) Andreas Schneider <asn@cryptomilk.org>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via http://bugs.opensuse.org/
#


%define pkg_name basedpyright

Name:           nodejs-basedpyright
Version:        1.37.4
Release:        0
Summary:        Pyright fork with various type checking improvements and pylance features
License:        MIT
Url:            https://github.com/DetachHead/basedpyright
Source0:        %{url}/archive/v%{version}/%{pkg_name}-%{version}.tar.gz
# Create with `bash prepare_vendor.sh`
Source1:        %{pkg_name}-%{version}-vendor.tar.zst
BuildRequires:  dos2unix
BuildRequires:  fdupes
BuildRequires:  npm >= 16
BuildRequires:  nodejs-packaging
BuildRequires:  nodejs-devel
BuildRequires:  rsync
BuildRequires:  zstd
%if 0%{?suse_version}
BuildRequires:  esbuild
%else
# Fedora: use vendored esbuild, only x86_64 supported
ExclusiveArch:  x86_64
%endif
%if 0%{?suse_version}
BuildRequires:  python311
BuildRequires:  python311-setuptools
%else
BuildRequires:  python3-setuptools
%endif
BuildArch:      noarch

%description
Basedpyright is a fork of pyright with various type checking improvements,
pylance features and more. It is a fast type checker meant for large Python
source bases. It can run in a "watch" mode and performs fast incremental
updates when files are modified.

%prep
%autosetup -n %{pkg_name}-%{version} -a1 -p1

sed -i '/"postinstall": .*/d' package.json
dos2unix README.md

%if 0%{?suse_version}
# SUSE: Use system esbuild binary instead of vendored platform-specific one
rm -rf node_modules/@esbuild

# Detect esbuild platform from machine arch
case "$(uname -m)" in
    x86_64)  ESBUILD_PLATFORM="linux-x64" ;;
    aarch64) ESBUILD_PLATFORM="linux-arm64" ;;
    *)       echo "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac

# Create stub @esbuild package with symlinked binary
mkdir -p "node_modules/@esbuild/${ESBUILD_PLATFORM}/bin"
ln -sf %{_bindir}/esbuild "node_modules/@esbuild/${ESBUILD_PLATFORM}/bin/esbuild"
cat > "node_modules/@esbuild/${ESBUILD_PLATFORM}/package.json" << EOF
{"name": "@esbuild/${ESBUILD_PLATFORM}", "version": "0.27.2", "main": "index.js", "bin": {"esbuild": "bin/esbuild"}}
EOF
cat > "node_modules/@esbuild/${ESBUILD_PLATFORM}/index.js" << EOF
module.exports = require("path").join(__dirname, "bin", "esbuild");
EOF
%endif
# Fedora: Use vendored esbuild from vendor tarball (x86_64 only)
# This is only required for building, we only package js code.

%build
export PATH="$(pwd)/node_modules/.bin:$(pwd)/packages/pyright/node_modules/.bin:$PATH"

%if 0%{?suse_version}
# Tell esbuild to use system binary
export ESBUILD_BINARY_PATH=%{_bindir}/esbuild
%else
# Fedora: use vendored esbuild binary from vendor tarball
# If system esbuild version matches vendored version, could use:
# case "$(uname -m)" in
#     x86_64)  ESBUILD_PLATFORM="linux-x64" ;;
#     aarch64) ESBUILD_PLATFORM="linux-arm64" ;;
# esac
# export ESBUILD_BINARY_PATH="%{_prefix}/lib/node_modules/@esbuild/${ESBUILD_PLATFORM}/bin/esbuild"
%endif

# Build the pyright CLI - webpack bundles everything to pure JS
pushd packages/pyright/
npm run build
popd

%install
export NODEJS_DEFAULT_VER=$(echo %nodejs_version | sed 's/\..*//')

install -d -m 0755 %{buildroot}%{_prefix}/lib/node_modules/%{pkg_name}/

sed -i 's#!/usr/bin/env node#!/usr/bin/node#' packages/pyright/index.js
sed -i 's#!/usr/bin/env node#!/usr/bin/node#' packages/pyright/langserver.index.js
rsync -av \
    packages/pyright/dist \
    packages/pyright/index.js \
    packages/pyright/langserver.index.js \
    %{buildroot}%{_prefix}/lib/node_modules/%{pkg_name}/

install -d -m 0755 %{buildroot}%{_bindir}
ln -s %{_prefix}/lib/node_modules/%{pkg_name}/index.js %{buildroot}%{_bindir}/%{pkg_name}
ln -s %{_prefix}/lib/node_modules/%{pkg_name}/langserver.index.js %{buildroot}%{_bindir}/%{pkg_name}-langserver

find %{buildroot}%{_prefix}/lib/node_modules/%{pkg_name} -type f -size 0 -delete
%fdupes %{buildroot}%{_prefix}/lib/node_modules/%{pkg_name}

%check
npm run check

%files
%license LICENSE.txt
%doc README.md
%{_bindir}/%{pkg_name}
%{_bindir}/%{pkg_name}-langserver
%dir %{_prefix}/lib/node_modules
%{_prefix}/lib/node_modules/%{pkg_name}/

%changelog
openSUSE Build Service is sponsored by