#!/usr/bin/make -f
# -*- makefile -*-

# TypeScript 6 refuses to ignore tsconfig.json silently when files are named on
# the command line (TS5112); the flag does not exist in TypeScript 5.
TSC_IGNORE_CONFIG := $(shell dpkg --compare-versions "$$(dpkg-query -W -f='$${Version}' node-typescript)" ge 6 && echo --ignoreConfig)

%:
	dh $@

execute_after_dh_auto_build:
	# Step 1: build a temporary @rollup/plugin-typescript
	# 1.1 : ES
	cd packages/typescript && \
		mkdir dist && \
		NODE_PATH=node_modules tsc $(TSC_IGNORE_CONFIG) --types node --module esnext --moduleResolution bundler --esModuleInterop --target es2015 src/index.ts
	cd packages/typescript/src && \
		tar cf - `find . -name '*.js' -type f` |(cd ../dist; tar xf -) && \
		find . -name '*.js' -type f -delete
	mkdir -p node_modules/@rollup/plugin-typescript/dist
	mv packages/typescript/dist node_modules/@rollup/plugin-typescript/dist/es
	for f in `find node_modules/@rollup/plugin-typescript/dist/es -name '*.js'`; do \
		ln -s `basename $$f` $${f%%.js}; \
	done
	## 1.2 : CJS
	#cd packages/typescript && \
	#	mkdir dist && \
	#	NODE_PATH=node_modules tsc --module commonjs --moduleResolution node --esModuleInterop src/index.ts
	#cd packages/typescript/src && \
	#	tar cf - `find . -name '*.js' -type f` |(cd ../dist; tar xf -) && \
	#	find . -name '*.js' -type f -delete
	#mkdir -p node_modules/@rollup/plugin-typescript/dist
	#mv packages/typescript/dist node_modules/@rollup/plugin-typescript/dist/cjs
	perl -pe 's/("name")/"type":"module",$$1/' <packages/typescript/package.json \
		> node_modules/@rollup/plugin-typescript/package.json
	# Step 2: upstream build
	cd packages/typescript && rollup -c
	rm -rf node_modules/@rollup/plugin-typescript/

override_dh_installchangelogs:
	dh_installchangelogs packages/typescript/CHANGELOG.md

execute_after_dh_clean:
	rm -f packages/typescript/test/fixtures/incremental-single/tsconfig.tsbuildinfo
