From eed1e1cf27ec883d5f880fbd3aef6062cb11c9df Mon Sep 17 00:00:00 2001
From: Alex Dima <alexdima@microsoft.com>
Date: Sat, 5 Feb 2022 00:23:00 +0100
Subject: [PATCH] Fix script for esbuild sample which cannot import `.ts` files

---
 build/fs.ts                          |  7 +---
 build/release.ts                     |  3 +-
 samples/browser-esm-esbuild/build.js | 48 ++++++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/build/fs.ts b/build/fs.ts
index 99e4fbfc..a076fb76 100644
--- a/build/fs.ts
+++ b/build/fs.ts
@@ -56,12 +56,7 @@ export function removeDir(_dirPath: string, keep?: (filename: string) => boolean
 	rmDir(dirPath, _dirPath);
 	console.log(`Deleted ${_dirPath}`);
 
-	/**
-	 * @param {string} dirPath
-	 * @param {string} relativeDirPath
-	 * @returns {boolean}
-	 */
-	function rmDir(dirPath, relativeDirPath) {
+	function rmDir(dirPath: string, relativeDirPath: string): boolean {
 		let keepsFiles = false;
 		const entries = fs.readdirSync(dirPath);
 		for (const entry of entries) {
diff --git a/build/release.ts b/build/release.ts
index dbcd9bf3..f9c09624 100644
--- a/build/release.ts
+++ b/build/release.ts
@@ -64,9 +64,8 @@ generateMetadata();
 
 /**
  * Release to `dev` or `min`.
- * @param {'dev'|'min'} type
  */
-function AMD_releaseOne(type) {
+function AMD_releaseOne(type: 'dev' | 'min') {
 	const coreFiles = readFiles(`node_modules/monaco-editor-core/${type}/**/*`, {
 		base: `node_modules/monaco-editor-core/${type}`
 	});
diff --git a/samples/browser-esm-esbuild/build.js b/samples/browser-esm-esbuild/build.js
index 85213842..c4b95a97 100644
--- a/samples/browser-esm-esbuild/build.js
+++ b/samples/browser-esm-esbuild/build.js
@@ -7,9 +7,9 @@
 
 const esbuild = require('esbuild');
 const path = require('path');
-const { removeDir } = require('../../build/fs');
+const fs = require('fs');
 
-removeDir('samples/browser-esm-esbuild/dist', (entry) => /index.html$/.test(entry));
+removeDir('dist', (entry) => /index.html$/.test(entry));
 
 const workerEntryPoints = [
 	'vs/language/json/json.worker.js',
@@ -50,3 +50,47 @@ function build(opts) {
 		}
 	});
 }
+
+/**
+ * Remove a directory and all its contents.
+ * @param {string} _dirPath
+ * @param {(filename: string) => boolean} [keep]
+ */
+function removeDir(_dirPath, keep) {
+	if (typeof keep === 'undefined') {
+		keep = () => false;
+	}
+	const dirPath = path.join(__dirname, _dirPath);
+	if (!fs.existsSync(dirPath)) {
+		return;
+	}
+	rmDir(dirPath, _dirPath);
+	console.log(`Deleted ${_dirPath}`);
+
+	/**
+	 * @param {string} dirPath
+	 * @param {string} relativeDirPath
+	 * @returns {boolean}
+	 */
+	function rmDir(dirPath, relativeDirPath) {
+		let keepsFiles = false;
+		const entries = fs.readdirSync(dirPath);
+		for (const entry of entries) {
+			const filePath = path.join(dirPath, entry);
+			const relativeFilePath = path.join(relativeDirPath, entry);
+			if (keep(relativeFilePath)) {
+				keepsFiles = true;
+				continue;
+			}
+			if (fs.statSync(filePath).isFile()) {
+				fs.unlinkSync(filePath);
+			} else {
+				keepsFiles = rmDir(filePath, relativeFilePath) || keepsFiles;
+			}
+		}
+		if (!keepsFiles) {
+			fs.rmdirSync(dirPath);
+		}
+		return keepsFiles;
+	}
+}