const { execSync } = require('child_process');
const args = require('args');
const { version: PKGVersion } = require('../package.json');

async function sentryRelease() {
  args
    .examples([
      {
        usage: `node ./scripts/sentry-release.js ${PKGVersion}`,
        description: 'Let Sentry figure out the commits for the release',
      },
      {
        usage: `node ./scripts/sentry-release.js ${PKGVersion} ac8264c 474721f`,
        description: 'Explictly tell Sentry the commits for the release',
      },
    ])
    .parse(process.argv, {
      version: false,
      name: 'Sentry Releaser',
    });

  const [version, last, current] = args.sub;

  console.log({ version, last, current });

  process.exit(0);

  const { SENTRY_ORG, SENTRY_AUTH_TOKEN } = process.env;

  if (!SENTRY_ORG) {
    throw new Error('A `SENTRY_ORG` environment variable is required');
  }

  if (!SENTRY_AUTH_TOKEN) {
    throw new Error('A `SENTRY_AUTH_TOKEN` environment variable is required');
  }

  try {
    execSync(`sentry-cli releases new -p blog ${version}`);
    console.log(`Release created for ${version}!`);
  } catch (error) {
    throw new Error(`An error occured creating that release :(`, error);
  }

  if (!last || !current) {
    console.log(
      `no commit shas were provided, letting sentry figure that out...`
    );
    try {
      execSync(`sentry-cli releases set-commits ${version} --auto`);
    } catch (error) {
      throw new Error(
        `An error occured setting the commits for that release :(`,
        error
      );
    }
  } else {
    try {
      execSync(
        `sentry-cli releases set-commits ${version} --commit "mcansh/blog@${last}..${current}"`
      );
    } catch (error) {
      throw new Error(
        `An error occured setting the commits for that release :(`,
        error
      );
    }
  }

  execSync(`sentry-cli releases deploys ${version} new -e production`);
}

sentryRelease();

@@ -661,72 +661,8 @@
 b;%0A%0A
-  console.log(%7B version, last, current %7D);%0A%0A  process.exit(0);%0A%0A
   co
@@ -1776,16 +1776,86 @@
  %7D%0A  %7D%0A%0A
+  const env = version.includes('canary') ? 'staging' : 'production';%0A%0A
   execSy
@@ -1904,26 +1904,22 @@
  new -e 
-production
+$%7Benv%7D
 %60);%0A%7D%0A%0As

set env depending on version