diff options
author | Sebastian <sebasjm@gmail.com> | 2023-06-01 12:23:41 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-06-01 12:24:07 -0300 |
commit | e9bdf7f312cff5d49b6a50e3722de38bb7238573 (patch) | |
tree | ad829ed828530cc1f8d5f72f06af55a68f78b785 /packages/web-util | |
parent | b916e53c68fc4e31fc7d945c450aaf9b0aeefc50 (diff) | |
download | wallet-core-e9bdf7f312cff5d49b6a50e3722de38bb7238573.tar.xz |
assets should have base directory to copy tree
Diffstat (limited to 'packages/web-util')
-rw-r--r-- | packages/web-util/src/index.build.ts | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/packages/web-util/src/index.build.ts b/packages/web-util/src/index.build.ts index ad31afb5c..6ee1be20a 100644 --- a/packages/web-util/src/index.build.ts +++ b/packages/web-util/src/index.build.ts @@ -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; |