Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiple JSON files for one environment #1099

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const EnvironmentDefault: Environment = {
passphrase: ''
},
cors: true,
routesFolder: false,
headers: [],
proxyReqHeaders: [],
proxyResHeaders: [],
Expand Down Expand Up @@ -349,6 +350,9 @@ export const EnvironmentSchema = Joi.object<Environment, true>({
.required(),
tlsOptions: TLSOptionsSchema,
cors: Joi.boolean().failover(EnvironmentDefault.cors).required(),
routesFolder: Joi.boolean()
.failover(EnvironmentDefault.routesFolder)
.required(),
headers: Joi.array()
.items(HeaderSchema, Joi.any().strip())
.failover(EnvironmentDefault.headers)
Expand Down
1 change: 1 addition & 0 deletions packages/commons/src/models/environment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type Environment = {
proxyResHeaders: Header[];
tlsOptions: EnvironmentTLSOptions;
cors: boolean;
routesFolder: boolean;
headers: Header[];
data: DataBucket[];
};
Expand Down
48 changes: 40 additions & 8 deletions packages/desktop/src/main/libs/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import {
} from '@mockoon/commons-server';
import {
BrowserWindow,
Menu,
clipboard,
dialog,
ipcMain,
Menu,
shell
} from 'electron';
import { getDataPath } from 'electron-json-storage';
import { promises as fsPromises } from 'fs';
import { existsSync as fsExistsSync, promises as fsPromises } from 'fs';
import { createServer } from 'http';
import { lookup as mimeTypeLookup } from 'mime-types';
import {
dirname as pathDirname,
format as pathFormat,
join as pathJoin,
parse as pathParse
Expand Down Expand Up @@ -137,18 +138,49 @@ export const initIPCListeners = (mainWindow: BrowserWindow) => {

ipcMain.handle('APP_GET_OS', () => process.platform);

ipcMain.handle(
'APP_READ_ENVIRONMENT_DATA',
async (event, path: string) => await readJSONData(path)
);
ipcMain.handle('APP_READ_ENVIRONMENT_DATA', async (event, path: string) => {
const root = await readJSONData(path);
if (!root.routesFolder) {
return root;
}

const routesFolderPath = pathJoin(pathDirname(path), './routes');

if (fsExistsSync(routesFolderPath)) {
const routeFiles = await fsPromises.readdir(routesFolderPath);

const routes = await Promise.all(
routeFiles
.map((file) => readJSONData(pathJoin(routesFolderPath, file)))
.filter(Boolean)
);

return { ...root, routes };
}

return root;
});

ipcMain.handle(
'APP_WRITE_ENVIRONMENT_DATA',
async (event, data, path: string, storagePrettyPrint?: boolean) => {
unwatchEnvironmentFile(data.uuid);

await writeJSONData(data, path, storagePrettyPrint);

if (!data.routesFolder) {
await writeJSONData(data, path, storagePrettyPrint);
} else {
const { routes, ...restData } = data;
const routesFolderPath = pathJoin(pathDirname(path), './routes');
await writeJSONData(restData, path, storagePrettyPrint);
routes.forEach(async (route: any) => {
const name = route.endpoint.replace(/\//g, '-');
await writeJSONData(
route,
pathJoin(routesFolderPath, `${name}.json`),
storagePrettyPrint
);
});
}
watchEnvironmentFile(data.uuid, path);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,26 @@
></app-svg>
</div>
</div>

<div class="input-group">
<div class="custom-control custom-checkbox">
<input
type="checkbox"
class="custom-control-input"
id="env-settings-routesFolder"
formControlName="routesFolder"
/>
<label class="custom-control-label" for="env-settings-routesFolder"
>Save each route as JSON
</label>
</div>
<div class="input-group-append ml-1">
<app-svg
icon="info"
class="ml-1"
ngbTooltip="Mockoon will save each route as a JSON file in the routes folder"
></app-svg>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Environment, EnvironmentDefault } from '@mockoon/commons';
import { merge, Observable, Subject } from 'rxjs';
import { Observable, Subject, merge } from 'rxjs';
import { filter, map, takeUntil, tap } from 'rxjs/operators';
import { ToggleItems } from 'src/renderer/app/models/common.model';
import { DialogsService } from 'src/renderer/app/services/dialogs.service';
Expand Down Expand Up @@ -93,7 +93,8 @@ export class EnvironmentSettingsComponent implements OnInit, OnDestroy {
endpointPrefix: [EnvironmentDefault.endpointPrefix],
latency: [EnvironmentDefault.latency],
tlsOptions: this.tlsOptionsFormGroup,
cors: [EnvironmentDefault.cors]
cors: [EnvironmentDefault.cors],
routesFolder: [EnvironmentDefault.routesFolder]
});

// send new activeEnvironmentForm values to the store, one by one
Expand Down Expand Up @@ -133,7 +134,8 @@ export class EnvironmentSettingsComponent implements OnInit, OnDestroy {
endpointPrefix: activeEnvironment.endpointPrefix,
latency: activeEnvironment.latency,
tlsOptions: activeEnvironment.tlsOptions,
cors: activeEnvironment.cors
cors: activeEnvironment.cors,
routesFolder: activeEnvironment.routesFolder
},
{ emitEvent: false }
);
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/renderer/app/stores/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
StoreType
} from 'src/renderer/app/models/store.model';
import { Toast } from 'src/renderer/app/models/toasts.model';
import { Actions, ActionTypes } from 'src/renderer/app/stores/actions';
import { ActionTypes, Actions } from 'src/renderer/app/stores/actions';
import {
findRouteFolderHierarchy,
getBodyEditorMode,
Expand Down Expand Up @@ -565,7 +565,8 @@ export const environmentReducer = (
'proxyRemovePrefix',
'tlsOptions',
'hostname',
'cors'
'cors',
'routesFolder'
];

newState = {
Expand Down