// @ts-check

const fs = require("fs")
const path = require("path")
const spawnSync = require("child_process").spawnSync
const chalk = require("chalk").default

function sh(command, canFail = false) {
  console.log("$ " + command)
  const task = spawnSync(command, { shell: true, stdio: "inherit" })
  if (!canFail && task.status != 0) {
    console.log(String(task.stdout))
    console.error(chalk.red(String(task.stderr)))
    throw new Error("[!] " + command)
  }
}

function publishPodspec(podspec) {
  const podspecFilename = path.basename(podspec)
  const podspecDir = path.dirname(path.resolve(podspec))
  const packagePath = path.join(podspecDir, "package.json")

  const name = path.basename(podspec, ".podspec")
  const version = JSON.parse(fs.readFileSync(packagePath).toString()).version

  const specRepo = path.join(process.env.HOME, ".cocoapods/repos/artsy")
  const relativeSpecPath = path.join(name, version, name + ".podspec.json")
  const specPath = path.join(specRepo, relativeSpecPath)

  if (!fs.existsSync(specPath)) {
    console.log("=> Pushing " + name + " podspec to spec-repo.")
    sh("mkdir -p " + path.dirname(specPath))
    sh("cd " + podspecDir + ' && INCLUDE_METADATA="true" pod ipc spec ' + podspecFilename + " > " + specPath)
    sh(
      "cd " +
        specRepo +
        " && git pull " +
        " && git add " +
        relativeSpecPath +
        ' && git commit -m "' +
        name +
        " " +
        version +
        '" && git push'
    )
  }
}

console.log(chalk.green("=> Validating your tools."))
sh("bundle --version")
sh("npm --version")

console.log(chalk.green("=> Creating release bundle."))
sh("npm run relay")
sh("npm run bundle")
sh("cd Example && bundle exec pod install")
sh('git add . && git commit -m "[Pod] Update release artefacts."', true)

sh("yarn run auto changelog --very-verbose")

console.log(chalk.green("=> Creating version bump commit and tag."))
sh(`npm version $(npx auto version) --message '%s [skip ci]'`)

sh("git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)")
sh("git push --tags")

sh("yarn run auto release --very-verbose")

console.log(chalk.green("=> Pushing Podspec"))
publishPodspec("Emission.podspec")

console.log(chalk.green("=> Updating Metaphysics and eigen"))
sh("npm run update-metaphysics-and-eigen")

@@ -1584,24 +1584,121 @@
 -version%22)%0A%0A
+console.log(chalk.green(%22=%3E Updating changelog.%22))%0Ash(%22yarn run auto changelog --very-verbose%22)%0A%0A
 console.log(
@@ -1893,62 +1893,26 @@
 cts.
-%22', true)%0A%0Ash(%22yarn run auto changelog --very-verbose%22
+ %5Bskip ci%5D%22', true
 )%0A%0Ac

update changelog before updating release artefacts