Skip to content

Commit

Permalink
build: use ESM script to cleann
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed May 19, 2021
1 parent 7c3c32f commit a702b4f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
44 changes: 44 additions & 0 deletions scripts/clean.mjs
Original file line number Diff line number Diff line change
@@ -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()
]);
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6339,20 +6339,20 @@ reusify@^1.0.4:
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==

[email protected], 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"
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
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"
Expand Down

0 comments on commit a702b4f

Please sign in to comment.