From 54fba9ddff1295557e9857c19c7f6ab21b41b9a1 Mon Sep 17 00:00:00 2001 From: Paul Berberian Date: Wed, 31 Jul 2024 13:13:36 +0200 Subject: [PATCH] Remove reliance on the rimraf dependency We often have to work on debugging applications. In most of those cases, we call `npm-link` or `yarn-link` to link our local version of the RxPlayer to a given application. Sadly it turns out that some of those applications have complex and poorly-configured package installation and build steps, which leads to many errors as we try to produce a build. An issue I was having right now, is linked to a peculiar way packages can be installed in one application at Canal+, leading in some cases to issues with how the `rimraf` package is imported. I grew tired of this issue. Considering that the `rimraf` dependency for our simple usage seems to be very easy to replace with a few line of Node.js (fs.rm fills what we want to do and seems to even be relied on by `rimraf` on the simple cases we're exploiting), I decided to do just that - so there's less possibility for dependency-importing issues to occur. --- package-lock.json | 1 - package.json | 5 ++--- scripts/generate_build.mjs | 12 ++---------- scripts/remove_dir.mjs | 27 +++++++++++++++++++++++++++ tests/performance/run.mjs | 12 ++---------- 5 files changed, 33 insertions(+), 24 deletions(-) create mode 100755 scripts/remove_dir.mjs diff --git a/package-lock.json b/package-lock.json index f7a63df358..1bcfd6c63d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,6 @@ "react": "18.2.0", "react-dom": "18.2.0", "regenerator-runtime": "0.14.1", - "rimraf": "5.0.5", "semver": "7.5.4", "typescript": "5.3.3", "vitest": "^1.6.0", diff --git a/package.json b/package.json index 1f1895bf0e..eeaee82444 100644 --- a/package.json +++ b/package.json @@ -154,11 +154,11 @@ "check:all": "npm run check:types && npm run lint && npm run lint:demo && npm run lint:tests && npm run test:unit && npm run test:integration && npm run test:memory && node -r esm ./scripts/check_nodejs_import_compatibility.js", "check:demo": "npm run check:demo:types && npm run lint:demo", "check:demo:types": "tsc --noEmit --project demo/", - "clean:build": "rimraf dist", + "clean:build": "scripts/remove_dir.mjs dist", "check:types": "tsc --noEmit --project .", "check:types:unit_tests": "tsc --noEmit --project ./tsconfig.unit-tests.json", "check:types:watch": "tsc --noEmit --watch --project .", - "clean:wasm": "rimraf dist/mpd-parser.wasm && rimraf ./src/parsers/manifest/dash/wasm-parser/target", + "clean:wasm": "scripts/remove_dir.mjs dist/mpd-parser.wasm && scripts/remove_dir.mjs ./src/parsers/manifest/dash/wasm-parser/target", "demo": "node ./scripts/build_demo.mjs --production-mode", "demo:min": "node ./scripts/build_demo.mjs --production-mode --minify", "demo:watch": "node ./scripts/build_demo.mjs --watch --production-mode", @@ -215,7 +215,6 @@ "react": "18.2.0", "react-dom": "18.2.0", "regenerator-runtime": "0.14.1", - "rimraf": "5.0.5", "semver": "7.5.4", "typescript": "5.3.3", "vitest": "^1.6.0", diff --git a/scripts/generate_build.mjs b/scripts/generate_build.mjs index 968dbdd263..ba528662c2 100755 --- a/scripts/generate_build.mjs +++ b/scripts/generate_build.mjs @@ -22,9 +22,9 @@ import { spawn } from "child_process"; import * as fs from "fs"; import * as path from "path"; import { fileURLToPath, pathToFileURL } from "url"; -import { rimraf } from "rimraf"; import generateEmbeds from "./generate_embeds.mjs"; import runBundler from "./run_bundler.mjs"; +import removeDir from "./remove_dir.mjs"; const currentDirectory = path.dirname(fileURLToPath(import.meta.url)); @@ -112,7 +112,7 @@ async function removePreviousBuildArtefacts() { await Promise.all( BUILD_ARTEFACTS_TO_REMOVE.map((name) => { const relativePath = path.join(ROOT_DIR, name); - return removeFile(relativePath); + return removeDir(relativePath); }), ); } @@ -146,14 +146,6 @@ async function compile(devMode) { ]); } -/** - * @param {string} fileName - * @returns {Promise} - */ -function removeFile(fileName) { - return rimraf(fileName); -} - /** * @param {string} command * @param {Array.} args diff --git a/scripts/remove_dir.mjs b/scripts/remove_dir.mjs new file mode 100755 index 0000000000..4ac9d84d6a --- /dev/null +++ b/scripts/remove_dir.mjs @@ -0,0 +1,27 @@ +#!/usr/bin/env node +import { pathToFileURL } from "url"; +import * as fs from "fs"; + +// If true, this script is called directly +if (import.meta.url === pathToFileURL(process.argv[1]).href) { + for (const dir of process.argv.slice(2)) { + removeDir(dir).catch((err) => { + console.error(`ERROR: Failed to remove "${dir}"`, err); + }); + } +} + +/** + * @param {string} fileName + * @returns {Promise} + */ +export default function removeDir(fileName) { + return new Promise((res, rej) => { + fs.rm(fileName, { recursive: true, force: true }, (err) => { + if (err) { + rej(err); + } + res(); + }); + }); +} diff --git a/tests/performance/run.mjs b/tests/performance/run.mjs index 4216333638..db74c91931 100644 --- a/tests/performance/run.mjs +++ b/tests/performance/run.mjs @@ -6,10 +6,10 @@ import esbuild from "esbuild"; import * as fs from "fs/promises"; import { createServer } from "http"; import * as path from "path"; -import { rimraf } from "rimraf"; import { fileURLToPath } from "url"; import launchStaticServer from "../../scripts/launch_static_server.mjs"; import getHumanReadableHours from "../../scripts/utils/get_human_readable_hours.mjs"; +import removeDir from "../../scripts/remove_dir.mjs"; import { createContentServer } from "../contents/server.mjs"; const currentDirectory = path.dirname(fileURLToPath(import.meta.url)); @@ -214,7 +214,7 @@ async function prepareLastRxPlayerTests() { * @returns {Promise} */ async function linkCurrentRxPlayer() { - await removeFile(path.join(currentDirectory, "node_modules")); + await removeDir(path.join(currentDirectory, "node_modules")); await fs.mkdir(path.join(currentDirectory, "node_modules")); await spawnProc( "npm run build", @@ -770,14 +770,6 @@ function createBundle(options) { }); } -/** - * @param {string} fileName - * @returns {Promise} - */ -function removeFile(fileName) { - return rimraf(fileName); -} - /** * @param {string} command * @param {Array.} args