assets should have base directory to copy tree

This commit is contained in:
Sebastian 2023-06-01 12:23:41 -03:00
parent b916e53c68
commit e9bdf7f312
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069
2 changed files with 31 additions and 15 deletions

View File

@ -15,8 +15,8 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { build } from "@gnu-taler/web-util/build";
import linaria from "@linaria/esbuild";
import { build, getFilesInDirectory } from "@gnu-taler/web-util/build";
await build({
type: "production",

View File

@ -10,12 +10,17 @@ import postcssrc from "postcss-load-config";
// the project is being built
const BASE = process.cwd();
export function getFilesInDirectory(
startPath: string,
regex?: RegExp,
): string[] {
type Assets = {
base: string;
files: string[];
};
export function getFilesInDirectory(startPath: string, regex?: RegExp): Assets {
if (!fs.existsSync(startPath)) {
return [];
return {
base: startPath,
files: [],
};
}
const files = fs.readdirSync(startPath);
const result = files
@ -24,7 +29,7 @@ export function getFilesInDirectory(
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
return getFilesInDirectory(filename, regex);
return getFilesInDirectory(filename, regex).files;
}
if (!regex || regex.test(filename)) {
return [filename];
@ -33,7 +38,10 @@ export function getFilesInDirectory(
})
.filter((x) => !!x);
return result;
return {
base: startPath,
files: result,
};
}
let GIT_ROOT = BASE;
@ -66,22 +74,30 @@ function git_hash() {
}
// FIXME: Put this into some helper library.
function copyFilesPlugin(files: Array<string>) {
function copyFilesPlugin(assets: Assets | Assets[]) {
return {
name: "copy-files",
setup(build: PluginBuild) {
if (!files || !files.length) {
if (!assets || (assets instanceof Array && !assets.length)) {
return;
}
const list = assets instanceof Array ? assets : [assets];
const outDir = build.initialOptions.outdir;
if (outDir === undefined) {
throw Error("esbuild build options does not specify outdir");
}
build.onEnd(() => {
for (const file of files) {
const name = path.parse(file).base;
fs.copyFileSync(file, path.join(outDir, name));
list.forEach((as) => {
for (const file of as.files) {
const destination = path.join(outDir, path.relative(as.base, file));
const dirname = path.dirname(destination);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
fs.copyFileSync(file, destination);
}
});
});
},
};
@ -175,7 +191,7 @@ const defaultEsBuildConfig: esbuild.BuildOptions = {
export interface BuildParams {
type: "development" | "test" | "production";
source: {
assets: string[];
assets: Assets | Assets[];
js: string[];
};
public?: string;