0

I'm trying to write a VSTS extension that needs to load (and parse) a JSON file but I'm having a hard time finding the right way to do it.

So I have something like:

VSS.init({
    explicitNotifyLoaded: true,
    usePlatformScripts: true
});

var witClient;
var rules;

VSS.ready(function () {
    require(["fs"], function (fs) {
        rules = JSON.parse(fs.readFileSync("urlMatches.json"));
    })

    VSS.require(["VSS/Service", "TFS/WorkItemTracking/RestClient"], function (VSS_Service, TFS_Wit_WebApi) {
        // Get the REST client
        witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
    });

     // Register a listener for the work item page contribution.
    VSS.register(VSS.getContribution().id, function () {
        return {
            // Called after the work item has been saved
            onSaved: function (args) {
                witClient.getWorkItem(args.id).then(
                    function (workItem) {
                        // do some stuff involving the loaded JSON file...
                    });
            }
        }
    });

    VSS.notifyLoadSucceeded();
});

I've tried a bunch of variations on this without any luck. I've even tried using jQuery to load my file synchronously and yet my rules ends up undefined.

So I have the file urlmatches.json and I need to load it and use it to populate the variable rules before I get to the onSaved handler.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • What's the detail error? Do you set that file can be addressable in vss-extension.json? ( { "path": "urlmatches.json", "addressable": true }) – starian chen-MSFT Mar 16 '18 at 06:22
  • Well on that particular attempt you get an error `GET https://***-internal.gallerycdn.vsassets.io/extensions/***-internal/***-extension/0.1.602/1521212199291/fs.js net::ERR_ABORTED`. `urlmatches.json` is set as `addressable` and I can actually access it in the browser with it's url. – Matt Burland Mar 16 '18 at 15:00
  • I am afraid you can't read file directly, I recommend that you can retrieve the data through HTTP request, check my answer. – starian chen-MSFT Mar 19 '18 at 02:30

1 Answers1

0

You can retrieve content through HTTP request, for example:

 onSaved: function (args) {
                        console.log("onSaved -" + JSON.stringify(args));
                        var request = new XMLHttpRequest();
                        request.open('GET', 'TestData.txt', true);
                        request.send(null);
                        request.onreadystatechange = function () {
                            if (request.readyState === 4 && request.status === 200) {
                                var type = request.getResponseHeader('Content-Type');
                                console.log(request.responseText);
                            }
                        }
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53