Skip to content

Commit

Permalink
perf: support cache text file
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjs committed Aug 4, 2024
1 parent 616b70c commit bb62b5f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
// "--enexSources", "${env:DROPBOX}${/}Notes${/}yarle-test${/}test_list2.enex",
// "--enexSources", "${env:DROPBOX}${/}Notes${/}yarle-test${/}test_delete_line.enex",
],
"env": {
"YARLE_TEXT_CACHE": "true"
},
"preLaunchTask": "npm: build:watch",
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/dropTheRope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import * as dropTheRopeRunner from './dropTheRopeRunner';
import { profileFn, timeFn } from './utils/logger';

// profileFn(() => dropTheRopeRunner.run({}))
// timeFn(() => dropTheRopeRunner.run({}))
dropTheRopeRunner.run({});
timeFn(() => dropTheRopeRunner.run({}))
// dropTheRopeRunner.run({});
5 changes: 3 additions & 2 deletions src/utils/apply-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LanguageFactory } from './../outputLanguages/LanguageFactory';
import { OutputFormat } from './../output-format';
import { isTOC } from './is-toc';
import { replaceBackSlashes, replaceBracketsForWikiLink } from './turndown-rules';
import { fsc } from './cached-files';

export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
Expand Down Expand Up @@ -60,7 +61,7 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s
return path.extname(file).toLowerCase() === extension;
});
for (const targetFile of targetFiles) {
const fileContent = fs.readFileSync(`${notebookFolder}${path.sep}${targetFile}`, 'utf-8');
const fileContent = fsc.readFileSync(`${notebookFolder}${path.sep}${targetFile}`, 'utf-8');
let updatedContent = fileContent;
if (isTanaOutput()) {
const tanaNote = JSON.parse(updatedContent)
Expand Down Expand Up @@ -124,7 +125,7 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s
const unrecognizable = "Unrecognizable";

for (const targetFile of allconvertedFiles) {
const fileContent = fs.readFileSync(targetFile, 'utf-8');
const fileContent = fsc.readFileSync(targetFile, 'utf-8');

// TODO APPLY EVERNOTE LINK
const evernoteInternalLinkPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_LINK_PLACEHOLDER>', 'g');
Expand Down
27 changes: 27 additions & 0 deletions src/utils/cached-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as fs from 'fs'

class CachedTextFiles {
private caches = new Map<string, string>
constructor(
) { }

readFileSync(path: string, encoding: 'utf-8') {
if (this.caches.has(path)) {
return this.caches.get(path)
}
const text = fs.readFileSync(path, encoding)
this.caches.set(path, text)
return text
}
writeFileSync(path: string, text: string) {
fs.writeFileSync(path, 'utf8')
this.caches.set(path, text)
}
appendFileSync(path: string, text: string) {
const currentText = this.readFileSync(path, 'utf-8')
fs.appendFileSync(path, text)
this.caches.set(path, currentText + text)
}
}

export const fsc = process.env.YARLE_TEXT_CACHE === 'true' ? new CachedTextFiles() : fs
52 changes: 26 additions & 26 deletions src/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ import { setFileDates } from './content-utils';
import { logger } from './../utils/logger';
import { yarleOptions } from './../yarle';
import { isLogseqJournal } from './is-logseq-journal';
import {getCreationTime} from './content-utils'
import { getCreationTime } from './content-utils'
import { EvernoteNoteData } from './../models';
import { resolve } from 'path';
import { fsc } from './cached-files';

export const writeFile = (absFilePath: string, noteContent: string, pureNoteData: EvernoteNoteData): void => {
try {
if (isLogseqJournal(yarleOptions) && fs.existsSync(absFilePath)){
// put the title as a first line
const compoundJournalNoteTitle = `# Journal Note for ${getCreationTime(pureNoteData)}`
const currentContent = fs.readFileSync(absFilePath, 'utf-8')
if (!currentContent.startsWith(compoundJournalNoteTitle)){
const updatedContent = `${compoundJournalNoteTitle}\n\n${currentContent}`
fs.writeFileSync(absFilePath, updatedContent)
}
try {
if (isLogseqJournal(yarleOptions) && fs.existsSync(absFilePath)) {
// put the title as a first line
const compoundJournalNoteTitle = `# Journal Note for ${getCreationTime(pureNoteData)}`
const currentContent = fsc.readFileSync(absFilePath, 'utf-8')
if (!currentContent.startsWith(compoundJournalNoteTitle)) {
const updatedContent = `${compoundJournalNoteTitle}\n\n${currentContent}`
fsc.writeFileSync(absFilePath, updatedContent)
}

fs.appendFileSync(absFilePath, noteContent)
fsc.appendFileSync(absFilePath, noteContent)

}
else {
fs.writeFileSync(absFilePath, noteContent);
setFileDates(absFilePath, pureNoteData.created, pureNoteData.updated);
}
} catch (e) {
// tslint:disable-next-line: no-console
logger.error('Cannot write file ', e);
throw e;
} else {
fsc.writeFileSync(absFilePath, noteContent);
setFileDates(absFilePath, pureNoteData.created, pureNoteData.updated);
}
};
} catch (e) {
// tslint:disable-next-line: no-console
logger.error('Cannot write file ', e);
throw e;
}
};

export const updateFileContentSafely = (filePath: string, updatedContent: string): void => {
const {birthtime, mtime} = fs.statSync(filePath)
console.log(`replaced output written to: ${filePath}`);
fs.writeFileSync(filePath, updatedContent);
setFileDates(filePath, birthtime, mtime)
export const updateFileContentSafely = (filePath: string, updatedContent: string): void => {
const { birthtime, mtime } = fs.statSync(filePath)
console.log(`replaced output written to: ${filePath}`);
fsc.writeFileSync(filePath, updatedContent);
setFileDates(filePath, birthtime, mtime)
}

export function* recursiveReaddirSync(dir: string): Generator<string> {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export const logger = createLogger({
});


export async function profileFn<R>(block: () => Promise<R>, label?: string) {
console.profile(label)
export async function profileFn<R>(block: () => Promise<R>) {
console.profile()
return block()
.then((r) => {
console.profileEnd(label)
console.profileEnd()
return r
})
}
Expand Down
7 changes: 4 additions & 3 deletions src/utils/tana/create-tana-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getAllOutputFilesWithExtension } from "../get-all-output-files";
import { NodeType, TanaIntermediateAttribute, TanaIntermediateFile, TanaIntermediateNode } from "./types";
import { createNewTanaFile } from './create-new-tana-file';
import { createTanaNode } from './create-tana-node';
import { fsc } from '../cached-files';
const tanaNoteFileName = 'notes-in-TIF.json';

export const createTanaOutput = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
Expand All @@ -15,7 +16,7 @@ export const createTanaOutput = (options: YarleOptions, outputNotebookFolders: A
for (const convertedFile of allconvertedFiles){
// load and parse mergedTanaNotes, or create if
const mergedNotes = getMergedTanaNotes(options)
const convertedTanaNote = JSON.parse(fs.readFileSync(convertedFile, 'utf-8'))
const convertedTanaNote = JSON.parse(fsc.readFileSync(convertedFile, 'utf-8'))

updateMergedNotes(mergedNotes, convertedTanaNote)
saveMergedTanaNotes(options, mergedNotes)
Expand Down Expand Up @@ -113,13 +114,13 @@ const getMergedTanaNotes = (options: YarleOptions): TanaIntermediateFile => {

let mergedTanaNote
try {
mergedTanaNote = JSON.parse(fs.readFileSync(`${options.outputDir}/${tanaNoteFileName}`, 'utf-8'))
mergedTanaNote = JSON.parse(fsc.readFileSync(`${options.outputDir}/${tanaNoteFileName}`, 'utf-8'))
}catch(error){
mergedTanaNote = createNewTanaFile()
}
return mergedTanaNote

}
const saveMergedTanaNotes = (options: YarleOptions, mergedNotes: TanaIntermediateFile): void => {
fs.writeFileSync(`${options.outputDir}/${tanaNoteFileName}`, JSON.stringify(mergedNotes))
fsc.writeFileSync(`${options.outputDir}/${tanaNoteFileName}`, JSON.stringify(mergedNotes))
}
5 changes: 3 additions & 2 deletions src/yarle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { TaskOutputFormat } from './task-output-format';
import { isTanaOutput } from './utils/tana/is-tana-output';
import { LanguageFactory } from './outputLanguages/LanguageFactory';
import { EvernoteNoteData } from './models';
import { fsc } from './utils/cached-files';

export const defaultYarleOptions: YarleOptions = {
enexSources: ['notebook.enex'],
Expand Down Expand Up @@ -146,7 +147,7 @@ export const parseStream = async (options: YarleOptions, enexSource: string): Pr
for (const task of Object.keys(tasks)) {

const taskPlaceholder = `<YARLE-EN-V10-TASK>${task}</YARLE-EN-V10-TASK>`
const fileContent = fs.readFileSync(currentNotePath, 'utf-8');
const fileContent = fsc.readFileSync(currentNotePath, 'utf-8');
const sortedTasks = new Map([...tasks[task]].sort());

let updatedContent = fileContent.replace(taskPlaceholder, [...sortedTasks.values()].join('\n'));
Expand All @@ -155,7 +156,7 @@ export const parseStream = async (options: YarleOptions, enexSource: string): Pr
const language = languageFactory.createLanguage(yarleOptions.outputFormat)
updatedContent = language.tagProcess(fileContent, sortedTasks, taskPlaceholder, updatedContent)

fs.writeFileSync(currentNotePath, updatedContent);
fsc.writeFileSync(currentNotePath, updatedContent);

}
}
Expand Down

0 comments on commit bb62b5f

Please sign in to comment.