Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 18, 2024
1 parent 4dcae55 commit 6a0ecda
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 566 deletions.
67 changes: 15 additions & 52 deletions code/node/archive.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
import {
BuildBaseInputFileOutputDirectoryModel,
UNARCHIVER_FORMAT_KEY,
buildCommandToConvertArchive,
buildCommandToDecompressWith7z,
buildCommandToDecompressWithUnarchiver,
} from '../shared/index.js'
import {
buildUseConvert,
buildUseDecompress,
transform_input_file_output_directory_array,
transform_input_output_file_array,
} from './base.js'
import { handleCommand } from './command.js'

// defineCompress('rar', buildCommandToCreateRar, runRarCommand)
Expand All @@ -27,50 +14,26 @@ import { handleCommand } from './command.js'
// await run7zCommand(cmd)
// }

const IO7z = BuildBaseInputFileOutputDirectoryModel.superRefine(
transform_input_file_output_directory_array(['7z']),
)

export const useDecompressWith7z = buildUseDecompress(['7z'], '7z')

export async function decompressWith7z(source) {
const input = IO7z.parse(source)
const list = buildCommandToDecompressWith7z(input)
for (const cmd of list) {
await handleCommand(cmd)
}
// const input = IO7z.parse(source)
// const list = buildCommandToDecompressWith7z(input)
// for (const cmd of list) {
// await handleCommand(cmd)
// }
}

const IOUnarchiver = BuildBaseInputFileOutputDirectoryModel.superRefine(
transform_input_file_output_directory_array(UNARCHIVER_FORMAT_KEY),
)

export const useDecompressWithUnarchiver = buildUseDecompress(
UNARCHIVER_FORMAT_KEY,
'unarchiver',
)

export async function decompressWithUnarchiver(source) {
const input = IOUnarchiver.parse(source)
const list = buildCommandToDecompressWithUnarchiver(input)
for (const cmd of list) {
await handleCommand(cmd)
}
// const input = IOUnarchiver.parse(source)
// const list = buildCommandToDecompressWithUnarchiver(input)
// for (const cmd of list) {
// await handleCommand(cmd)
// }
}

const IOConvertArchive = BuildBaseFileInputOutputModel.superRefine(
transform_input_output_file_array(UNARCHIVER_FORMAT_KEY, ['zip']),
)

export const useConvertArchive = buildUseConvert(
UNARCHIVER_FORMAT_KEY,
['zip'],
)

export async function convertArchive(source) {
const input = IOConvertArchive.parse(source)
const list = buildCommandToConvertArchive(input)
for (const cmd of list) {
await handleCommand(cmd)
}
// const input = IOConvertArchive.parse(source)
// const list = buildCommandToConvertArchive(input)
// for (const cmd of list) {
// await handleCommand(cmd)
// }
}
197 changes: 1 addition & 196 deletions code/node/base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { RefinementCtx, z } from 'zod'
import { replaceFileExtension } from '../shared/tool.js'
import path from 'path'
import {
BuildBaseFileInputModel,
BuildBaseFileInputOutputModel,
Task,
} from '../shared/index.js'
import kink from '../shared/kink.js'
import { BuildBaseFileInputModel, Task } from '../shared/index.js'

export const transform_input_output_file =
(a: string, b: string) => (v: any, ctx: RefinementCtx) => {
Expand Down Expand Up @@ -47,193 +42,3 @@ export const transform_input_output_file =

return v
}

export const transform_input_output_file_array =
(a: ReadonlyArray<string>, b: ReadonlyArray<string>) =>
(v: any, ctx: RefinementCtx) => {
const inputPathFormat = path
.extname(v.input.file.path as string)
.slice(1)

if (!a.includes(inputPathFormat)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Input path has incorrect extension.`,
path: ['input', 'file', 'path'],
params: {
format: a,
},
})
}

v.input.format = inputPathFormat

const outputFormat = b.filter(x => x !== inputPathFormat)

if (!v.output.file?.path) {
if (!v.output.format) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Must specify a an output file path or format.`,
path: ['output'],
})
} else if (!outputFormat.includes(v.output.format as string)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Output format is incorrect.`,
path: ['output', 'format'],
params: {
format: b,
},
})
} else {
v.output.file ??= {}
v.output.file.path = replaceFileExtension(
v.input.file.path as string,
`.${v.output.format}`,
)
}
} else {
const outputPathFormat = path
.extname(v.output.file.path as string)
.slice(1)

if (!outputFormat.includes(outputPathFormat)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Output path has incorrect extension.`,
path: ['output', 'file', 'path'],
})
}
}

return v
}

export const transform_input_file_output_directory_array =
(a: ReadonlyArray<string>) => (v: any, ctx: RefinementCtx) => {
const inputPathFormat = path
.extname(v.input.file.path as string)
.slice(1)

if (!a.includes(inputPathFormat)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Input path has incorrect extension.`,
path: ['input', 'file', 'path'],
params: {
format: a,
},
})
}

return v
}

export function buildUseConvert(
i: ReadonlyArray<string>,
o: ReadonlyArray<string>,
tool?: string,
) {
return async (task: Task, source): Promise<boolean> => {
if (task !== 'convert') {
return false
}

const input = BuildBaseFileInputOutputModel.parse(source)
const inputFormat = path
.extname(input.input.file.path)
.slice(1)
.toLowerCase()

if (!i.includes(inputFormat as (typeof i)[number])) {
return false
}

const outputFormat = input.output.file?.path
? path.extname(input.output.file.path).slice(1).toLowerCase()
: input.output.format
? input.output.format.toLowerCase()
: undefined

if (
!outputFormat ||
!o.includes(outputFormat as (typeof o)[number])
) {
return false
}

if (inputFormat === outputFormat) {
return false
}

if (tool && input.tool && input.tool !== tool) {
return false
}

return true
}
}

export function buildUseSplit(i: ReadonlyArray<string>, tool: string) {
return async (task: Task, source): Promise<boolean> => {
if (task !== 'split') {
return false
}

const input = BuildBaseFileInputModel.parse(source)
const inputFormat = path
.extname(input.input.file.path as string)
.slice(1)
.toLowerCase()

if (!i.includes(inputFormat as (typeof i)[number])) {
return false
}

const outputFormat = path
.extname(input.output.file.path as string)
.slice(1)
.toLowerCase()

if (
!outputFormat ||
!i.includes(outputFormat as (typeof i)[number])
) {
return false
}

if (input.tool && input.tool !== tool) {
return false
}

return true
}
}

export function buildUseDecompress(
i: ReadonlyArray<string>,
tool: string,
) {
return async (task: Task, source): Promise<boolean> => {
if (task !== 'decompress') {
return false
}

const input = BuildBaseFileInputOutputModel.parse(source)
const inputFormat = path
.extname(input.input.file.path)
.slice(1)
.toLowerCase()

if (!i.includes(inputFormat as (typeof i)[number])) {
return false
}

if (input.tool && input.tool !== tool) {
return false
}

return true
}
}
20 changes: 6 additions & 14 deletions code/node/call/convert.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable react-hooks/rules-of-hooks */
import {
CALIBRE_INPUT_FORMAT_CONTENT_KEY,
CALIBRE_OUTPUT_FORMAT_CONTENT_KEY,
CalibreInputFormatContentKey,
CalibreOutputFormatContentKey,
CALIBRE_INPUT_FORMAT,
CALIBRE_OUTPUT_FORMAT,
CalibreInputFormat,
CalibreOutputFormat,
FFMPEG_FORMAT,
FONT_FORMAT,
FfmpegFormat,
Expand Down Expand Up @@ -138,18 +138,10 @@ export function useConvertDocumentWithCalibre(a: string, b: string) {
if (a === b) {
return false
}
if (
!CALIBRE_INPUT_FORMAT_CONTENT_KEY.includes(
a as CalibreInputFormatContentKey,
)
) {
if (!CALIBRE_INPUT_FORMAT.includes(a as CalibreInputFormat)) {
return false
}
if (
!CALIBRE_OUTPUT_FORMAT_CONTENT_KEY.includes(
b as CalibreOutputFormatContentKey,
)
) {
if (!CALIBRE_OUTPUT_FORMAT.includes(b as CalibreOutputFormat)) {
return false
}
return true
Expand Down
9 changes: 7 additions & 2 deletions code/node/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import bytes from 'bytes'
import child_process from 'child_process'
import { Command, CommandCall, CommandName } from '../shared/index.js'
import kink from '../shared/kink.js'
import { ChildProcessError, Message, exec, spawn } from './process.js'
import { ChildProcessError, Message, exec } from './process.js'
import Kink from '@termsurf/kink'

export type CommandHandle = (cmd: CommandCall) => Promise<string | void>
Expand Down Expand Up @@ -36,6 +36,7 @@ export const COMMAND_HANDLE: Record<CommandName, CommandHandle> = {
zip: handleZipCommand,
tar: handleTarCommand,
exiftool: handleExiftoolCommand,
gifsicle: handleGifsicleCommand,
'ebook-convert': handleEbookConvertCommand,
soffice: handleSofficeCommand,
jupyter: handleJupyterCommand,
Expand All @@ -46,7 +47,7 @@ export const COMMAND_HANDLE: Record<CommandName, CommandHandle> = {
export async function handleFfmpegCommand(
cmd: CommandCall,
onUpdate?: (msg: Message) => void,
) {
): Promise<void> {
return new Promise((res, rej) => {
const command = cmd.shift() as string
const child = child_process.spawn(command, cmd)
Expand Down Expand Up @@ -81,6 +82,10 @@ export async function handleFfmpegCommand(
})
}

export async function handleGifsicleCommand(cmd: CommandCall) {
return await exec(cmd.join(' '))
}

export async function handleBlackCommand(cmd: CommandCall) {
return await exec(cmd.join(' '))
}
Expand Down
Loading

0 comments on commit 6a0ecda

Please sign in to comment.