#!/usr/bin/env node
import { execFileSync } from 'node:child_process';
import { createRequire } from 'node:module';
import { cpSync, rmSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const packageDir = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const typescriptPackageJson = require.resolve('typescript/package.json', { paths: [packageDir] });
const tscBin = resolve(dirname(typescriptPackageJson), 'bin/tsc');

rmSync(resolve(packageDir, 'dist'), { recursive: true, force: true });
execFileSync(process.execPath, [tscBin, '-p', resolve(packageDir, 'tsconfig.json')], {
  cwd: packageDir,
  stdio: 'inherit',
});
execFileSync(process.execPath, [resolve(packageDir, 'fix-esm-imports.mjs')], {
  cwd: packageDir,
  stdio: 'inherit',
});
cpSync(resolve(packageDir, 'src/theme/base.css'), resolve(packageDir, 'dist/theme/base.css'));
cpSync(resolve(packageDir, 'src/theme/tokens.css'), resolve(packageDir, 'dist/theme/tokens.css'));
