Skip to content

Commit 0ab5a63

Browse files
committed
json5 support #9
1 parent 0b78e87 commit 0ab5a63

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Tasks/JsonPatch/json5Patcher.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import jsonPatcher = require('./common/jsonPatcher');
2+
import patch = require('./common/patch');
3+
var JSON5 = require('json5');
4+
5+
export class Json5Patcher extends jsonPatcher.JsonPatcher {
6+
7+
parse(content: string): any {
8+
return JSON5.parse(content);
9+
}
10+
11+
stringify(content: any): string {
12+
return JSON5.stringify(content);
13+
}
14+
}

Tasks/JsonPatch/jsonPatch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ import micromatch = require('micromatch');
66
import patch = require('./common/patch');
77
import patchProcess = require('./common/patchProcess');
88
import jsonPatcher = require('./common/jsonPatcher');
9+
import json5Patcher = require('./json5Patcher');
910

1011
var targetPath = tl.getPathInput("JsonWorkingDir");
1112
var patchContent = tl.getInput("JsonPatchContent");
1213

1314
var patterns: any = tl.getInput("JsonTargetFilters")
1415
var outputPatchedFile = tl.getBoolInput("OutputPatchFile");
1516
var syntax = tl.getInput("SyntaxType");
17+
var useJson5 = tl.getBoolInput("UseJson5");
1618

1719
try {
1820
var patches: patch.IPatch[] = syntax == "slick" ?
1921
patchProcess.expandVariablesAndParseSlickPatch(patchContent) :
2022
patchProcess.expandVariablesAndParseJson(patchContent);
2123

22-
patchProcess.apply(new jsonPatcher.JsonPatcher(patches), targetPath, patterns, outputPatchedFile);
24+
var patcher = useJson5 ? new json5Patcher.Json5Patcher(patches) : new jsonPatcher.JsonPatcher(patches);
25+
26+
patchProcess.apply(patcher, targetPath, patterns, outputPatchedFile);
2327

2428
tl.setResult(tl.TaskResult.Succeeded, "Files Patched");
2529

Tasks/JsonPatch/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"micromatch": "2.3.11",
88
"fast-json-patch": "1.0.0",
99
"xregexp": "3.1.1",
10-
"vsts-task-lib": "0.9.6"
10+
"vsts-task-lib": "0.9.6",
11+
"json5" : "0.5.0"
1112
}
1213
}

Tasks/JsonPatch/task.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
6868
"label": "Output patched file in logs",
6969
"defaultValue": "false",
7070
"helpMarkDown": "Output patched file in logs"
71+
},
72+
{
73+
"name": "UseJson5",
74+
"type": "boolean",
75+
"label": "Use JSON 5",
76+
"defaultValue": "false",
77+
"helpMarkDown": "This enables JSON5 syntax support"
7178
}
7279
],
7380
"execution": {

0 commit comments

Comments
 (0)