/* eslint-disable @typescript-eslint/no-var-requires */

require('dotenv').config();

// const rm = require('rimraf');
const esBuild = require('esbuild');

/** @type BuildOptions */
let buildSettingsMain = {
  entryPoints: ['src/index.ts'],
  bundle: true,
  target: 'node16',
  platform: 'node',
  treeShaking: true,
  outfile: 'lib/index.js',
  sourcemap: true,
  format: 'cjs',

  external: ['pg-native'],
};

if (process.env.NODE_ENV == 'development' && process.env.WATCH == 'true') {
  buildSettingsMain.watch = {
    onRebuild(error, result) {
      if (error) console.error('watch build failed:', error);
      else console.log('watch build succeeded:', result);
    },
  };
}

// rm.sync('./lib/**');

Promise.all([esBuild.build(buildSettingsMain)]).catch(() => process.exit(1));
