From 4efffe18242a171d313229975ea6f6bc31477834 Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Wed, 10 Jan 2024 14:21:12 +0100 Subject: [PATCH] the copy module --- bin/copy.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ vite.config.ts | 4 ++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 bin/copy.ts diff --git a/bin/copy.ts b/bin/copy.ts new file mode 100644 index 0000000..75b39b7 --- /dev/null +++ b/bin/copy.ts @@ -0,0 +1,71 @@ +import * as path from 'path'; +import * as fs from "fs/promises"; +import {Stats} from "node:fs"; +import fg from 'fast-glob'; + +const SRC_DIR = path.resolve(process.cwd(), 'src'); +const BUILD_DIR = path.resolve(process.cwd(), 'build'); + + +// a regex to match all the php files +const phpFileMatch = /\.php$/ + +/** + * Returns a custom global resolver that maps external libraries and objects to their `window` counterparts + */ +export function copy(id: string) { + console.log("🔗 copyFiles ", id); + // if the file exists in the build folder and the modification time is newer than the source file + fs.access(path.join(BUILD_DIR, id)) + .then(async () => { + // Get the modification time of the file in the build folder + const buildStat: Promise = fs.stat(path.join(BUILD_DIR, id)); + // Get the modification time of the file in the source folder + const srcStat: Promise = fs.stat(path.join(SRC_DIR, id)); + // Wait for both promises + await Promise.all([buildStat, srcStat]) + .then(([buildStat, srcStat]) => { + // if the modification time of the file in the build folder is newer than the source file + console.log("🔗 copyFiles old ", buildStat.mtimeMs, "new", srcStat.mtimeMs, "file", id); + // Copy the file to the build folder + if (buildStat.mtimeMs > srcStat.mtimeMs) { + // copy the file to the build folder + fs.copyFile(path.join(SRC_DIR, id), path.join(BUILD_DIR, id)); + } + }) + }) +} + + +export const wpCopy = (args?: { targets: string[] }) => { + + let config; + + let targets = args?.targets ?? ['**/*.php', 'block.json']; + const outDir = path.resolve(__dirname, 'build'); + + // console.log("🔗 config", config); + const copyTargets = fg(targets, {cwd: SRC_DIR }) + .then(files => files + .map(file => path + .join(config.root, 'src', file))); + + const pluginTools = { + name: "File-copy", + + + configResolved(resolvedConfig) { + // store the resolved config + config = resolvedConfig + console.log("🔗 targets", targets); + }, + + buildStart() { + const target = copyTargets + console.log(target) + target.then(res => res.forEach( file => copy(path.join(config.root, 'src', file)))); + } + }; + + return [pluginTools]; +}; diff --git a/vite.config.ts b/vite.config.ts index f8ecfac..f34455c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,6 +3,7 @@ import * as process from "process"; import {resolveGlobals, wpBlock} from "./bin/resolver"; import * as path from "node:path"; +import {wpCopy} from "./bin/copy"; export const HOST = 'http://localhost:8888' // or leave empty export const SOURCEFOLDER = 'src' @@ -23,7 +24,8 @@ export default defineConfig({ base: `${HOST}/wp-content/plugins/${blockConfig.name}/${BUILDFOLDER}/`, mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', plugins: [ - wpBlock(blockConfig) + wpCopy(), + wpBlock(blockConfig), ], server: { host: '0.0.0.0',