iles
Version:
Vite & Vue powered static site generator with partial hydration
57 lines (55 loc) • 1.53 kB
JavaScript
// src/node/build/utils.ts
import fs from "fs";
import { performance } from "perf_hooks";
import newSpinner from "mico-spinner";
var warnMark = "\x1B[33m\u26A0\x1B[0m";
async function withSpinner(message, fn) {
const spinner = newSpinner(message).start();
const startTime = performance.now();
try {
const result = await fn();
spinner.succeed();
console.info(` done in ${timeSince(startTime)}
`);
return result;
} catch (e) {
spinner.fail();
throw e;
}
}
function timeSince(start) {
const diff = performance.now() - start;
return diff < 750 ? `${Math.round(diff)}ms` : `${(diff / 1e3).toFixed(1)}s`;
}
function slash(path) {
return path.replace(/\\/g, "/");
}
function uniq(arr) {
return [...new Set(arr.filter((x) => x))];
}
function flattenPath(path) {
return pathToFilename(path).replace(/\//g, "_");
}
function pathToFilename(path, ext = "") {
if (path.endsWith(ext)) ext = "";
const decodedPath = decodeURIComponent(path);
return `${(decodedPath.endsWith("/") ? `${decodedPath}index` : decodedPath).replace(/^\//g, "")}${ext}`;
}
async function replaceAsync(str, regex, asyncFn) {
const promises = Array.from(str.matchAll(regex)).map(([match, ...args]) => asyncFn(match, ...args));
const replacements = await Promise.all(promises);
return str.replace(regex, () => replacements.shift());
}
function rm(dir) {
fs.rmSync(dir, { recursive: true, force: true });
}
export {
warnMark,
withSpinner,
slash,
uniq,
flattenPath,
pathToFilename,
replaceAsync,
rm
};