move diff

This commit is contained in:
monoid 2021-01-11 20:47:55 +09:00
parent f887ce6a9b
commit 051382fe18
2 changed files with 33 additions and 18 deletions

View File

@ -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<string,ContentReferrer>
*/
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));
}
}
}

View File

@ -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)=>{