# Clean up dist folder
rm -rf dist && \
# Prepare new dist folder
mkdir -p dist/esm dist/cjs && \
# Build ESM modules alternative
tsc --project tsconfig.release-esm.json && \
# Build CJS alternative
tsc --project tsconfig.release-cjs.json && \
# Build types
tsc --project tsconfig.release-types.json && \
# Build shared types used for internal Buttonize purposes
tsup cli/api/_ws.ts -d dist/types --tsconfig=tsconfig.release-types.json --dts-only && \
# Didn't found any other simple way how to support both ESM and CJS __dirname vs. import.meta.url
pnpm replace-in-file "/__dirname/g" "path.dirname(process.platform === 'win32' ? import.meta.url.replace('file:///', '') : import.meta.url.replace('file://', ''))" "dist/esm/cdk/**/*.js,dist/esm/library/**/*.js"  --isRegex && \
# Remove husky in prepare script from package.json and copy it to dist
# because contents of dist contain all files that will be published to NPM
sh ./scripts/package-json-dist.sh && \
# This hack allows cjs/esm to co-exist in parallel
echo '{"type": "commonjs"}' > dist/cjs/package.json && \
echo '{"type": "module"}' > dist/esm/package.json && \
# This enables ESM version of lambda handler to be running okay
echo '{"type": "commonjs"}' > dist/cjs/cdk/custom-resources/handler/package.json && \
echo '{"type": "module"}' > dist/esm/cdk/custom-resources/handler/package.json && \
# Copy remaining files important for the final package 
cp LICENSE dist && \
cp README.md dist
