Skip to content

Commit

Permalink
fix(vscode): Fix total overwriting of host and local settings JSON fi…
Browse files Browse the repository at this point in the history
…les (#4205)

Fix total overwritting of host and local settings json
  • Loading branch information
ccastrotrejo committed Feb 15, 2024
1 parent 15e0ea8 commit 360405e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getOrCreateDesignTimeDirectory,
isDesignTimeUp,
startDesignTimeProcess,
updateProjectPath,
waitForDesignTimeStartUp,
} from '../../utils/codeless/startDesignTimeApi';
import { getFunctionsCommand } from '../../utils/funcCoreTools/funcVersion';
Expand Down Expand Up @@ -41,6 +42,7 @@ export async function startBackendRuntime(projectPath: string): Promise<void> {
if (designTimeDirectory) {
await createJsonFile(designTimeDirectory, hostFileName, hostFileContent);
await createJsonFile(designTimeDirectory, localSettingsFileName, modifiedSettingsFileContent);
await updateProjectPath(designTimeDirectory, localSettingsFileName, projectPath);

const cwd: string = designTimeDirectory.fsPath;
const portArgs = `--port ${ext.designTimePort}`;
Expand Down
30 changes: 25 additions & 5 deletions apps/vs-code-designer/src/app/utils/codeless/startDesignTimeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ export async function startDesignTimeApi(projectPath: string): Promise<void> {
);

const designTimeDirectory: Uri | undefined = await getOrCreateDesignTimeDirectory(designTimeDirectoryName, projectPath);
settingsFileContent.Values[ProjectDirectoryPath] = path.join(projectPath);
settingsFileContent.Values[ProjectDirectoryPath] = projectPath;

if (designTimeDirectory) {
await createJsonFile(designTimeDirectory, hostFileName, hostFileContent);
await createJsonFile(designTimeDirectory, localSettingsFileName, settingsFileContent);
await updateProjectPath(designTimeDirectory, localSettingsFileName, projectPath);
await updateFuncIgnore(projectPath, [`${designTimeDirectoryName}/`]);
const cwd: string = designTimeDirectory.fsPath;
const portArgs = `--port ${ext.designTimePort}`;
Expand Down Expand Up @@ -238,6 +239,14 @@ export async function promptStartDesignTimeOption(context: IActionContext) {
}
}

/**
* Creates a JSON file in the specified directory with the given file name and content.
* If the file already exists, it will not be overwritten.
* @param {Uri} directory - The directory where the file will be created.
* @param {string} fileName - The name of the file to be created.
* @param {hostFileContent | settingsFileContent}fileContent - The content of the file to be created.
* @returns A Promise that resolves when the file is created successfully.
*/
export async function createJsonFile(
directory: Uri,
fileName: string,
Expand All @@ -249,10 +258,21 @@ export async function createJsonFile(
if (!fs.existsSync(filePath.fsPath)) {
await writeFormattedJson(filePath.fsPath, fileContent);
}
// Else merge new settings into existing file
else {
const fileJson = JSON.parse(await fse.readFile(filePath.fsPath, 'utf-8'));
}

/**
* Updates the project path in a settings file.
* @param {Uri} directory - The directory where the settings file is located.
* @param {string} fileName - The name of the settings file.
* @param {string} projectDirectoryPath - The new project directory path to be updated in the settings file.
*/
export async function updateProjectPath(directory: Uri, fileName: string, projectDirectoryPath: string) {
const filePath: Uri = Uri.file(path.join(directory.fsPath, fileName));

await fse.writeFile(filePath.fsPath, JSON.stringify(extend({}, fileJson, fileContent), null, 2), 'utf-8');
// Overwrite file
if (fs.existsSync(filePath.fsPath)) {
const fileJson: typeof settingsFileContent = JSON.parse(await fse.readFile(filePath.fsPath, 'utf-8'));
fileJson.Values[ProjectDirectoryPath] = projectDirectoryPath;
await fse.writeFile(filePath.fsPath, JSON.stringify(extend({}, fileJson), null, 2), 'utf-8');
}
}

0 comments on commit 360405e

Please sign in to comment.