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/> 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 linaria from "@linaria/esbuild";
import { build, getFilesInDirectory } from "@gnu-taler/web-util/build";
await build({ await build({
type: "production", type: "production",

View File

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