Skip to content

Commit

Permalink
Added quick-replace method once initially built + various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Clarke committed May 16, 2022
1 parent e7cc6ff commit 89d2d83
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 62 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ There is a `--debug` argument that adds extra logs and outputs a debug `.json` f

## Issues

The XML parsing lib isn't very performant once files get above the 1mb mark. I've not been able to find and good alternatives for deno as of now.
The XML parsing lib isn't very performant once files get above the 1mb mark. I've not been able to find and good alternatives for deno as of now.


---

## To-do

If a script file is modified while XML re-encoding is in-progress, it should cancel and start over (taking into account change that was currently being processed + new change).
73 changes: 67 additions & 6 deletions src/fileHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function decodeToscFile(filePath: string) {
return decodedContent
}

export function parseToscXML(xmlString: string, fileSize?: number): ToscDoc {
export function parseToscXML({ xmlString, fileSize }: { xmlString: string; fileSize?: number }): ToscDoc {
console.log('⏱ Parsing XML...')
stopwatchTick()
const total = fileSize || new Blob([xmlString]).size
Expand All @@ -59,14 +59,22 @@ export function parseToscXML(xmlString: string, fileSize?: number): ToscDoc {
}
}

export async function writeDebugFiles(fileDir: string, fileName: string, parsedProject: ToscDoc) {
export async function writeDebugFiles({
parsedProject,
projectFileDir,
projectFileName,
}: {
projectFileDir: string
projectFileName: string
parsedProject: ToscDoc
}) {
stopwatchTick()
await Deno.writeTextFile(fileDir + fileName + '_DEBUG.json', JSON.stringify(parsedProject, null, 2))
await Deno.writeTextFile(projectFileDir + projectFileName + '_DEBUG.json', JSON.stringify(parsedProject, null, 2))
console.log(`βœ… Wrote to JSON file for debugging (took ${stopwatchTick()} ms)`)

stopwatchTick()
await Deno.writeTextFile(
fileDir + fileName + '_DEBUG.tosc',
projectFileDir + projectFileName + '_DEBUG.tosc',
encodeXml(parsedProject as any, { replacer: cDataRestorer })
)
console.log(`βœ… Wrote to XML file for debugging (took ${stopwatchTick()} ms)`)
Expand All @@ -79,7 +87,17 @@ const cDataRestorer: StringifierOptions['replacer'] = ({ key, value, tag }) =>
: value

export let lastEncodeTime = 0
export async function writeProjectFile(parsedProject: ToscDoc, fileDir: string, fileName: string, fileSize?: number) {
export async function writeProjectFile({
parsedProject,
projectFileDir,
projectFileName,
fileSize,
}: {
parsedProject: ToscDoc
projectFileDir: string
projectFileName: string
fileSize?: number
}) {
console.log('πŸ“ Re-encoding to XML...')
const progress = new ProgressBar({
total: fileSize || lastEncodeTime,
Expand All @@ -99,8 +117,51 @@ export async function writeProjectFile(parsedProject: ToscDoc, fileDir: string,
if (fileSize) lastEncodeTime = Date.now() - initialTime
console.log(`βœ… XML encoding done (took ${stopwatchTick()} ms)`)
console.log('⏱ Writing file...')
const newFileName = fileDir + fileName + '_INJECTED.tosc'
const newFileName = projectFileDir + projectFileName + '_INJECTED.tosc'
await Deno.writeTextFile(newFileName, xmlString)
console.log(`βœ… Project file written (took ${stopwatchTick()} ms)`)
console.log(newFileName)
}

export async function findReplaceScriptQuick({
oldScript,
newScript,
projectFileDir,
projectFileName,
}: {
oldScript: string
newScript: string
projectFileDir: string
projectFileName: string
}) {
const projectFile = projectFileDir + projectFileName + '_INJECTED.tosc'
const content = await (async () => {
try {
return await Deno.readTextFile(projectFile)
} catch (e) {
return false
}
})()
if (!content) return false

// the xml formatting process removes indentation within the script
// so we need to do the same in order for the find-replace to work
const formattedOldScript = oldScript.replace(/^[^\n]\s+/gm, '').trim()

let search = ''
if (content.indexOf(oldScript) >= 0) search = oldScript
else if (content.indexOf(formattedOldScript) >= 0) search = formattedOldScript
else return false

let replacements = 0
const newContent = content.replaceAll(search, () => {
replacements++
return newScript
})
try {
await Deno.writeTextFile(projectFile, newContent)
} catch (error) {
return false
}
return replacements
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ async function handleProjectFileChange() {
throw 'pls stop'
}

async function letsGo(filePath: string, scriptsDir: string) {
async function letsGo(projectFilePath: string, scriptsDir: string) {
const parsedProject = await (async () => {
try {
return await processToscFile(filePath, scriptsDir, debugMode)
return await processToscFile({ projectFilePath, scriptsDir, debugMode })
} catch (e) {
console.log(e)
return false
Expand All @@ -61,8 +61,8 @@ async function letsGo(filePath: string, scriptsDir: string) {
console.log('\nπŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰\n')

await Promise.all([
startScriptsWatcher(parsedProject, filePath, scriptsDir),
startToscFileWatcher(filePath, handleProjectFileChange),
startScriptsWatcher({ parsedProject, projectFilePath, scriptsDir }),
startToscFileWatcher({ projectFilePath, callback: handleProjectFileChange }),
])
}

Expand Down
Loading

0 comments on commit 89d2d83

Please sign in to comment.