Rename directory: ext -> extension

This commit is contained in:
hensm
2023-02-26 18:21:59 +00:00
parent 33bcbc0dca
commit a9406fde11
119 changed files with 40 additions and 42 deletions

View File

@@ -0,0 +1,107 @@
// @ts-check
"use strict";
import path from "path";
import fs from "fs";
// eslint-disable-next-line no-unused-vars
import esbuild from "esbuild";
/**
* Walks file tree from a given root path.
* @param {string} rootPath
*/
function* walk(rootPath) {
const pathsToWalk = [rootPath];
while (pathsToWalk.length > 0) {
const currentPath = /** @type {string} */ (pathsToWalk.pop());
if (fs.statSync(currentPath).isFile()) {
yield currentPath;
} else {
for (const child of fs.readdirSync(currentPath)) {
pathsToWalk.push(path.join(currentPath, child));
}
}
}
}
/**
* @typedef {object} CopyFilesPluginOpts
* @prop {string} src Source path
* @prop {string} dest Destination path
* @prop {RegExp=} excludePattern Exclude path pattern
*/
/**
* Plugin that copies files from specified source to destination after
* each build.
*
* @type {(opts: CopyFilesPluginOpts) => esbuild.Plugin}
*/
export default opts => {
if (!fs.existsSync(opts.src)) {
throw new Error("copyFilesPlugin: src path not found!");
}
const matchingPaths = [...walk(opts.src)].filter(
path => !opts.excludePattern?.test(path)
);
return {
name: "copy-files",
setup(build) {
/** First run for the set of import paths in each build. */
let isFirstRun = true;
build.onResolve({ filter: /.*/ }, () => {
/**
* Attach watch files to first resolve result.
* Presumably there is a much better way of doing
* this?
*/
if (isFirstRun) {
isFirstRun = false;
return {
watchFiles: matchingPaths
};
}
});
build.onEnd(() => {
isFirstRun = true;
// Copy any watched files that changed
for (const srcPath of matchingPaths) {
const destPath = path.resolve(
opts.dest,
path.relative(opts.src, srcPath)
);
// Ignore if source file is missing
if (!fs.existsSync(srcPath)) {
if (fs.existsSync(destPath)) {
fs.rmSync(destPath);
}
continue;
}
// Ensure containing destination directory exists
const dirName = path.dirname(destPath);
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, { recursive: true });
}
// Check if files match
if (fs.existsSync(destPath)) {
const srcContent = fs.readFileSync(srcPath);
const destContent = fs.readFileSync(destPath);
if (srcContent.equals(destContent)) {
continue;
}
}
fs.copyFileSync(srcPath, destPath);
}
});
}
};
};