Ensure ESBuild versions are in sync in PRESUBMIT

We have ESBuild as a dep in two places:

1. `DEPS`, managed by gclient
2. `manage_node_deps.py`, managed by npm

These versions need to match, otherwise any builds that use `esbuild`
will fail in Chrome land. This CL introduces a script and PRESUBMIT
check to ensure that the two versions are in sync.

Bug: none
Change-Id: I7ea9172ae386e4e21e1ac82c208d171b65b29e22
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4067299
Reviewed-by: Liviu Rau <liviurau@google.com>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
diff --git a/scripts/check_esbuild_versions.js b/scripts/check_esbuild_versions.js
new file mode 100644
index 0000000..69e360c
--- /dev/null
+++ b/scripts/check_esbuild_versions.js
@@ -0,0 +1,51 @@
+#!/usr/bin/env node
+
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+'use strict';
+
+const path = require('path');
+const fs = require('fs');
+
+const {
+  devtoolsRootPath,
+} = require('./devtools_paths.js');
+
+function bail(message) {
+  console.error(message);
+  process.exit(1);
+}
+
+function findVersionFromDepsFile() {
+  const filePath = path.join(devtoolsRootPath(), 'DEPS');
+  const contents = fs.readFileSync(filePath, 'utf8').split('\n');
+  const esbuildPackageLine = contents.findIndex(line => line.match(/infra\/3pp\/tools\/esbuild/));
+  if (esbuildPackageLine === -1) {
+    bail('Could not find ESBuild within DEPS file.');
+  }
+  const esbuildVersionLine = contents[esbuildPackageLine + 1];
+  const result = /@(\d{1,2}\.\d{1,2}\.\d{1,2})/.exec(esbuildVersionLine)?.[1];
+  if (!result) {
+    bail('Could not parse out ESBuild version from DEPS');
+  }
+  return result;
+}
+
+function findVersionFromNodeDepsFile() {
+  const filePath = path.join(devtoolsRootPath(), 'scripts', 'deps', 'manage_node_deps.py');
+  const contents = fs.readFileSync(filePath, 'utf8');
+  const result = /"esbuild": "([0-9\.]+)"/.exec(contents)?.[1];
+  if (!result) {
+    bail('Could not parse out ESBuild version from manage_node_deps.py');
+  }
+  return result;
+}
+
+const nodeDepsVersion = findVersionFromNodeDepsFile();
+const depsVersion = findVersionFromDepsFile();
+if (nodeDepsVersion !== depsVersion) {
+  bail(`Found mismatching esbuild versions in DEPS vs manage_node_deps:
+    manage_node_deps.py: ${nodeDepsVersion}
+    DEPS:                ${depsVersion}\n`);
+}