Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating file references in a json file via a grunt task

I'm a JavaScript developer and fairly new to creating a build process from scratch. I chose to use Grunt for my current project and have created a GruntFile that does about 90% of what I need it to do and it works great, except for this one issue. I have several JavaScript files that I reference while I'm developing a chrome extension in the manifest.json file. For my build process I am concatenating all of these files and minifying it into one file to be included in manifest.json. Is there anyway to update the file references in the manifest.json file during the build process so it points to the minified version?

Here is a snippet of the src manifest file:

{
    "content_scripts": [{
        "matches": [
            "http://*/*"
        ],
        "js": [
            "js/lib/zepto.js",
            "js/injection.js",
            "js/plugins/plugin1.js",
            "js/plugins/plugin2.js",
            "js/plugins/plugin3.js",
            "js/injection-init.js"
        ]
    }],
    "version": "2.0",
}

I have a grunt task that concatenates and minifies all the js files listed above into one file called injection.js and would like a grunt task that can modify the manifest file so it looks like this:

{
    "content_scripts": [{
        "matches": [
            "http://*/*"
        ],
        "js": [
            "js/injection.js"
        ]
    }],
    "version": "2.0",
}

What I've done for now is have 2 versions of the manifest file, one for dev and one for build, during the build process it copies the build version instead. This means I need to maintain 2 versions which I'd rather not do. Is there anyway to do this more elegantly with Grunt?

like image 346
Charles Avatar asked Jun 11 '13 19:06

Charles


People also ask

How do I reference a JSON file?

The JSON Reference uses a $ref key. The value of the <reference> is a JSON Reference which is composed of two parts <relative path to file or URL><JSON pointer>. empty (to refer to the entire file -- this is the same as # ). # with path to the object (refers to the root of the document). Each path segment should have a / preceding it (eg. #/foo ).

How to extend an object in a JSON file?

Use a JSON Pointer to the object. Within a separate file we need a relative path to the file and then the JSON Pointer path to the object separated by a # mark. This object cannot be extended with additional properties and any properties added SHALL be ignored.

What is the value of $ref in JSON?

The JSON Reference uses a $ref key. The value of the <reference> is a JSON Reference which is composed of two parts <relative path to file or URL><JSON pointer>. empty (to refer to the entire file -- this is the same as # ).

Can I use a JSON reference with OpenAPI?

Using paths relative to the root file is a common mistake. To be strictly compliant with OpenAPI 3.x, a JSON Reference can only be used where explicitly noted in the OpenAPI specification. For example, it can be used for Paths, Parameters, Schema Objects, and more:


2 Answers

Grunt gives its own api for reading and writing files, i feel that better than other dependencies like fs: Edit/update json file using grunt with command grunt updatejson:key:value after putting this task in your gruntjs file

grunt.registerTask('updatejson', function (key, value) {
        var projectFile = "path/to/json/file";


        if (!grunt.file.exists(projectFile)) {
            grunt.log.error("file " + projectFile + " not found");
            return true;//return false to abort the execution
        }
        var project = grunt.file.readJSON(projectFile);//get file as json object

        project[key]= value;//edit the value of json object, you can also use projec.key if you know what you are updating

        grunt.file.write(projectFile, JSON.stringify(project, null, 2));//serialize it back to file

    });
like image 118
tekkavi Avatar answered Oct 29 '22 03:10

tekkavi


I do something similar - you can load your manifest, update the contents then serialize it out again. Something like:

grunt.registerTask('fixmanifest', function() {
     var tmpPkg = require('./path/to/manifest/manifest.json');

     tmpPkg.foo = "bar";
     fs.writeFileSync('./new/path/to/manifest.json', JSON.stringify(tmpPkg,null,2));
});
like image 23
dc5 Avatar answered Oct 29 '22 03:10

dc5