From a702b4f567d7226a2cd3aba8375b72140bec4d15 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Wed, 19 May 2021 23:49:31 +0200 Subject: [PATCH] build: use ESM script to cleann --- package.json | 3 +-- scripts/clean.mjs | 44 ++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 14 +++++++------- 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 scripts/clean.mjs diff --git a/package.json b/package.json index e4b4d9cf..2c00ec9c 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "next build src", "start": "next start src", "export": "next export src", - "clean": "rimraf src/.next src/public/sw.js src/public/workbox* src/public/sitemap.xml", + "clean": "node scripts/clean.mjs", "build:sitemap": "node scripts/sitemapGenerator.mjs", "format": "prettier --write --loglevel=error src/**/*.{js,jsx,ts,tsx}", "update": "yarn upgrade-interactive --latest", @@ -79,7 +79,6 @@ "nextjs-sitemap-generator": "^1.3.1", "prettier": "^2.3.0", "pretty-quick": "^3.1.0", - "rimraf": "3.0.2", "typescript": "4.2.4", "utility-types": "^3.10.0", "webpack": "^5.37.0" diff --git a/scripts/clean.mjs b/scripts/clean.mjs new file mode 100644 index 00000000..71cc5cbf --- /dev/null +++ b/scripts/clean.mjs @@ -0,0 +1,44 @@ +import { opendir, rm } from 'node:fs/promises'; +import { join } from 'node:path'; +import { pathToFileURL, URL } from 'node:url'; + +async function* scan(path, cb) { + const dir = await opendir(path); + + for await (const item of dir) { + const file = join(dir.path, item.name); + if (item.isFile()) { + if (cb(file)) yield file; + } else if (item.isDirectory()) { + yield* scan(file, cb); + } + } +} + +const workboxFileRegex = /workbox-/; +const srcFolder = new URL('../src/', import.meta.url); +const publicFolder = new URL('public/', srcFolder); +const nextFolder = new URL('.next/', srcFolder); +const tsbuildInfoFile = new URL('.tsbuildinfo', srcFolder); +const serviceWorkerFile = new URL('sw.js', publicFolder); +const sitemapFile = new URL('sitemap.xml', publicFolder); + +const options = { recursive: true, force: true }; + +let workboxFile; + +for await (const path of scan(publicFolder, (path) => workboxFileRegex.test(path))) { + workboxFile = path; +} + +if (workboxFile) { + workboxFile = pathToFileURL(workboxFile); +} + +await Promise.all([ + rm(nextFolder, options), + rm(tsbuildInfoFile, options), + rm(serviceWorkerFile, options), + rm(sitemapFile, options), + workboxFile ? rm(workboxFile, options) : Promise.resolve() +]); diff --git a/yarn.lock b/yarn.lock index 14f83c27..bcc98db6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6339,13 +6339,6 @@ reusify@^1.0.4: resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76" integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw== -rimraf@3.0.2, rimraf@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a" - integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA== - dependencies: - glob "^7.1.3" - rimraf@^2.6.3: version "2.7.1" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec" @@ -6353,6 +6346,13 @@ rimraf@^2.6.3: dependencies: glob "^7.1.3" +rimraf@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a" + integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA== + dependencies: + glob "^7.1.3" + ripemd160@^2.0.0, ripemd160@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"