From a8878c8cfe87c8ebb926f7dbb8ed7b33db5615e5 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 20 Dec 2021 15:41:33 +0100 Subject: [PATCH] Fix #2843 --- build/build.js | 3 +- build/fs.js | 98 ++++++++++++++++++++++++++++ build/release.js | 3 +- build/releaseMetadata.js | 3 +- build/simpleserver.js | 3 +- build/utils.js | 73 +-------------------- build/website.js | 3 +- samples/browser-esm-esbuild/build.js | 11 +++- 8 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 build/fs.js diff --git a/build/build.js b/build/build.js index a59d5fa5..045e6a18 100644 --- a/build/build.js +++ b/build/build.js @@ -6,7 +6,8 @@ //@ts-check const glob = require('glob'); -const { copyFile, removeDir, tsc, dts, buildESM, buildAMD } = require('../build/utils'); +const { tsc, dts, buildESM, buildAMD } = require('../build/utils'); +const { copyFile, removeDir } = require('../build/fs'); removeDir(`out`); diff --git a/build/fs.js b/build/fs.js new file mode 100644 index 00000000..b0cdcebe --- /dev/null +++ b/build/fs.js @@ -0,0 +1,98 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +//@ts-check + +const fs = require('fs'); +const path = require('path'); + +const REPO_ROOT = path.join(__dirname, '../'); + +const existingDirCache = new Set(); +/** + * @param {string} dirname + */ +function ensureDir(dirname) { + /** @type {string[]} */ + const dirs = []; + + while (dirname.length > REPO_ROOT.length) { + dirs.push(dirname); + dirname = path.dirname(dirname); + } + dirs.reverse(); + dirs.forEach((dir) => { + if (!existingDirCache.has(dir)) { + try { + fs.mkdirSync(dir); + } catch (err) {} + existingDirCache.add(dir); + } + }); +} +exports.ensureDir = ensureDir; + +/** + * Copy a file. + * + * @param {string} _source + * @param {string} _destination + */ +function copyFile(_source, _destination) { + const source = path.join(REPO_ROOT, _source); + const destination = path.join(REPO_ROOT, _destination); + + ensureDir(path.dirname(destination)); + fs.writeFileSync(destination, fs.readFileSync(source)); + + console.log(`Copied ${_source} to ${_destination}`); +} +exports.copyFile = copyFile; + +/** + * 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(REPO_ROOT, _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; + } +} +exports.removeDir = removeDir; diff --git a/build/release.js b/build/release.js index 6abee52e..5761bd2b 100644 --- a/build/release.js +++ b/build/release.js @@ -15,7 +15,8 @@ const path = require('path'); const fs = require('fs'); -const { REPO_ROOT, removeDir, readFiles, writeFiles } = require('../build/utils'); +const { REPO_ROOT, readFiles, writeFiles } = require('../build/utils'); +const { removeDir } = require('../build/fs'); const ts = require('typescript'); /**@type { IMetadata } */ const metadata = require('../metadata.js'); diff --git a/build/releaseMetadata.js b/build/releaseMetadata.js index 37598a55..555d251d 100644 --- a/build/releaseMetadata.js +++ b/build/releaseMetadata.js @@ -8,7 +8,8 @@ const glob = require('glob'); const path = require('path'); const fs = require('fs'); -const { REPO_ROOT, prettier, ensureDir } = require('./utils'); +const { REPO_ROOT } = require('./utils'); +const { ensureDir } = require('./fs'); const customFeatureLabels = { 'vs/editor/browser/controller/coreCommands': 'coreCommands', diff --git a/build/simpleserver.js b/build/simpleserver.js index 5d6cfe02..250ea8b4 100644 --- a/build/simpleserver.js +++ b/build/simpleserver.js @@ -9,7 +9,8 @@ const fs = require('fs'); const path = require('path'); const http = require('http'); const yaserver = require('yaserver'); -const { REPO_ROOT, ensureDir } = require('./utils'); +const { REPO_ROOT } = require('./utils'); +const { ensureDir } = require('./fs'); const WEBSITE_GENERATED_PATH = path.join(REPO_ROOT, 'website/playground/new-samples'); diff --git a/build/utils.js b/build/utils.js index 052b65eb..932956a4 100644 --- a/build/utils.js +++ b/build/utils.js @@ -12,82 +12,11 @@ const esbuild = require('esbuild'); /** @type {any} */ const alias = require('esbuild-plugin-alias'); const glob = require('glob'); +const { ensureDir } = require('./fs'); const REPO_ROOT = path.join(__dirname, '../'); exports.REPO_ROOT = REPO_ROOT; -const existingDirCache = new Set(); -/** - * @param {string} dirname - */ -function ensureDir(dirname) { - /** @type {string[]} */ - const dirs = []; - - while (dirname.length > REPO_ROOT.length) { - dirs.push(dirname); - dirname = path.dirname(dirname); - } - dirs.reverse(); - dirs.forEach((dir) => { - if (!existingDirCache.has(dir)) { - try { - fs.mkdirSync(dir); - } catch (err) {} - existingDirCache.add(dir); - } - }); -} -exports.ensureDir = ensureDir; - -/** - * Copy a file. - * - * @param {string} _source - * @param {string} _destination - */ -function copyFile(_source, _destination) { - const source = path.join(REPO_ROOT, _source); - const destination = path.join(REPO_ROOT, _destination); - - ensureDir(path.dirname(destination)); - fs.writeFileSync(destination, fs.readFileSync(source)); - - console.log(`Copied ${_source} to ${_destination}`); -} -exports.copyFile = copyFile; - -/** - * Remove a directory and all its contents. - * - * @param {string} _dirPath - */ -function removeDir(_dirPath) { - const dirPath = path.join(REPO_ROOT, _dirPath); - if (!fs.existsSync(dirPath)) { - return; - } - rmDir(dirPath); - console.log(`Deleted ${_dirPath}`); - - /** - * @param {string} dirPath - */ - function rmDir(dirPath) { - const entries = fs.readdirSync(dirPath); - for (const entry of entries) { - const filePath = path.join(dirPath, entry); - if (fs.statSync(filePath).isFile()) { - fs.unlinkSync(filePath); - } else { - rmDir(filePath); - } - } - fs.rmdirSync(dirPath); - } -} -exports.removeDir = removeDir; - /** * Launch the typescript compiler synchronously over a project. * diff --git a/build/website.js b/build/website.js index 5b92cba5..48c343ce 100644 --- a/build/website.js +++ b/build/website.js @@ -12,7 +12,8 @@ const path = require('path'); const fs = require('fs'); const cp = require('child_process'); const CleanCSS = require('clean-css'); -const { REPO_ROOT, removeDir, readFiles, writeFiles } = require('./utils'); +const { REPO_ROOT, readFiles, writeFiles } = require('./utils'); +const { removeDir } = require('./fs'); /** @type {string} */ const MONACO_EDITOR_VERSION = (() => { diff --git a/samples/browser-esm-esbuild/build.js b/samples/browser-esm-esbuild/build.js index 14bad23a..85213842 100644 --- a/samples/browser-esm-esbuild/build.js +++ b/samples/browser-esm-esbuild/build.js @@ -1,8 +1,15 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +//@ts-check + const esbuild = require('esbuild'); const path = require('path'); -const { removeDir } = require('../../build/utils'); +const { removeDir } = require('../../build/fs'); -removeDir(path.join(__dirname, 'dist')); +removeDir('samples/browser-esm-esbuild/dist', (entry) => /index.html$/.test(entry)); const workerEntryPoints = [ 'vs/language/json/json.worker.js',