Skip to content

Commit

Permalink
fix: update project config
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanurazvan committed Apr 6, 2023
1 parent 0b6d979 commit 9147cde
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {useCallback, useEffect, useMemo, useState} from 'react';

import {Button, Input, Select} from 'antd';

import _ from 'lodash';
import _, {cloneDeep} from 'lodash';
import {v4 as uuidv4} from 'uuid';

import {HELM_INSTALL_OPTIONS_DOCS_URL, HELM_TEMPLATE_OPTIONS_DOCS_URL} from '@constants/constants';
Expand Down Expand Up @@ -161,7 +161,7 @@ const PreviewConfigurationEditor = () => {
valuesFileItemMap,
};

const updatedPreviewConfigurationMap = JSON.parse(JSON.stringify(previewConfigurationMap));
const updatedPreviewConfigurationMap = cloneDeep(previewConfigurationMap);
updatedPreviewConfigurationMap[input.id] = input;

dispatch(
Expand All @@ -182,14 +182,15 @@ const PreviewConfigurationEditor = () => {
}
},
[
dispatch,
helmChart,
name,
previewConfiguration,
helmCommand,
helmOptions,
previewConfigurationMap,
previewConfiguration,
helmChart,
valuesFileItemMap,
previewConfigurationMap,
dispatch,
projectConfigRef,
]
);

Expand Down
2 changes: 1 addition & 1 deletion src/redux/appConfig/appConfig.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export const configSlice = createSlice({

if (
!action.payload.fromConfigFile &&
(keys.length > 0 || !existsSync(CONFIG_PATH(state.selectedProjectRootFolder)))
(keys.length > 0 || deletedKeys.length > 0 || !existsSync(CONFIG_PATH(state.selectedProjectRootFolder)))
) {
writeProjectConfigFile(state);
}
Expand Down
7 changes: 6 additions & 1 deletion src/redux/services/projectConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {readFileSync, writeFileSync} from 'fs';
import {readFileSync, statSync, writeFileSync} from 'fs';
import _, {isArray, mergeWith} from 'lodash';
import log from 'loglevel';
import {sep} from 'path';
Expand All @@ -10,6 +10,7 @@ import {updateProjectConfig} from '@redux/appConfig';
import {K8S_VERSIONS, PREDEFINED_K8S_VERSION} from '@shared/constants/k8s';
import {AppConfig, ProjectConfig} from '@shared/models/config';
import {isEqual} from '@shared/utils/isEqual';
import {updateProjectConfigTimestamp} from '@shared/utils/projectConfig';

export interface SerializableObject {
[name: string]: any;
Expand Down Expand Up @@ -38,6 +39,8 @@ export const writeProjectConfigFile = (state: AppConfig | SerializableObject) =>
} else {
writeFileSync(absolutePath, ``, 'utf-8');
}

updateProjectConfigTimestamp(statSync(absolutePath).mtimeMs);
} catch (error) {
log.error(error);
}
Expand Down Expand Up @@ -119,6 +122,7 @@ export const readProjectConfig = (projectRootPath?: string | null): ProjectConfi
savedCommandMap,
}: ProjectConfig = JSON.parse(readFileSync(CONFIG_PATH(projectRootPath), 'utf8'));
const projectConfig: ProjectConfig = {};

projectConfig.settings = settings
? {
helmPreviewMode: _.includes(['template', 'install'], settings.helmPreviewMode)
Expand Down Expand Up @@ -174,6 +178,7 @@ export const updateProjectSettings = (dispatch: (action: AnyAction) => void, pro
dispatch(updateProjectConfig({config: projectConfig, fromConfigFile: true}));
return;
}

dispatch(updateProjectConfig({config: null, fromConfigFile: true}));
};

Expand Down
14 changes: 13 additions & 1 deletion src/redux/services/projectConfigMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {FSWatcher, watch} from 'chokidar';
import {statSync} from 'fs';
import log from 'loglevel';

import {AppDispatch} from '@shared/models/appDispatch';
import {getProjectConfigTimestamp} from '@shared/utils/projectConfig';

import {CONFIG_PATH, updateProjectSettings} from './projectConfig';

Expand Down Expand Up @@ -30,9 +32,19 @@ export function monitorProjectConfigFile(dispatch: AppDispatch, filePath?: strin

watcher
.on('change', () => {
updateProjectSettings(dispatch, filePath);
if (!filePath) {
return;
}

if (statSync(absolutePath).mtimeMs !== getProjectConfigTimestamp()) {
updateProjectSettings(dispatch, filePath);
}
})
.on('unlink', () => {
if (!filePath) {
return;
}

updateProjectSettings(dispatch, filePath);
})

Expand Down
24 changes: 15 additions & 9 deletions src/redux/thunks/previewConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ export const deletePreviewConfiguration = createAsyncThunk<void, string, {dispat
}

const previewConfigurationMapCopy = cloneDeep(previewConfigurationMap);
delete previewConfigurationMapCopy[previewConfigurationId];

const updatedHelmConfig = {
helm: {
previewConfigurationMap: previewConfigurationMapCopy,
},
};

thunkAPI.dispatch(updateProjectConfig({config: updatedHelmConfig, fromConfigFile: false}));
previewConfigurationMapCopy[previewConfigurationId] = null;

const projectConfig = state.config.projectConfig;

thunkAPI.dispatch(
updateProjectConfig({
config: {
...projectConfig,
helm: {
previewConfigurationMap: previewConfigurationMapCopy,
},
},
fromConfigFile: false,
})
);
}
);
9 changes: 9 additions & 0 deletions src/shared/utils/projectConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let PROJECT_CONFIG_TIMESTAMP = 0;

export function getProjectConfigTimestamp(): number {
return PROJECT_CONFIG_TIMESTAMP;
}

export function updateProjectConfigTimestamp(timestamp: number): void {
PROJECT_CONFIG_TIMESTAMP = timestamp;
}

0 comments on commit 9147cde

Please sign in to comment.