Skip to content

Commit

Permalink
more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 13, 2024
1 parent 9d3ee4f commit 2640d74
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 133 deletions.
File renamed without changes.
12 changes: 0 additions & 12 deletions code/node/archive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { exec } from './process.js'
import archiver from 'archiver'
import fs from 'fs'
import { BuildCommandToDecompress7ZModel } from '../shared/index.js'

export async function buildCommandToDecompress7z(source) {
const input = BuildCommandToDecompress7ZModel.parse(source)
return [
`7z`,
`x`,
`"${input.input.directory}"`,
`-o`,
`"${input.output.file.path}"`,
]
}

export async function createZip({
inputDirectory,
Expand Down
83 changes: 58 additions & 25 deletions code/node/code.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
import path from 'path'
import {
AssemblySyntax,
ClangStyleAll,
BuildBaseFileInputOutputModel,
CallLink,
IOPath,
LLVM_ARCHITECTURE_CONTENT,
LlvmArchitecture,
buildCommandToFormatAssembly,
buildCommandToFormatC,
buildCommandToFormatCpp,
buildCommandToFormatJava,
buildCommandToFormatKotlin,
buildCommandToFormatPython,
buildCommandToFormatRuby,
buildCommandToFormatRust,
buildCommandToFormatSwift,
} from '../shared/index.js'
import { exec } from './process.js'
import { transform_input_output_file } from './base.js'

export const CALL_CODE: Record<string, CallLink> = {}
export const FIND_CODE = (input: Record<string, any>) => {
const i = input.input.file.path
? path.extname(input.input.file.path as string).replace(/^\./, '')
: input.input.file.format
? input.input.file.format
: undefined
const o = input.output.file.path
? path.extname(input.output.file.path as string).replace(/^\./, '')
: input.output.file.format
? input.output.file.format
: undefined
return CALL_CODE[`/${i}/format`]
}

function define(
lang: string,
build: (x) => Array<string>,
run: (cmd: Array<string>) => Promise<void>,
) {
const IO = BuildBaseFileInputOutputModel.superRefine(
transform_input_output_file(lang, lang),
)

CALL_CODE[String(`/${lang}/format`)] = async (source: any) => {
const input = IO.parse(source)
const cmd = build(input)
await run(cmd)
}
}

define('kotlin', buildCommandToFormatKotlin, runFormatKotlinCommand)
define('swift', buildCommandToFormatSwift, runFormatSwiftCommand)
define('rust', buildCommandToFormatRust, runFormatRustCommand)
define('python', buildCommandToFormatPython, runFormatPythonCommand)
define('ruby', buildCommandToFormatRuby, runFormatRubyCommand)
define(
'assembly',
buildCommandToFormatAssembly,
runFormatAssemblyCommand,
)
define('c', buildCommandToFormatC, runFormatCCommand)
define('cpp', buildCommandToFormatCpp, runFormatCCommand)
define('java', buildCommandToFormatJava, runFormatCCommand)

// CSharp: .cs
// Java: .java
Expand All @@ -16,23 +73,6 @@ import { exec } from './process.js'
// TableGen: .td
// TextProto: .textpb .pb.txt .textproto .asciipb
// Verilog: .sv .svh .v .vh
export async function formatCodeWithClang({
inputPath,
style = 'llvm',
}: {
inputPath: string
style?: ClangStyleAll['basedOnStyle']
}) {
let styleKey
if (typeof style === 'object') {
styleKey = JSON.stringify(style).replace(/"/g, '')
} else {
styleKey = style
}
// const styleKey = CLANG_FORMAT_CONTENT[style].key
// https://clang.llvm.org/docs/ClangFormatStyleOptions.html
return await exec(`clang-format --style="${style}" "${inputPath}"`)
}

export async function generateAssemblyFromLlvmIr({
inputPath,
Expand Down Expand Up @@ -72,37 +112,30 @@ export async function generateAssemblyFromLlvmIr({
// objdump disassembly
export async function runFormatKotlinCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatSwiftCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatRustCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatPythonCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatRubyCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatAssemblyCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runFormatCCommand(cmd: Array<string>) {
await exec(cmd.join(' '))
return cmd
}

export async function runSwiftCommand(command: Array<string>) {
Expand Down
101 changes: 67 additions & 34 deletions code/node/document.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { PDFDocument } from 'pdf-lib'
import { exec, spawn } from './process.js'
import { ChildProcessError, exec, spawn } from './process.js'
import tmp from 'tmp-promise'
import libre from 'libreoffice-convert'
import fs from 'node:fs/promises'
import {
BuildBaseFileInputOutputModel,
BuildCommandToConvertDocumentWithPandoc,
CALIBRE_INPUT_FORMAT_CONTENT_KEY,
CALIBRE_OUTPUT_FORMAT_CONTENT_KEY,
CallLink,
PANDOC_INPUT_FORMAT,
PANDOC_OUTPUT_FORMAT,
buildCommandToConvertDocumentWithCalibre,
buildCommandToConvertDocumentWithPandoc,
buildCommandToGenerateLatexPdf,
} from '../shared/index.js'
import path from 'path'
import { replaceFileExtension } from '../shared/tool.js'
import { RefinementCtx, z } from 'zod'
import { transform_input_output_file } from './base.js'

Expand Down Expand Up @@ -188,32 +190,64 @@ PANDOC_INPUT_FORMAT.forEach(a => {
source: any,
) => {
const input = IO.parse(source)
const cmd = await buildCommandToConvertDocumentWithPandoc(input)
await execPandoc(cmd)
}
})
})

const cmd = await buildCommandToConvertDocumentWithPandoc({
...input,
})
CALIBRE_INPUT_FORMAT_CONTENT_KEY.forEach(a => {
CALIBRE_OUTPUT_FORMAT_CONTENT_KEY.forEach(b => {
if (a === b) {
return
}

console.log(cmd.join(' '))
const IO = BuildBaseFileInputOutputModel.superRefine(
transform_input_output_file(a, b),
)

await execPandoc(cmd)
CALL_DOCUMENT[String(`/${a}/convert/${b}`)] = async (
source: any,
) => {
const input = IO.parse(source)
const cmd = buildCommandToConvertDocumentWithCalibre(input)
await execCalibre(cmd)
}
})
})

const IO = BuildBaseFileInputOutputModel.superRefine(
transform_input_output_file('tex', 'pdf'),
(v: any, ctx: RefinementCtx) => {
if (!v.input.file.path.endsWith(`.tex`)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Input path must have extension .tex`,
path: ['input', 'file', 'path'],
})
}

return v
},
)

CALL_DOCUMENT[String(`/tex/convert/pdf`)] = CALL_DOCUMENT[
String(`/latex/convert/pdf`)
] = async source => {
const input = IO.parse(source)
console.log(input)
// console.log(
// await generateLatexPDF({
// ...input,
// }),
// )

const tmpDirectory = await tmp.dir()

const cmd = buildCommandToGenerateLatexPdf({
...input,
output: {
directory: tmpDirectory,
file: {
name: 'document',
},
},
})

await execPdfLatexCommand(cmd)
}

export async function execLatex(cmd: Array<string>) {
Expand Down Expand Up @@ -244,25 +278,24 @@ export async function execPandoc(cmd: Array<string>) {
}
}

export async function buildCommandToGenerateLatexPDF(source: any) {
// const iPath = inputPath ? inputPath : await tmp.tmpName()
// if (!inputPath && input) {
// await fs.writeFile(iPath, input)
// }
// const tmpDirectory = tmp.dirSync()
// const outputPath = `${tmpDirectory.name}/preview.pdf`
// const command = [
// `pdflatex -interaction=nonstopmode -halt-on-error -output-directory ${tmpDirectory.name} -jobname=preview ${iPath}`,
// ]
// try {
// await exec(command.join(' '))
// } catch (e) {
// const message = parseLatexError(e.data.stdout)
// const error = new Error(message)
// error.cause = 'latex'
// throw error
// }
// return outputPath
export async function execCalibre(cmd: Array<string>) {
try {
await exec(cmd.join(' '))
} catch (e) {
throw e
}
}

export async function execPdfLatexCommand(cmd: Array<string>) {
try {
await exec(cmd.join(' '))
} catch (e) {
if (e instanceof ChildProcessError) {
const message = e.data.stdout && parseLatexError(e.data.stdout)
const error = new Error(message ?? e.data.error.message)
throw error
}
}
}

function parseLatexError(text: string) {
Expand Down
7 changes: 1 addition & 6 deletions code/node/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ FONT_FORMAT.forEach(a => {

CALL_FONT[`/${a}/convert/${b}`] = async source => {
const input = IO.parse(source)

const cmd = await buildCommandToConvertFontWithFontForge({
...input,
// inputExtension: a,
// outputExtension: b,
})
const cmd = await buildCommandToConvertFontWithFontForge(input)
await execFontForge(cmd)
}
})
Expand Down
2 changes: 1 addition & 1 deletion code/node/html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Browser, Puppeteer } from 'puppeteer'
import { Browser } from 'puppeteer'
import puppeteer from 'puppeteer-extra'
// add stealth plugin and use defaults (all evasion techniques)
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
Expand Down
17 changes: 3 additions & 14 deletions code/node/image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import _ from 'lodash'
import {
BuildBaseFileInputOutputModel,
BuildCommandToConvertAiToSvgWithInkscape,
BuildCommandToConvertAiToSvgWithInkscapeModel,
BuildCommandToProcessImage,
BuildCommandToProcessImageModel,
CallLink,
IMAGE_MAGICK_INPUT_FORMAT,
IMAGE_MAGICK_OUTPUT_FORMAT,
Expand Down Expand Up @@ -117,10 +114,7 @@ IMAGE_MAGICK_INPUT_FORMAT.forEach(a => {

CALL_IMAGE[`/${a}/convert/${b}`] = async source => {
const input = IO.parse(source)

const cmd = buildCommandToProcessImage({
...input,
})
const cmd = buildCommandToProcessImage(input)
await execImageMagick(cmd)
}
})
Expand All @@ -131,14 +125,9 @@ INKSCAPE_EXPORT_FORMAT.forEach(format => {
transform_input_output_file('ai', format),
)

CALL_IMAGE[`/ai/convert/${format}`] = async (
source: BuildCommandToProcessImage,
) => {
CALL_IMAGE[`/ai/convert/${format}`] = async source => {
const input = IO.parse(source)

const cmd = await buildCommandToConvertAIToSVGWithInkscape({
...input,
})
const cmd = await buildCommandToConvertAIToSVGWithInkscape(input)
await execInkscape(cmd)
}
})
10 changes: 10 additions & 0 deletions code/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { FIND_IMAGE } from './image.js'
import { FIND_FONT } from './font.js'
import { FIND_VIDEO } from './video.js'
import { FIND_DOCUMENT } from './document.js'
import { FIND_CODE } from './code.js'

export * from '../shared/index.js'

export * from './document.js'
export * from './code.js'
export * from './archive.js'
export * from './image.js'
export * from './video.js'
export * from './archive.js'
Expand Down Expand Up @@ -36,6 +39,13 @@ export default async function call(
}
break
}
case 'format': {
const code = FIND_CODE(input)
if (code) {
return code(input)
}
break
}
}

throw new Error(`Action ${task} not yet handled`)
Expand Down
12 changes: 12 additions & 0 deletions code/shared/archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BuildCommandToDecompress7ZModel } from '../shared/index.js'

export async function buildCommandToDecompress7z(source) {
const input = BuildCommandToDecompress7ZModel.parse(source)
return [
`7z`,
`x`,
`"${input.input.directory}"`,
`-o`,
`"${input.output.file.path}"`,
]
}
Loading

0 comments on commit 2640d74

Please sign in to comment.