Skip to content

Commit

Permalink
Remove the rimraf dependency in favor of the built-in Node.js `fs.r…
Browse files Browse the repository at this point in the history
…mSync`

In Node.js 14.14.0 the `fs.rmSync` function was added that removes files
and directories. The `recursive` option is used to remove directories
and their contents, making it a drop-in replacement for the `rimraf`
dependency we use.

Given that PDF.js now requires Node.js 18+ we can be sure that this
option is available, so we can safely remove `rimraf` and reduce the
number of project dependencies.

Co-authored-by: Wojciech Maj <[email protected]>
  • Loading branch information
timvandermeij and wojtekmaj committed May 16, 2024
1 parent 0603d1a commit fad1432
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
66 changes: 39 additions & 27 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import postcssNesting from "postcss-nesting";
import { preprocess } from "./external/builder/builder.mjs";
import rename from "gulp-rename";
import replace from "gulp-replace";
import rimraf from "rimraf";
import stream from "stream";
import streamqueue from "streamqueue";
import TerserPlugin from "terser-webpack-plugin";
Expand Down Expand Up @@ -918,7 +917,7 @@ gulp.task("locale", function () {
console.log();
console.log("### Building localization files");

rimraf.sync(VIEWER_LOCALE_OUTPUT);
fs.rmSync(VIEWER_LOCALE_OUTPUT, { recursive: true, force: true });
fs.mkdirSync(VIEWER_LOCALE_OUTPUT, { recursive: true });

const subfolders = fs.readdirSync(L10N_DIR);
Expand Down Expand Up @@ -1020,7 +1019,7 @@ function preprocessHTML(source, defines) {
}

function buildGeneric(defines, dir) {
rimraf.sync(dir);
fs.rmSync(dir, { recursive: true, force: true });

return merge([
createMainBundle(defines).pipe(gulp.dest(dir + "build")),
Expand Down Expand Up @@ -1115,7 +1114,7 @@ gulp.task(
);

function buildComponents(defines, dir) {
rimraf.sync(dir);
fs.rmSync(dir, { recursive: true, force: true });

const COMPONENTS_IMAGES = [
"web/images/annotation-*.svg",
Expand Down Expand Up @@ -1201,7 +1200,7 @@ gulp.task(
);

function buildMinified(defines, dir) {
rimraf.sync(dir);
fs.rmSync(dir, { recursive: true, force: true });

return merge([
createMainBundle(defines).pipe(gulp.dest(dir + "build")),
Expand Down Expand Up @@ -1332,7 +1331,7 @@ gulp.task(
};

// Clear out everything in the firefox extension build directory
rimraf.sync(MOZCENTRAL_DIR);
fs.rmSync(MOZCENTRAL_DIR, { recursive: true, force: true });

return merge([
createMainBundle(defines).pipe(
Expand Down Expand Up @@ -1430,7 +1429,7 @@ gulp.task(
];

// Clear out everything in the chrome extension build directory
rimraf.sync(CHROME_BUILD_DIR);
fs.rmSync(CHROME_BUILD_DIR, { recursive: true, force: true });

const version = getVersionJSON().version;

Expand Down Expand Up @@ -1503,17 +1502,16 @@ gulp.task("jsdoc", function (done) {

const JSDOC_FILES = ["src/display/api.js"];

rimraf(JSDOC_BUILD_DIR, function () {
fs.mkdirSync(JSDOC_BUILD_DIR, { recursive: true });
fs.rmSync(JSDOC_BUILD_DIR, { recursive: true, force: true });
fs.mkdirSync(JSDOC_BUILD_DIR, { recursive: true });

const command =
'"node_modules/.bin/jsdoc" -d ' +
JSDOC_BUILD_DIR +
" " +
JSDOC_FILES.join(" ");
const command =
'"node_modules/.bin/jsdoc" -d ' +
JSDOC_BUILD_DIR +
" " +
JSDOC_FILES.join(" ");

exec(command, done);
});
exec(command, done);
});

gulp.task("types", function (done) {
Expand Down Expand Up @@ -2013,7 +2011,7 @@ gulp.task(
const defines = { ...DEFINES, GENERIC: true, TESTING: true };
const sandboxDir = BUILD_DIR + "dev-sandbox/";

rimraf.sync(sandboxDir);
fs.rmSync(sandboxDir, { recursive: true, force: true });

return createSandboxBundle(defines, {
disableVersionInfo: true,
Expand Down Expand Up @@ -2059,7 +2057,8 @@ gulp.task("clean", function (done) {
console.log();
console.log("### Cleaning up project builds");

rimraf(BUILD_DIR, done);
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
done();
});

gulp.task("importl10n", async function () {
Expand All @@ -2078,7 +2077,7 @@ function ghPagesPrepare() {
console.log();
console.log("### Creating web site");

rimraf.sync(GH_PAGES_DIR);
fs.rmSync(GH_PAGES_DIR, { recursive: true, force: true });

return merge([
gulp
Expand Down Expand Up @@ -2192,13 +2191,20 @@ gulp.task(
console.log();
console.log("### Cloning baseline distribution");

rimraf.sync(DIST_DIR);
fs.rmSync(DIST_DIR, { recursive: true, force: true });
fs.mkdirSync(DIST_DIR, { recursive: true });
safeSpawnSync("git", ["clone", "--depth", "1", DIST_REPO_URL, DIST_DIR]);

console.log();
console.log("### Overwriting all files");
rimraf.sync(path.join(DIST_DIR, "*"));

// Remove all files/folders, except for `.git` because it needs to be a
// valid Git repository for the Git commands in the `dist` target to work.
for (const entry of fs.readdirSync(DIST_DIR)) {
if (entry !== ".git") {
fs.rmSync(DIST_DIR + entry, { recursive: true, force: true });
}
}

return merge([
packageJson().pipe(gulp.dest(DIST_DIR)),
Expand Down Expand Up @@ -2330,7 +2336,7 @@ gulp.task(
console.log("### Creating mozcentral baseline environment");

// Create a mozcentral build.
rimraf.sync(BASELINE_DIR + BUILD_DIR);
fs.rmSync(BASELINE_DIR + BUILD_DIR, { recursive: true, force: true });

const workingDirectory = path.resolve(process.cwd(), BASELINE_DIR);
safeSpawnSync("gulp", ["mozcentral"], {
Expand All @@ -2340,7 +2346,7 @@ gulp.task(
});

// Copy the mozcentral build to the mozcentral baseline directory.
rimraf.sync(MOZCENTRAL_BASELINE_DIR);
fs.rmSync(MOZCENTRAL_BASELINE_DIR, { recursive: true, force: true });
fs.mkdirSync(MOZCENTRAL_BASELINE_DIR, { recursive: true });

gulp
Expand Down Expand Up @@ -2369,10 +2375,16 @@ gulp.task(

// Create the diff between the current mozcentral build and the
// baseline mozcentral build, which both exist at this point.
// The mozcentral baseline directory is a Git repository, so we
// remove all files and copy the current mozcentral build files
// into it to create the diff.
rimraf.sync(MOZCENTRAL_BASELINE_DIR + "*");
// Remove all files/folders, except for `.git` because it needs to be a
// valid Git repository for the Git commands below to work.
for (const entry of fs.readdirSync(MOZCENTRAL_BASELINE_DIR)) {
if (entry !== ".git") {
fs.rmSync(MOZCENTRAL_BASELINE_DIR + entry, {
recursive: true,
force: true,
});
}
}

gulp
.src([BUILD_DIR + "mozcentral/**/*"])
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"postcss-nesting": "^12.1.4",
"prettier": "^3.2.5",
"puppeteer": "^22.8.1",
"rimraf": "^3.0.2",
"streamqueue": "^1.1.2",
"stylelint": "^16.5.0",
"stylelint-prettier": "^5.0.0",
Expand Down

0 comments on commit fad1432

Please sign in to comment.