Skip to content

Commit

Permalink
Merge pull request #7 from joon610/v1.0.3
Browse files Browse the repository at this point in the history
v1.0.3  Auto Restart Server when json file changed
  • Loading branch information
joon610 authored Jan 31, 2020
2 parents 520f9cc + eb0a7c3 commit 2f4adeb
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mockup-server",
"version": "1.0.2",
"version": "1.0.3",
"private": true,
"scripts": {
"build": "vue-cli-service build",
Expand Down
5 changes: 3 additions & 2 deletions src/components/Logger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import fs from 'fs';
export default class ComponentName extends Vue {
private updated() {
this.$nextTick(() => this.scrollToEnd());;
}
}
private scrollToEnd() {
private scrollToEnd() {
(this.$refs.logConsole as HTMLElement).scrollTop =
(this.$refs.logStart as HTMLElement).offsetHeight;
}
Expand Down Expand Up @@ -78,6 +78,7 @@ export default class ComponentName extends Vue {
}
private openLogFile() {
const shell = require("electron").shell;
const result = fs.existsSync(this.$store.getters.rootPath+REQUEST_PARAM_JSON);
if(result){
Expand Down
2 changes: 1 addition & 1 deletion src/const/mockConst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = '1.0.2';
export const VERSION = '1.0.3';
export const STANDARD_PORT = '9000';
export const INDEX_JSON = '/index.json';
export const ERROR_JSON = '/error.json';
Expand Down
27 changes: 23 additions & 4 deletions src/utils/filetreeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiInfo } from '@/const/mockType';
import { INDEX_JSON, ERROR_JSON, SETTING_JSON } from '@/const/mockConst';

import MockupServer from '@/utils/server/mockupServer';
import store from '../store';
const { remote } = window.require('electron');
const fs = remote.require('fs');

Expand All @@ -20,6 +21,8 @@ export default class FiletreeUtils {

private apiList: ApiInfo[] = [];

private apiHashMap: any = {};

public getInstance(): FiletreeUtils {
if (FiletreeUtils.instance === undefined) {
FiletreeUtils.instance = new FiletreeUtils();
Expand All @@ -43,13 +46,14 @@ export default class FiletreeUtils {
}

private makeApiList(): ApiInfo[] {
const apiList = this.dirPathList.map((value: string) => {
const apiList = this.dirPathList.map((value: string,cnt:number) => {
const api = value.replace(this.rootPath, '');

const indexPath = this.rootPath + api + INDEX_JSON;
this.apiHashMap[indexPath] = cnt;
console.log(indexPath);
const errorPath = this.rootPath + api + ERROR_JSON;
const settingPath = this.rootPath + api + SETTING_JSON;

const settingInfo = this.readJson(settingPath);
const indexJson = this.readJson(indexPath)
const errorJson = this.readJson(errorPath);
Expand All @@ -62,12 +66,27 @@ export default class FiletreeUtils {
apiInfo.cookies = settingInfo?.cookies;
apiInfo.description = settingInfo?.description;
apiInfo.dynamicRoute = settingInfo?.dynamicRoute;


this.fileWatch(indexPath);
return apiInfo;
});
return apiList;
}

private fileWatch(tragetDir:string){
let watcherId:any = undefined;
try{
fs.watch(tragetDir, (eventType:any, filename:any) => {
clearTimeout(watcherId)
watcherId = setTimeout(() => {
store.state.apiInfoList = FiletreeUtils.instance.makeApiList();
}, 300);
});
}catch {
console.log('empty file');
}
}

private readJson(indexPath: string) {
try {
const readJson = JSON.parse(fs.readFileSync(indexPath));
Expand Down
17 changes: 8 additions & 9 deletions src/utils/server/mockupServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiInfo } from '@/const/mockType';
import { GET,PUT,DELETE,POST, COLOR_PALLET } from '@/const/mockConst';
import bodyParser from 'body-parser';
import JsonLogic from './jsonLogic';
import store from '../../store';
import cors from 'cors';
import { initLogObject, saveLogInfo, addLogHistroy } from './logLogic';
const app = express();
Expand All @@ -23,10 +24,9 @@ export default class MockupServer {

private self: any;

public constructor(vueComponent: any, serverPort: string) {
public constructor(serverPort: string) {
this.port = serverPort;
this.self = vueComponent;
this.restfullList = this.self.$store.state.apiInfoList;
this.restfullList = store.state.apiInfoList;
initLogObject(this.self);
}

Expand All @@ -38,7 +38,6 @@ export default class MockupServer {
this.server = app.listen(this.port, () => {
console.log(`server started at http://localhost:${this.port}`);
});

this.generateAPI();
return true;
}
Expand All @@ -52,7 +51,7 @@ export default class MockupServer {
});

delete this.server;
this.self.$store.commit('apiInfoList', []);
store.commit('apiInfoList', []);
return false;
}

Expand All @@ -64,7 +63,7 @@ export default class MockupServer {
}

private generateAPI(): void {
this.self.$store.state.apiInfoList.forEach(
store.state.apiInfoList.forEach(
(restful: ApiInfo, cnt: number) => {
try {
this.getApi(restful, cnt);
Expand All @@ -86,8 +85,8 @@ export default class MockupServer {
restful.status === 'success'
? this.jsonLogic.postData(req, restful)
: restful.error;
this.self.$store.state.apiInfoList[cnt].index = result;
// res = this.setHeader(res, restful);

store.state.apiInfoList[cnt]!.index = result;
saveLogInfo(restful.api,req.body);
addLogHistroy(restful.api,POST,req.body);
res.send(result);
Expand Down Expand Up @@ -146,7 +145,7 @@ export default class MockupServer {
private getApi(restful: ApiInfo, cnt: number): void {
app.get(restful.api, (req: any, res: any) => {
const result = this.jsonLogic.getJson(
this.self.$store.state.apiInfoList[cnt],
store.state.apiInfoList[cnt],
);
// res = this.setHeader(res, restful);
saveLogInfo(restful.api, req.body);
Expand Down
2 changes: 1 addition & 1 deletion src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class MockServer extends Vue {
private async startServer(): Promise<void> {
this.writePort(this.rootPath,this.portNum);
await this.makeFileTree();
this.server = new MockupServer(this, this.portNum);
this.server = new MockupServer(this.portNum);
this.isServerOn = await this.server.start();
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strictNullChecks": false,
"sourceMap": true,
"baseUrl": ".",
"types": [
Expand Down

0 comments on commit 2f4adeb

Please sign in to comment.