Skip to content

Commit

Permalink
exclude option added
Browse files Browse the repository at this point in the history
  • Loading branch information
ksv90 committed Aug 11, 2023
1 parent d55c0bf commit f55d927
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-hornets-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@releaseband/vite-plugin-meta": patch
---

exclude option added
34 changes: 23 additions & 11 deletions src/MetaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export default class MetaPlugin {
optionLog: option.optionLog,
publicLog: option.publicLog,
fileChangeLog: option.fileChangeLog,
exclude: option.exclude ?? [],
};

if (option.optionLog) console.log(this.option);
}

Expand All @@ -71,17 +73,23 @@ export default class MetaPlugin {
const videoExt: ReadonlyArray<string> = [Ext.mp4];

if (this.option.publicLog) console.log(publicDir);
[this.imagesFiles, this.soundsFiles, this.animationsFiles, this.videoFiles] = getFilesPaths(publicDir).reduce(
([imagesFiles, soundsFiles, animationsFiles, videoFiles], file) => {
const extname = path.extname(file).toLowerCase();
if (imagesExt.includes(extname)) imagesFiles.push(file);
else if (soundsExt.includes(extname)) soundsFiles.push(file);
else if (animationsExt.includes(extname)) animationsFiles.push(file);
else if (videoExt.includes(extname)) videoFiles.push(file);
return [imagesFiles, soundsFiles, animationsFiles, videoFiles];
},
[new Array<string>(), new Array<string>(), new Array<string>(), new Array<string>()]
);
[this.imagesFiles, this.soundsFiles, this.animationsFiles, this.videoFiles] = getFilesPaths(publicDir)
.filter((filePath) => {
const newPath = filePath.replace(`${publicDir}${path.sep}`, '');
return !this.option.exclude.find((file) => newPath === file);
})
.reduce(
([imagesFiles, soundsFiles, animationsFiles, videoFiles], file) => {
const extname = path.extname(file).toLowerCase();
if (imagesExt.includes(extname)) imagesFiles.push(file);
else if (soundsExt.includes(extname)) soundsFiles.push(file);
else if (animationsExt.includes(extname)) animationsFiles.push(file);
else if (videoExt.includes(extname)) videoFiles.push(file);
return [imagesFiles, soundsFiles, animationsFiles, videoFiles];
},
[new Array<string>(), new Array<string>(), new Array<string>(), new Array<string>()]
);

if (this.option.selectFilesLog) {
console.log(this.imagesFiles, this.soundsFiles, this.animationsFiles, this.videoFiles);
}
Expand Down Expand Up @@ -114,6 +122,7 @@ export default class MetaPlugin {
const fileHash = await makeHash(imagePath);
if (this.option.converLog) console.log(imagePath, this.filesHash[imagePath], fileHash);
if (this.filesHash[imagePath] === fileHash) return;
if (this.option.fileChangeLog) fileLog(`file conversion "${imagePath}" started`);
await convertImage(imagePath, this.option.storageDir);
if (this.option.fileChangeLog) fileLog('add', imagePath);
this.filesHash[imagePath] = fileHash;
Expand All @@ -130,6 +139,7 @@ export default class MetaPlugin {
const fileHash = await makeHash(soundPath);
if (this.option.converLog) console.log(soundPath, this.filesHash[soundPath], fileHash);
if (this.filesHash[soundPath] === fileHash) return;
if (this.option.fileChangeLog) fileLog(`file conversion "${soundPath}" started`);
await convertSound(soundPath, formats, this.option.storageDir);
if (this.option.fileChangeLog) fileLog('add', soundPath);
this.filesHash[soundPath] = fileHash;
Expand All @@ -147,6 +157,7 @@ export default class MetaPlugin {
const fileHash = await makeHash(animationPath);
if (this.option.converLog) console.log(animationPath, this.filesHash[animationPath], fileHash);
if (this.filesHash[animationPath] === fileHash) return;
if (this.option.fileChangeLog) fileLog(`file conversion "${animationPath}" started`);
if (nb_frames && +nb_frames > 50) {
console.warn(`image "${animationPath}" contains ${nb_frames} frames`);
}
Expand All @@ -166,6 +177,7 @@ export default class MetaPlugin {
const fileHash = await makeHash(videoPath);
if (this.option.converLog) console.log(videoPath, this.filesHash[videoPath], fileHash);
if (this.filesHash[videoPath] === fileHash) return;
if (this.option.fileChangeLog) fileLog(`file conversion "${videoPath}" started`);
await convertVideo(videoPath, formats, this.option.storageDir);
if (this.option.fileChangeLog) fileLog('add', videoPath);
this.filesHash[videoPath] = fileHash;
Expand Down
4 changes: 4 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const enum Flags {
optionLog = '--optionLog',
publicLog = '--publicLog',
fileChangeLog = '--fileChangeLog',
exclude = '--exclude',
}

export const getParameter = (key: string): string | null => {
Expand All @@ -34,6 +35,9 @@ const plugin = new MetaPlugin({
optionLog: checkParameter(Flags.optionLog),
publicLog: checkParameter(Flags.publicLog),
fileChangeLog: checkParameter(Flags.fileChangeLog),
exclude: getParameter(Flags.exclude)
?.split(',')
.map((file) => file.trim()),
});

const publicDir = getParameter(Flags.publicDir) ?? Names.publicDir;
Expand Down
4 changes: 2 additions & 2 deletions src/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sharp from 'sharp';
import { createHash } from 'node:crypto';

import { replaceRoot, waitConvert } from './helpers';
import { Ext } from './types';
import { Ext, VideoCodecs } from './types';

export function getFilesPaths(inputPath: string): ReadonlyArray<string> {
if (!existsSync(inputPath)) return [];
Expand Down Expand Up @@ -159,7 +159,7 @@ export async function convertVideo(
try {
await Promise.all([
ffmpeg('-i', videoPath, formatsOptions[Ext.mp4], newPath.replace(ext, Ext.mp4)),
ffmpeg('-i', videoPath, formatsOptions[Ext.av1], newPath.replace(ext, `.av1${Ext.mp4}`)),
ffmpeg('-i', videoPath, formatsOptions[Ext.av1], newPath.replace(ext, `.${VideoCodecs.av1}${Ext.mp4}`)),
]);
} catch (err) {
throw new Error(`${convertVideo.name} ${videoPath} file error:\n${String(err)}`);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ export type MetaPluginOption = {
readonly optionLog?: boolean;
readonly publicLog?: boolean;
readonly fileChangeLog?: boolean;
readonly exclude: ReadonlyArray<string>;
};

0 comments on commit f55d927

Please sign in to comment.