Skip to content

Commit

Permalink
Update move-assets.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
cdedreuille committed Feb 13, 2024
1 parent aba61fe commit 6956c78
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions scripts/transform-nextjs/move-assets.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import fs from 'fs';
import path from 'path';

export function moveMediaFiles(sourceDirectory: string, targetDirectory: string) {
if (!fs.existsSync(targetDirectory)) {
fs.mkdirSync(targetDirectory, { recursive: true });
}
export function moveMediaFiles(
sourceDirectory: string,
targetDirectory: string,
currentDirectory = sourceDirectory
) {
const files = fs.readdirSync(currentDirectory);

const files = fs.readdirSync(sourceDirectory);

files.forEach((file: any) => {
const filePath = path.join(sourceDirectory, file);
files.forEach((file) => {
const filePath = path.join(currentDirectory, file);
const stats = fs.statSync(filePath);

if (stats.isDirectory()) {
moveMediaFiles(filePath, targetDirectory);
moveMediaFiles(sourceDirectory, targetDirectory, filePath);
} else {
const ext = path.extname(file).toLowerCase();
if (['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.mp4'].includes(ext)) {
const targetPath = path.join(targetDirectory, file);
const relativePath = path.relative(sourceDirectory, currentDirectory);
const targetPathDirectory = path.join(targetDirectory, relativePath);

if (!fs.existsSync(targetPathDirectory)) {
fs.mkdirSync(targetPathDirectory, { recursive: true });
}

const targetPath = path.join(targetPathDirectory, file);
fs.renameSync(filePath, targetPath);
}
}
Expand Down

0 comments on commit 6956c78

Please sign in to comment.