Skip to content

Commit

Permalink
files transfer process
Browse files Browse the repository at this point in the history
  • Loading branch information
Сергей Краснов committed Jun 16, 2023
1 parent 2368596 commit afbea9f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-trains-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@releaseband/vite-plugin-meta": minor
---

file storage added
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"README.md"
],
"scripts": {
"build": "tsup-node src/index.ts src/updateHash.ts --sourcemap --dts --format cjs,esm",
"build": "tsup-node src/index.ts src/updateHash.ts --sourcemap --clean --dts --format cjs,esm",
"dev": "pnpm build --watch",
"changeset": "changeset",
"version": "changeset version && pnpm install --lockfile-only",
Expand Down
39 changes: 33 additions & 6 deletions src/MetaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
makeHash,
readConfig,
removeFile,
transferFile,
writeConfig,
} from './processes';
import { MetaPluginOption, Names } from './types';
Expand Down Expand Up @@ -60,11 +61,15 @@ export default class MetaPlugin {
this.publicDir = publicDir;
const imagesExt: ReadonlyArray<string> = [Ext.png, Ext.jpg, Ext.jpeg];
const soundsExt: ReadonlyArray<string> = [Ext.wav];
getFilesPaths(publicDir).forEach((file) => {
const extname = path.extname(file).toLowerCase();
if (imagesExt.includes(extname)) this.imagesFiles.push(file);
else if (soundsExt.includes(extname)) this.soundsFiles.push(file);
});
[this.imagesFiles, this.soundsFiles] = getFilesPaths(publicDir).reduce(
([imagesFiles, soundsFiles], file) => {
const extname = path.extname(file).toLowerCase();
if (imagesExt.includes(extname)) return [[...imagesFiles, file], soundsFiles];
else if (soundsExt.includes(extname)) return [imagesFiles, [...soundsFiles, file]];
return [imagesFiles, soundsFiles];
},
[new Array<string>(), new Array<string>()]
);
}

public async loadHashs(): Promise<void> {
Expand Down Expand Up @@ -159,14 +164,36 @@ export default class MetaPlugin {
await Promise.all(jobs);
}

public async convertProcess(): Promise<void> {
public async convertProcess(publicDir: string): Promise<void> {
this.selectFiles(publicDir);
await this.loadHashs();
await this.checkFiles();
await this.imagesConversionProcess();
await this.soundsConversionProcess();
await this.writeHashConfig();
}

public async transferProcess(): Promise<void> {
const imagesJobs = this.imagesFiles.map(async (filePath) => {
const ext = path.extname(filePath);
const newPath = replaceRoot(filePath, this.option.storageDir);
await removeFile(filePath);
await transferFile(newPath.replace(ext, Ext.png), filePath.replace(ext, Ext.png));
await transferFile(newPath.replace(ext, Ext.avif), filePath.replace(ext, Ext.avif));
await transferFile(newPath.replace(ext, Ext.webp), filePath.replace(ext, Ext.webp));
});
await Promise.all(imagesJobs);
const soundJobs = this.soundsFiles.map(async (filePath) => {
const ext = path.extname(filePath);
const newPath = replaceRoot(filePath, this.option.storageDir);
await removeFile(filePath);
await transferFile(newPath.replace(ext, Ext.m4a), filePath.replace(ext, Ext.m4a));
await transferFile(newPath.replace(ext, Ext.ogg), filePath.replace(ext, Ext.ogg));
await transferFile(newPath.replace(ext, Ext.mp3), filePath.replace(ext, Ext.mp3));
});
await Promise.all(soundJobs);
}

public async removeConfig(): Promise<void> {
if (!this.configPath) return;
await removeFile(this.configPath);
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const metaPlugin = (pluginConfig: MetaPluginConfig = {}): PluginOption => {
version: process.env['GAME_VERSION'] || '0.0.0',
metaConfigName: pluginConfig.metaConfigName,
hashConfigName: pluginConfig.hashConfigName,
storageDir: pluginConfig.storageDir ?? 'file-storage',
storageDir: pluginConfig.storageDir,
});

let config: PluginConfig;
Expand Down Expand Up @@ -71,10 +71,11 @@ const metaPlugin = (pluginConfig: MetaPluginConfig = {}): PluginOption => {
const { logger, outDir } = config;
logger.info(`\n${greenText('Metaprocesses started')}`);
try {
if (convert) await plugin.convertProcess(config.publicDir);
plugin.selectFiles(outDir);
if (audioDuration) await plugin.audioDurationProcess();
if (convert) await plugin.convertProcess();
await plugin.writeConfig(convert, outDir);
if (convert) await plugin.transferProcess();
logger.info(`${greenText('✓')} metaprocesses completed\n`);
} catch (err) {
logger.error(createError(err));
Expand Down
10 changes: 9 additions & 1 deletion src/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ffmpeg from 'ffmpeg-static';
import { spawn } from 'node:child_process';
import { createReadStream, createWriteStream, readdirSync, statSync, existsSync, mkdirSync } from 'node:fs';
import { rename, unlink, writeFile, readFile } from 'node:fs/promises';
import { rename, unlink, writeFile, readFile, copyFile } from 'node:fs/promises';
import { extname, join, sep, dirname } from 'node:path';
import ffprobeStatic from 'ffprobe-static';
import ffprobe from 'ffprobe';
Expand Down Expand Up @@ -119,6 +119,14 @@ export async function getAudioDuration(soundPath: string): Promise<number> {
}
}

export async function transferFile(fromFilePath: string, toFilePath: string): Promise<void> {
try {
await copyFile(fromFilePath, toFilePath);
} catch (err) {
throw new Error(`${transferFile.name} error \n` + String(err));
}
}

// TODO: не используется
export async function imageConvert(imagePath: string): Promise<void> {
return new Promise((resolve, reject) => {
Expand Down
3 changes: 1 addition & 2 deletions src/updateHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const publicDir = getParameter(Flags.publicDir) ?? Names.publicDir;
const hashConfigName = getParameter(Flags.configName) ?? Names.hashConfigName;

const plugin = new MetaPlugin({ storageDir, hashConfigName });
plugin.selectFiles(publicDir);
plugin.convertProcess().catch((err) => {
plugin.convertProcess(publicDir).catch((err) => {
console.error(String(err));
process.exit(1);
});

0 comments on commit afbea9f

Please sign in to comment.