Skip to content

Commit

Permalink
fix(bugs): fix small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
makaanneo committed Oct 27, 2022
1 parent 9b6d51c commit 9ea694c
Show file tree
Hide file tree
Showing 7 changed files with 843 additions and 898 deletions.
25 changes: 15 additions & 10 deletions src/core/archiveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface iArchiveFile {
archiveBaseFolder: string,
dateTime: Date
): Promise<string>;
getFileCount(fileName: string, archiveTarget: string): Promise<number>;
getFileNameNoise(fileName: string, archiveTarget: string): Promise<string>;
}

@injectable()
Expand All @@ -30,10 +30,12 @@ export class archiveFile implements iArchiveFile {
) {
this._settings = settings;
}
async getFileCount(fileName: string, archiveTarget: string): Promise<number> {
let target = path.join(archiveTarget, fileName);
const childs = readdirSync(archiveTarget);
return childs.filter((x) => x === fileName).length;
async getFileNameNoise(
fileName: string,
archiveTarget: string
): Promise<string> {
const currentDate = new Date(Date.now());
return currentDate.getUTCMilliseconds().toString();
}

async archive(file: iRawFile): Promise<string> {
Expand All @@ -48,7 +50,7 @@ export class archiveFile implements iArchiveFile {
await this.archiveFile(file.FullPath, file.Name, target, fileExt);
} catch (e) {
console.error(e);
return;
return undefined;
}
return '';
}
Expand All @@ -59,12 +61,15 @@ export class archiveFile implements iArchiveFile {
archivePath: string,
fileExtension: string
): Promise<string> {
let target = path.join(archivePath, fileName);
const baseName = path.basename(fileName);
const extension = path.extname(baseName);
const clean = baseName.replace(extension, '');
let target = path.join(archivePath, clean);
if (fs.existsSync(target)) {
const newFileName = `${fileName}-(${await this.getFileCount(
fileName,
const newFileName = `${clean}-(${await this.getFileNameNoise(
clean,
archivePath
)}${fileExtension})`;
)})${fileExtension}`;
target = path.join(archivePath, newFileName);
fs.moveSync(sourceFile, target, { overwrite: false });
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/core/defaultFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class defaultFileHandler
Title: file.Name,
Author: '',
CreationDate: fileStats.ctime,
Keywords: null,
Keywords: [],
ModificationDate: fileStats.mtime,
Subject: file.Name
};
Expand Down
5 changes: 3 additions & 2 deletions src/core/joplinApiBc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'reflect-metadata';
import joplin from 'api';
import { basename } from 'path';
import { injectable } from 'inversify';
import path = require('path');
import { iPreparedNote } from './iPreparedNote';
import { joplinResource } from './joplinResource';
import { JoplinNotebook } from './JoplinNotebook';
import path from 'path';

export class joplinNote {
Id: string;
Expand Down Expand Up @@ -90,7 +91,7 @@ export class joplinApiBc implements iJoplinApiBc {
['resources'],
null,
{
title: path.basename(file)
title: basename(file)
},
[
{
Expand Down
6 changes: 5 additions & 1 deletion src/core/joplinTagProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export class joplinTagProcessor implements iJoplinTagProcessor {
const result: Array<joplinTag> = new Array<joplinTag>();
for (let index = 0; index < tags.length; index++) {
const tag = await this._jApi.findTagByName(tags[index]);
console.log(`Retrieved tag: ${tag.Id} and title: ${tag.Title}`);
if (tag !== null) {
console.log(`Retrieved tag: ${tag.Id} and title: ${tag.Title}`);
}
if (tag === null || tag === undefined) {
const newTag = await this._jApi.postTag(tags[index]);
result.push(newTag);
Expand All @@ -44,7 +46,9 @@ export class joplinTagProcessor implements iJoplinTagProcessor {
}

async buildTagsFromSettings(): Promise<Array<string>> {
this._settings.initilize();
const tagsFromSettings = this._settings.Values.tagNewFilesTags;
console.log(`Tags from settings: ${tagsFromSettings}`);
const result = tagsFromSettings.split(/\s*,\s*/);
return result;
}
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { iDirectoryMonitoreWorker } from './worker';
import { myContainer } from './inversify.config';
import { TYPES } from './types';
import { register } from './settings/settings';
import { iAthenaConfiguration } from './settings/athenaConfiguration';
import {
athenaConfiguration,
iAthenaConfiguration
} from './settings/athenaConfiguration';

joplin.plugins.register({
onStart: async () => {
Expand All @@ -26,13 +29,24 @@ joplin.plugins.register({
joplin.settings.onChange(async (event: any) => {
console.log('Settings changed');
if (await settings.verify()) {
await settings.initilize();
await directoryMonitore.removeWatcher();
await directoryMonitore.watchDirectory();
}
});

// register business logic
console.info('Athena plugin start watching!');
myContainer.snapshot();
myContainer.unbind(TYPES.iAthenaConfiguration);
myContainer
.bind<iAthenaConfiguration>(TYPES.iAthenaConfiguration)
.to(athenaConfiguration)
.inSingletonScope();
myContainer.restore();

if (await settings.verify()) {
await directoryMonitore.removeWatcher();
await directoryMonitore.watchDirectory();
}

Expand Down
18 changes: 14 additions & 4 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ let watcher: Array<any> = [];

export interface iDirectoryMonitoreWorker {
watchDirectory(): Promise<void>;
removeWatcher(): Promise<void>;
}

@injectable()
export class directoryMonitoreWorker implements iDirectoryMonitoreWorker {
private _settings: iAthenaConfiguration;
private _jnp: iJoplinNoteProcessor;
private _directoryWatcher: Array<any> = new Array<any>();
constructor(
@inject(TYPES.iAthenaConfiguration) settings: iAthenaConfiguration,
@inject(TYPES.iJoplinNoteProcessor) jnp: iJoplinNoteProcessor
) {
this._settings = settings;
this._jnp = jnp;
}
async removeWatcher(): Promise<void> {
if (this._directoryWatcher.length > 0) {
this._directoryWatcher.forEach(async (element) => {
console.log(`End watching directory: ${element}`);
await element.close();
});
}
}

async watchDirectory(): Promise<void> {
await this._settings.initilize();
Expand All @@ -35,12 +45,12 @@ export class directoryMonitoreWorker implements iDirectoryMonitoreWorker {
this._settings.Values.importPath !== null
) {
const ownObject = this;
if (watcher.length > 0) {
watcher.forEach(async (element) => {
if (this._directoryWatcher.length > 0) {
this._directoryWatcher.forEach(async (element) => {
console.log(`End watching directory: ${element}`);
await element.close();
});
watcher = [];
this._directoryWatcher = [];
}
let chokidarWatcher = null;
try {
Expand All @@ -59,7 +69,7 @@ export class directoryMonitoreWorker implements iDirectoryMonitoreWorker {
} catch (error) {
console.error(error);
}
watcher.push(chokidarWatcher);
this._directoryWatcher.push(chokidarWatcher);
} else {
console.log('Nothing to monitore!');
}
Expand Down
Loading

0 comments on commit 9ea694c

Please sign in to comment.