diff --git a/package-lock.json b/package-lock.json index 983ab99..8436587 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,9 @@ "": { "name": "settings-organizer", "version": "0.0.16", + "dependencies": { + "comment-json": "^4.2.4" + }, "devDependencies": { "@types/mocha": "^10.0.7", "@types/node": "20.x", @@ -644,6 +647,12 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, + "node_modules/array-timsort": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/array-timsort/-/array-timsort-1.0.3.tgz", + "integrity": "sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==", + "license": "MIT" + }, "node_modules/array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -961,6 +970,22 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/comment-json": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/comment-json/-/comment-json-4.2.4.tgz", + "integrity": "sha512-E5AjpSW+O+N5T2GsOQMHLLsJvrYw6G/AFt9GvU6NguEAfzKShh7hRiLtVo6S9KbRpFMGqE5ojo0/hE+sdteWvQ==", + "license": "MIT", + "dependencies": { + "array-timsort": "^1.0.3", + "core-util-is": "^1.0.3", + "esprima": "^4.0.1", + "has-own-prop": "^2.0.0", + "repeat-string": "^1.6.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -976,8 +1001,7 @@ "node_modules/core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", - "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", - "dev": true + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, "node_modules/cross-spawn": { "version": "7.0.3", @@ -1241,6 +1265,19 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "license": "BSD-2-Clause", + "bin": { + "esparse": "bin/esparse.js", + "esvalidate": "bin/esvalidate.js" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/esquery": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.6.0.tgz", @@ -1528,6 +1565,15 @@ "node": ">=8" } }, + "node_modules/has-own-prop": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-own-prop/-/has-own-prop-2.0.0.tgz", + "integrity": "sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", @@ -2536,6 +2582,15 @@ "node": ">=8.10.0" } }, + "node_modules/repeat-string": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", + "integrity": "sha512-PV0dzCYDNfRi1jCDbJzpW7jNNDRuCOG/jI5ctQcGKt/clZD+YcPS3yIlWuTJMmESC8aevCFmWJy5wjAFgNqN6w==", + "license": "MIT", + "engines": { + "node": ">=0.10" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/package.json b/package.json index 58848d8..5679d28 100644 --- a/package.json +++ b/package.json @@ -41,14 +41,17 @@ "test": "vscode-test" }, "devDependencies": { - "@types/vscode": "^1.91.0", "@types/mocha": "^10.0.7", "@types/node": "20.x", + "@types/vscode": "^1.91.0", "@typescript-eslint/eslint-plugin": "^7.14.1", "@typescript-eslint/parser": "^7.11.0", - "eslint": "^8.57.0", - "typescript": "^5.4.5", "@vscode/test-cli": "^0.0.9", - "@vscode/test-electron": "^2.4.0" + "@vscode/test-electron": "^2.4.0", + "eslint": "^8.57.0", + "typescript": "^5.4.5" + }, + "dependencies": { + "comment-json": "^4.2.4" } } diff --git a/src/extension.ts b/src/extension.ts index 132f450..f8637a6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,6 +2,7 @@ import * as vscode from "vscode"; import * as fs from "fs"; import * as path from "path"; import * as os from "os"; +import * as cjson from "comment-json"; export function activate(context: vscode.ExtensionContext) { let settings_organizer_global = vscode.commands.registerCommand( @@ -41,12 +42,12 @@ export function activate(context: vscode.ExtensionContext) { if (fs.existsSync(userSettingsPath)) { const rawSettings = fs.readFileSync(userSettingsPath, "utf-8"); - const cleanedSettings = preprocessJSON(rawSettings); - const settings = JSON.parse(cleanedSettings); + const settings = cjson.parse(rawSettings); + console.log(settings); const categorizedSettings = categorizeSettings(settings); fs.writeFileSync( userSettingsPath, - JSON.stringify(categorizedSettings, null, 2) + cjson.stringify(categorizedSettings, null, 2) ); vscode.window.showInformationMessage("Global settings.json organized!"); } else { @@ -74,12 +75,12 @@ export function activate(context: vscode.ExtensionContext) { if (fs.existsSync(userSettingsPath)) { const rawSettings = fs.readFileSync(userSettingsPath, "utf-8"); - const cleanedSettings = preprocessJSON(rawSettings); - const settings = JSON.parse(cleanedSettings); + const settings = cjson.parse(rawSettings); + console.log(settings); const categorizedSettings = categorizeSettings(settings); fs.writeFileSync( userSettingsPath, - JSON.stringify(categorizedSettings, null, 2) + cjson.stringify(categorizedSettings, null, 2) ); vscode.window.showInformationMessage("Local settings.json organized!"); } else { @@ -94,14 +95,6 @@ export function activate(context: vscode.ExtensionContext) { export function deactivate() {} -function preprocessJSON(jsonString: string): string { - // Remove comments - jsonString = jsonString.replace(/\/\/.*$/gm, ""); - // Remove trailing commas - jsonString = jsonString.replace(/,(\s*[}\]])/g, "$1"); - return jsonString; -} - function categorizeSettings(settings: any) { const sortedSettings: { [key: string]: any } = {}; const categoriesOrder = [