Implement strip-json-comments in sage

Hello,
I want to add comments in my json files in sage, and I don’ t really know how to implement it.
On strip-json-comments npm page it says :

Install

$ npm install --save-dev gulp-strip-json-comments

##Usage

var gulp = require('gulp');
var stripJsonComments = require('gulp-strip-json-comments');
 
gulp.task('default', function () {
	return gulp.src('src/config.json')
		.pipe(stripJsonComments())
		.pipe(gulp.dest('dist'));
});

But sage gulpfile is not so easy to a beginner.

// See https://github.com/austinpray/asset-builder
var manifest = require('asset-builder')('./assets/manifest.json');
// ### JSHint
// `gulp jshint` - Lints configuration JSON and project JS.
gulp.task('jshint', function() {
  return gulp.src([
    'bower.json', 'gulpfile.js'
  ].concat(project.js))
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'))
    .pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
});

How can I modify this to strip comments ?
Thanks

What have you tried?

Here you go for manifest.json, I am quite new to gulp/node, don’t laugh:

var manifest = require('asset-builder')()
  .pipe(stripJsonComments('./assets/manifest.json'));

TypeError: Path must be a string. Received undefined

var manifestFile = stripJsonComments('./assets/manifest.json');
var manifest = require('asset-builder')(manifestFile);

This one too

var manifest = gulp.src('./assets/manifest.json')
  .pipe(stripJsonComments())
    .pipe(require('asset-builder')());

same, here again.
I don’t know how to give a path with stripped ou comments to asset-builder without writing a temporary file…
Do we have to hack assets-builder for that ?

For bower.json it works with multi-line comments /**/ out of the box, no need for strip-json-comments. (Don’t know where is it implemented).
Thanks for the lesson @cfx

So you want to put comments in your manifest.json file? And strip them out before we load/use the file?

I don’t think it’s possible :frowning:. asset-building takes the path to a manifest file and doesn’t support a JSON string.

Maybe @austin can update asset-builder to be more flexible or you’d have to fork it to support it yourself.

edit: if you wanted to fork and update, you’d just need to update this:

to

var stripJsonComments = require('strip-json-comments');

return JSON.parse(stripJsonComments(fs.readFileSync(p.normalize(path), 'utf8')))
1 Like

Thank you very much @swalkinshaw , I could do it following your instructions
Had forked @austin 's asset-builder
Then edited package.json of sage theme and remplaced version like this :
"asset-builder": "https://github.com/abumalick/asset-builder/tarball/master",
then :
npm install
gulp

And everything worked.
Thanks again for your help.

Made a pull request, maybe @austin will want to merge it :

1 Like