Skip to content

Commit 5ac25f6

Browse files
committed
Support JSON 5 with new --json5 flag (thanks @dwilsonmsft)
1 parent 288ffeb commit 5ac25f6

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

app/exec/extension/_lib/interfaces.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ export interface MergeSettings {
164164
* Path to the root of localized resource files
165165
*/
166166
locRoot: string;
167+
168+
/**
169+
* If true, treat JSON files as "Json 5" or "extended JSON",
170+
* which supports comments, unquoted keys, etc.
171+
*/
172+
json5: boolean;
167173
}
168174

169175
export interface PackageSettings {

app/exec/extension/_lib/merger.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { forwardSlashesPath, toZipItemName } from "./utils";
1414
import _ = require("lodash");
1515
import fs = require("fs");
1616
import glob = require("glob");
17+
import jju = require("jju");
1718
import jsonInPlace = require("json-in-place");
1819
import loc = require("./loc");
1920
import path = require("path");
@@ -112,7 +113,7 @@ export class Merger {
112113
promisify(readFile)(file, "utf8").then(data => {
113114
const jsonData = data.replace(/^\uFEFF/, "");
114115
try {
115-
const result = JSON.parse(jsonData);
116+
const result = this.settings.json5 ? jju.parse(jsonData) : JSON.parse(jsonData);
116117
result.__origin = file; // save the origin in order to resolve relative paths later.
117118
return result;
118119
} catch (err) {
@@ -153,7 +154,14 @@ export class Merger {
153154

154155
updateVersionPromise = promisify(readFile)(partial.__origin, "utf8").then(versionPartial => {
155156
try {
156-
const newPartial = jsonInPlace(versionPartial).set("version", newVersionString);
157+
let newPartial: any;
158+
if (this.settings.json5) {
159+
const parsed = jju.parse(versionPartial);
160+
parsed["version"] = newVersionString;
161+
newPartial = jju.update(versionPartial, parsed);
162+
} else {
163+
newPartial = jsonInPlace(versionPartial).set("version", newVersionString);
164+
}
157165
return promisify(writeFile)(partial.__origin, newPartial);
158166
} catch (e) {
159167
trace.warn(

app/exec/extension/create.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class ExtensionCreate extends extBase.ExtensionBase<CreationResult> {
4949
"root",
5050
"manifests",
5151
"manifestGlobs",
52+
"json5",
5253
"override",
5354
"overridesFile",
5455
"revVersion",

app/exec/extension/default.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export interface ExtensionArguments extends CoreArguments {
2525
bypassValidation: args.BooleanArgument;
2626
description: args.StringArgument;
2727
displayName: args.StringArgument;
28-
extensionName: args.StringArgument;
2928
extensionId: args.StringArgument;
29+
extensionName: args.StringArgument;
30+
json5: args.BooleanArgument;
3031
locRoot: args.ExistingDirectoriesArgument;
3132
manifestGlobs: args.ArrayArgument;
3233
manifests: args.ArrayArgument;
@@ -88,6 +89,13 @@ export class ExtensionBase<T> extends TfCommand<ExtensionArguments, T> {
8889
args.ArrayArgument,
8990
null,
9091
);
92+
this.registerCommandArgument(
93+
"json5",
94+
"Extended JSON",
95+
"Support extended JSON (aka JSON 5) for comments, unquoted strings, dangling commas, etc.",
96+
args.BooleanArgument,
97+
"false"
98+
);
9199
this.registerCommandArgument("outputPath", "Output path", "Path to write the VSIX.", args.StringArgument, "{auto}");
92100
this.registerCommandArgument(
93101
"override",
@@ -169,6 +177,7 @@ export class ExtensionBase<T> extends TfCommand<ExtensionArguments, T> {
169177
this.commandArgs.bypassValidation.val(),
170178
this.commandArgs.publisher.val(true),
171179
this.commandArgs.extensionId.val(true),
180+
this.commandArgs.json5.val(true),
172181
]).then<MergeSettings>(values => {
173182
const [
174183
root,
@@ -181,6 +190,7 @@ export class ExtensionBase<T> extends TfCommand<ExtensionArguments, T> {
181190
bypassValidation,
182191
publisher,
183192
extensionId,
193+
json5,
184194
] = values;
185195
if (publisher) {
186196
_.set(override, "publisher", publisher);
@@ -217,6 +227,7 @@ export class ExtensionBase<T> extends TfCommand<ExtensionArguments, T> {
217227
overrides: mergedOverrides,
218228
bypassValidation: bypassValidation,
219229
revVersion: revVersion,
230+
json5: json5,
220231
};
221232
});
222233
});

app/exec/extension/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ export class ExtensionInit extends extBase.ExtensionBase<InitResult> {
400400
manifests: null,
401401
overrides: {},
402402
root: initPath,
403+
json5: false,
403404
},
404405
{
405406
locRoot: null,

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"clipboardy": "~1.2.3",
2626
"colors": "~1.3.0",
2727
"glob": "7.1.2",
28+
"jju": "^1.4.0",
2829
"json-in-place": "^1.0.1",
2930
"jszip": "~3.1.5",
3031
"lodash": "~4.17.11",
@@ -46,6 +47,7 @@
4647
"devDependencies": {
4748
"@types/clipboardy": "~1.1.0",
4849
"@types/glob": "^5.0.29",
50+
"@types/jju": "^1.4.1",
4951
"@types/jszip": "~3.1.2",
5052
"@types/lodash": "~4.14.110",
5153
"@types/mkdirp": "^0.3.28",

0 commit comments

Comments
 (0)