-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.