Skip to content

Commit

Permalink
Avoid extension overwrite app.json in expo application (#1849)
Browse files Browse the repository at this point in the history
* Add log

* Update

* Update
  • Loading branch information
EzioLi01 committed Oct 31, 2022
1 parent 3b4a4a7 commit 7888898
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import * as path from "path";
import json5 = require("json5");
import { ChildProcess } from "./node/childProcess";
import { HostPlatform } from "./hostPlatform";
import customRequire from "./customRequire";
import stripJsonComments = require("strip-json-comments");
import { parse } from "json5";

export function removeModuleFromRequireCacheByName(moduleName: string): void {
const moduleKey = Object.keys(customRequire.cache).find(key => key.includes(moduleName));
Expand Down Expand Up @@ -69,6 +70,11 @@ function padZeroes(minDesiredLength: number, numberToPad: string): string {
export function stripJsonTrailingComma(str: string): any {
const endOfStringTrailingCommaRegex = /,\s*$/g;
const result = str.replace(endOfStringTrailingCommaRegex, "");
const objResult = json5.parse(result);
let objResult;
try {
objResult = parse(result);
} catch {
objResult = JSON.parse(stripJsonComments(str));
}
return objResult;
}
6 changes: 5 additions & 1 deletion src/extension/exponent/exponentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as semver from "semver";
import * as vscode from "vscode";
import { sync as globSync } from "glob";
import * as nls from "vscode-nls";
import { logger } from "vscode-debugadapter";
import { stripJsonTrailingComma, getNodeModulesGlobalPath } from "../../common/utils";
import { Package, IPackageInformation } from "../../common/node/package";
import { ProjectVersionHelper } from "../../common/projectVersionHelper";
Expand Down Expand Up @@ -309,9 +310,11 @@ require('${entryPoint}');`;
private async patchAppJson(isExpo: boolean = true): Promise<void> {
let appJson: AppJson;
try {
logger.log("Reading app.json file.");
appJson = await this.readAppJson();
} catch {
// if app.json doesn't exist but it's ok, we will create it
// If app.json doesn't exist, we will create it
logger.log("Cannot get existing app.json file. Create new one.");
appJson = <AppJson>{};
}
const packageName = await this.getPackageName();
Expand Down Expand Up @@ -442,6 +445,7 @@ require('${entryPoint}');`;

private async readAppJson(): Promise<AppJson> {
const appJsonPath = this.pathToFileInWorkspace(APP_JSON);
logger.log(`Getting app.json path: ${appJsonPath}`);
return this.fs.readFile(appJsonPath).then(content => {
return stripJsonTrailingComma(content.toString());
});
Expand Down

0 comments on commit 7888898

Please sign in to comment.