%bcond check 1

Name:           coffeescript
Version:        2.7.0
Release:        0.3%{?dist}
Summary:        A programming language that transcompiles to JavaScript
License:        MIT
URL:            http://coffeescript.org/
Source0:        https://github.com/jashkenas/%{name}/archive/%{version}/%{name}-%{version}.tar.gz
BuildArch:      noarch
ExclusiveArch:  %{nodejs_arches} noarch

Source1:        %{name}-%{version}-bundled-licenses.txt
Source2:        %{name}-%{version}-nm-dev.tgz

BuildRequires:  nodejs
BuildRequires:  nodejs-packaging
BuildRequires:  web-assets-devel

Obsoletes:      coffee-script < 1.10.0-20

Requires:       js-%{name} == %{version}-%{release}


%description
CoffeeScript is a little language that compiles into JavaScript. Underneath all
of those embarrassing braces and semicolons, JavaScript has always had a
gorgeous object model at its heart. CoffeeScript is an attempt to expose the
good parts of JavaScript in a simple way.

The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles
one-to-one into the equivalent JS, and there is no interpretation at runtime.
You can use any existing JavaScript library seamlessly (and vice-versa). The
compiled output is readable and pretty-printed, passes through JavaScript Lint
without warnings, will work in every JavaScript implementation, and tends to run
as fast or faster than the equivalent handwritten JavaScript.


%package -n js-%{name}
Summary:        A programming language that transcompiles to JavaScript - core compiler

Provides:       %{name}-common = %{version}-%{release}
Obsoletes:      %{name}-common < 1.10.0-4
Obsoletes:      js-coffee-script < 1.10.0-20

Requires:       web-assets-filesystem
Requires:       %{name} == %{version}-%{release}


%description -n js-%{name}
This is the core compiler for the CoffeeScript language, suitable for use in
browsers or by other JavaScript implementations.

For the primary compiler and cake utility used in conjunction with Node.js,
install the 'coffeescript' package.


%package doc
Summary:        A programming language that transcompiles to JavaScript - documentation

%description doc
The documentation for the CoffeeScript programming language.


%prep
%autosetup -p1 -a2
mv node_modules_dev node_modules


%build
# CoffeeScript is written in itself, so we need to build it twice, to ensure
# we ship a version compiled by us
node ./bin/cake build:except-parser
node ./bin/cake build:parser

# Above updated bin/cake and bin/coffee, so now we rebuild with the updated
# compiler, similar to how gcc does things.
node ./bin/cake build:except-parser
node ./bin/cake build:parser

#build the minified coffeescript browser version and rename it appropriately
MINIFY=true node ./bin/cake build:browser
mv lib/coffeescript-browser-compiler-modern/coffeescript.js \
   lib/coffeescript-browser-compiler-modern/coffeescript.min.js

mv lib/coffeescript-browser-compiler-legacy/coffeescript.js \
   lib/coffeescript-browser-compiler-legacy/coffeescript.min.js

#also build the unminifed version
MINIFY=false node ./bin/cake build:browser


%install
# Install the coffeescript node module
mkdir -p %{buildroot}%{nodejs_sitelib}/%{name}
cp -a *.js bin lib package.json \
      %{buildroot}%{nodejs_sitelib}/%{name}/

# Install the cake and coffee binaries
mkdir -p %{buildroot}%{_bindir}
ln -srf %{buildroot}%{nodejs_sitelib}/%{name}/bin/cake \
        %{buildroot}%{_bindir}/cake
ln -srf %{buildroot}%{nodejs_sitelib}/%{name}/bin/coffee \
        %{buildroot}%{_bindir}/coffee

# Install the browser JavaScript for web-assets
mkdir -p %{buildroot}%{_jsdir}/%{name} \
         %{buildroot}%{_jsdir}/%{name}-legacy

# Move the browser compilers to the JavaScript directory
mv %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-modern/coffeescript.js \
   %{buildroot}%{_jsdir}/%{name}/coffeescript.js
ln -srf %{buildroot}%{_jsdir}/%{name}/coffeescript.js \
        %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-modern/coffeescript.js

mv %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-modern/coffeescript.min.js \
   %{buildroot}%{_jsdir}/%{name}/coffeescript.min.js
ln -srf %{buildroot}%{_jsdir}/%{name}/coffeescript.min.js \
        %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-modern/coffeescript.min.js

mv %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-legacy/coffeescript.js \
   %{buildroot}%{_jsdir}/%{name}-legacy/coffeescript.js
ln -srf %{buildroot}%{_jsdir}/%{name}-legacy/coffeescript.js \
        %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-legacy/coffeescript.js

mv %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-legacy/coffeescript.min.js \
   %{buildroot}%{_jsdir}/%{name}-legacy/coffeescript.min.js
ln -srf %{buildroot}%{_jsdir}/%{name}-legacy/coffeescript.min.js \
        %{buildroot}%{nodejs_sitelib}/%{name}/lib/coffeescript-browser-compiler-legacy/coffeescript.min.js


%check
%if %{with check}
node ./bin/cake doc:test
node ./bin/cake test:browser:node
node ./bin/cake test:integrations
%endif


%files
%{nodejs_sitelib}/%{name}
%{_bindir}/coffee
%{_bindir}/cake


%files -n js-%{name}
%doc README.md
%license LICENSE
%{_jsdir}/%{name}
%{_jsdir}/%{name}-legacy


%files doc
%doc docs


%changelog
* Wed Nov 02 2022 Stephen Gallagher <sgallagh@redhat.com> - 2.7.0-0.3
- Make js-coffeescript standalone
- Simplify %%prep

* Tue Nov 01 2022 Stephen Gallagher <sgallagh@redhat.com> - 2.7.0-0.2
- Fix missing BuildRequires on nodejs

* Mon Oct 31 2022 Stephen Gallagher <sgallagh@redhat.com> - 2.7.0-0.1
- Initial release of coffeescript 2.x