Skip to content

Commit

Permalink
fix: moddates updated during applylinks, implemented and tested (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosbalasko authored Sep 16, 2023
1 parent 333df1d commit 40ea8a0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/process-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const createResourceFromData = (
}

fs.writeFileSync(absFilePath, data, base64 ? 'base64' : undefined);
utils.setFileDates(absFilePath, note);
utils.setFileDates(absFilePath, note.created, note.updated);

utils.loggerInfo(`data url resource ${fileName} added`);

Expand Down
6 changes: 4 additions & 2 deletions src/utils/apply-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { truncatFileName } from './folder-utils';
import { escapeStringRegexp } from './escape-string-regexp';
import { getAllOutputFilesWithExtension } from './get-all-output-files';
import { isTanaOutput } from './tana/is-tana-output';
import { updateFileContentSafely } from './file-utils';

export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<string>): void => {
const linkNameMap = RuntimePropertiesSingleton.getInstance();
Expand Down Expand Up @@ -62,8 +63,9 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s


if (fileContent !== updatedContent) {
console.log(`replaced output written to: ${notebookFolder}${path.sep}${targetFile}`);
fs.writeFileSync(`${notebookFolder}${path.sep}${targetFile}`, updatedContent);
const filePath = `${notebookFolder}${path.sep}${targetFile}`;
updateFileContentSafely(filePath, updatedContent);

}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/utils/content-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ export const logTags = (note: any): string => {
return undefined;
};

export const setFileDates = (path: string, note: any): void => {
const updated = Moment(note.updated).valueOf();
const mtime = updated / 1000;
export const setFileDates = (path: string, created: any, updated: any): void => {
const updatedMoment = Moment(updated).valueOf();
const mtime = updatedMoment / 1000;
utimesSync(path, mtime, mtime);
// also set creation time where supported
const creationTime = Moment(note.created);
const creationTime = Moment(created);

const created = (Moment('1970-01-01 00:00:00.000').isBefore(creationTime)
const createdMoment = (Moment('1970-01-01 00:00:00.000').isBefore(creationTime)
? creationTime
: Moment('1970-01-01 00:00:00.001')).valueOf();
if (created) {
utimes(path, {btime: created});
if (createdMoment) {
utimes(path, {btime: createdMoment});
}
};

Expand Down
9 changes: 8 additions & 1 deletion src/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ export const writeFile = (absFilePath: string, data: any, note: any): void => {
}
else {
fs.writeFileSync(absFilePath, data);
setFileDates(absFilePath, note);
setFileDates(absFilePath, note.created, note.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)
}
1 change: 1 addition & 0 deletions test/data/updateFileContentSafelyFile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the updated File content
23 changes: 20 additions & 3 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import moment from 'moment';

import * as utils from './../src/utils';
import * as options from './../src/xml-parser.options';
import { updateFileContentSafely } from '../src/utils/file-utils';

describe('SetFileDates', () => {
let content;
Expand All @@ -19,7 +20,7 @@ describe('SetFileDates', () => {
});

it('happy path =» file exists and modified successfully', () => {
utils.setFileDates('./test/data/test-justText.enex', notes['note']);
utils.setFileDates('./test/data/test-justText.enex', notes['note']['created'], notes['note']['updated']);
const fStat = fs.statSync('./test/data/test-justText.enex');
const atime = moment(fStat.atime).format();
const mtime = moment(fStat.mtime).format();
Expand All @@ -32,7 +33,7 @@ describe('SetFileDates', () => {
it('throws an error in case of a missing file', () => {
let errorHappened = false;
try {
utils.setFileDates('./test/data/do_not_exists.enex', notes['note']);
utils.setFileDates('./test/data/do_not_exists.enex', notes['note']['created'], notes['note']['updated']);
} catch (e) {
errorHappened = true;
}
Expand All @@ -41,7 +42,7 @@ describe('SetFileDates', () => {
});
it('set to now if no updated field in note', () => {
notes['note']['updated'] = undefined;
utils.setFileDates('./test/data/test-justText.enex', notes['note']);
utils.setFileDates('./test/data/test-justText.enex', notes['note']['created'], notes['note']['updated']);
const fStat = fs.statSync('./test/data/test-justText.enex');
const atime = moment(fStat.atime);
const mtime = moment(fStat.mtime);
Expand All @@ -53,6 +54,22 @@ describe('SetFileDates', () => {

});

describe('file utils', () => {
it('update file content safely', () => {
const filePath = './test/data/updateFileContentSafelyFile.md';
const origFStat = fs.statSync(filePath);

updateFileContentSafely(filePath, 'this is the updated File content');

const newFStat = fs.statSync(filePath);

assert.deepEqual(origFStat.birthtime, newFStat.birthtime);
assert.deepEqual(origFStat.mtime, newFStat.mtime);

});

})

describe('extensions', () => {
it('no resource-attributes', () => {
const resource = {};
Expand Down

0 comments on commit 40ea8a0

Please sign in to comment.