Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy module #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions bin/copy.ts
Original file line number Diff line number Diff line change
@@ -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<Stats> = fs.stat(path.join(BUILD_DIR, id));
// Get the modification time of the file in the source folder
const srcStat: Promise<Stats> = 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];
};
4 changes: 3 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand Down