diff --git a/src/diff.ts b/src/diff/diff.ts similarity index 55% rename from src/diff.ts rename to src/diff/diff.ts index ad1adbd..96b3d66 100644 --- a/src/diff.ts +++ b/src/diff/diff.ts @@ -1,29 +1,32 @@ import { watch } from 'fs'; import { promises } from 'fs'; +import { ContentReferrer, createContentReferrer, getContentRefererConstructor } from '../content/referrer' +import path from 'path'; const readdir = promises.readdir; -type FileDiff = { - name: string, - desc: string -}; export class Watcher{ + private _type: string; private _path:string; - private _added: FileDiff[]; - private _deleted: FileDiff[]; - constructor(path:string){ + /** + * @todo : alter type Map + */ + private _added: ContentReferrer[]; + private _deleted: ContentReferrer[]; + constructor(path:string,type:string){ this._path = path; this._added =[]; - this._deleted =[] + this._deleted =[]; + this._type = type; } - public get added() : FileDiff[] { + public get added() : ContentReferrer[] { return this._added; } /*public set added(diff : FileDiff[]) { this._added = diff; }*/ - public get deleted(): FileDiff[]{ + public get deleted(): ContentReferrer[]{ return this._deleted; } /*public set deleted(diff : FileDiff[]){ @@ -32,7 +35,15 @@ export class Watcher{ public get path(){ return this._path; } - + public get type(){ + return this._type; + } + private createCR(filename: string){ + return createContentReferrer(`${this.path}/${filename}`,this.type); + } + /** + * + */ async setup(initial_filenames:string[]){ const cur = (await readdir(this._path,{ encoding:"utf8", @@ -40,23 +51,25 @@ export class Watcher{ })).filter(x=>x.isFile).map(x=>x.name); let added = cur.filter(x => !initial_filenames.includes(x)); let deleted = initial_filenames.filter(x=>!cur.includes(x)); - this._added = added.map(x=>{return {name:x,desc:""}}); - this._deleted = deleted.map(x=>{return {name:x,desc:""}}); + this._added = added.map(x=>this.createCR(x)); + this._deleted = deleted.map(x=>this.createCR(x)); watch(this._path,{persistent: true, recursive:false},async (eventType,filename)=>{ if(eventType === "rename"){ const cur = (await readdir(this._path,{ encoding:"utf8", withFileTypes: true, })).filter(x=>x.isFile).map(x=>x.name); + //add if(cur.includes(filename)){ - this._added.push({name:filename,desc:""}); + this._added.push(this.createCR(filename)); } else{ - if(this._added.map(x=>x.name).includes(filename)){ - this._added = this._added.filter(x=> x.name !== filename); + //added has one + if(this._added.map(x=>x.path).includes(path.join(this.path,filename))){ + this._added = this._added.filter(x=> x.path !== path.join(this.path,filename)); } else { - this._deleted.push({name:filename,desc:""}); + this._deleted.push(this.createCR(filename)); } } } diff --git a/src/server.ts b/src/server.ts index fc62ab4..ea8da2e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,7 +3,7 @@ import Router from 'koa-router'; import {get_setting} from './setting'; import {connectDB} from './database'; -import {Watcher} from './diff' +import {Watcher} from './diff/diff' import { createReadStream, readFileSync } from 'fs'; import getContentRouter from './route/contents'; @@ -51,6 +51,8 @@ export async function create_server(){ serveindex('/doc/:rest(.*)'); serveindex('/search'); serveindex('/login'); + serveindex('/profile'); + serveindex('/difference'); const static_file_server = (path:string,type:string) => { router.get('/'+path,async (ctx,next)=>{