.PHONY: install test typecheck build publish patch minor major clean help

# ── Config ───────────────────────────────────────────────────────────────────

PACKAGE_NAME := $(shell node -p "require('./package.json').name" 2>/dev/null)
VERSION      := $(shell node -p "require('./package.json').version" 2>/dev/null)

# ── Default ──────────────────────────────────────────────────────────────────

help:
	@echo "$(PACKAGE_NAME) v$(VERSION)"
	@echo ""
	@echo "  make install     Install dependencies"
	@echo "  make test        Run tests"
	@echo "  make typecheck   Type-check TypeScript (no emit)"
	@echo "  make build       install + typecheck + test"
	@echo "  make patch       Bump patch version and publish"
	@echo "  make minor       Bump minor version and publish"
	@echo "  make major       Bump major version and publish"
	@echo "  make publish     Publish current version to npm"
	@echo "  make clean       Remove node_modules"

# ── Core tasks ───────────────────────────────────────────────────────────────

install:
	npm install

test:
	npm test

typecheck:
	npx tsc --noEmit --strict --skipLibCheck --moduleResolution bundler --module esnext \
		--target esnext --allowImportingTsExtensions \
		extensions/index.ts $(wildcard src/*.ts)

build: install typecheck test
	@echo "✓ Build complete — $(PACKAGE_NAME) v$(VERSION)"

# ── Publishing ───────────────────────────────────────────────────────────────

publish: clean build
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(VERSION)"

patch: clean build
	npm version patch
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

minor: clean build
	npm version minor
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

major: clean build
	npm version major
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

# ── Cleanup ──────────────────────────────────────────────────────────────────

clean:
	rm -rf node_modules
