Skip to content

Commit

Permalink
restructure actions
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Oct 25, 2023
1 parent 7b9a0ca commit f4ca26b
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 217 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ module.exports = {
'json/*': ['error', 'allowComments']
}
}
]
],
rules: {
'@typescript-eslint/ban-ts-comment': 'warn'
}
};
213 changes: 0 additions & 213 deletions src/actions.ts

This file was deleted.

81 changes: 81 additions & 0 deletions src/actions/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { basename, extname } from "node:path";
import { convertFile, defaultOptions } from "./shared";
import { writeFile } from 'node:fs/promises';
import { watch } from 'chokidar';
import * as Utils from '../utils';
import colors from 'picocolors';
import logSymbols from 'log-symbols';

export async function convert(inputFiles, options = defaultOptions) {
if (options.watch) {
__watch(inputFiles, options);
} else {
console.log(/* let it breathe */);
console.time('✨ Completed');

await __convert(inputFiles, options);

console.log(/* let it breathe */);
console.timeEnd('✨ Completed');
}
}

function __watch(inputFiles, options = defaultOptions) {
console.log(/* let it breathe */);
console.log('👓 Watching for changes...');
console.log(/* let it breathe */);

const watcher = watch(inputFiles, {
awaitWriteFinish: {
stabilityThreshold: 5
}
});

const dictionary = {};

watcher.on('change', async inputFile => {
const hash = await Utils.hashFile(inputFile, 'sha1');

if (hash === dictionary[inputFile]) {
return;
}

await __convert(inputFile, options);

dictionary[inputFile] = hash;
});
}

async function __convert(inputFiles, options = defaultOptions) {
const avsFiles = Array.isArray(inputFiles)
? inputFiles
: [inputFiles];

for (const avsFile of avsFiles.sort()) {
const presetExtension = extname(avsFile);

if (!['.avs', '.wvs'].includes(presetExtension)) {
console.warn(logSymbols.warning, `Skipping conversion ${colors.cyan(avsFile)}`);
return;
}

const presetName = basename(avsFile, presetExtension);
const start = performance.now();

try {
const webvs = await convertFile(avsFile, options);
await writeFile(`${presetName}.webvs`, JSON.stringify(webvs, null, options.indent), 'utf-8');

console.log(logSymbols.success, `Converted ${colors.cyan(`${presetName}.avs`)} ${Utils.formatDuration(start)}`);
} catch (err) {
console.error(logSymbols.error, `Converted ${colors.cyan(`${presetName}.avs`)} ${Utils.formatDuration(start)}`);

if (options.debug) {
console.log(/* let it breathe */);
console.error(err instanceof Error ? colors.red(err.message) : colors.red(err));
}

continue;
}
}
}
Loading

0 comments on commit f4ca26b

Please sign in to comment.