I've got the following simple scenario:
- I parse manifest.json (a JSON file)
- I add some paths to an array field of this JSON object
- I stringify the object back to JSON and write back to the file
An issue appears somewhere inbetween: when I add a relative path to the array and then output the array I'm getting double backslashes in my output.
This is part of the code:
var entry = manifest['content_scripts'][0]['js'] = [];
routines.forEach(function(routine) {
var rel = path.relative(sourcePath, routine);
console.log('rel %s', rel);
entry.push(rel);
console.log('added rel %s', entry[entry.length-1]);
console.log('total array %a', entry);
});
This returns:
rel routines\boot.js
added rel routines\boot.js
total array %a [ 'routines\\boot.js' ]
How is this possible? The first entry in the "total array output" is not equal to the output of directly logging this last entry.
May JSON.stringify be causing issues here?
Extra: for those interested, this is the entire build script:
var path = require('path'),
fs = require('fs'),
findit = require('findit'),
sourcePath = path.resolve('./src'),
manifestPath = path.join(sourcePath, 'manifest.json'),
routinesDir = path.join(sourcePath, 'routines');
//find routines
var routines = findit.sync(routinesDir);
if (!Array.isArray(routines) || routines.length === 0) throw new Error('no routines found');
//get file
fs.readFile(manifestPath, 'utf8', function(err, data) {
if (err) throw err;
//get manifest
var manifest = JSON.parse(data);
//flush and reset js entry
var entry = manifest['content_scripts'][0]['js'] = [];
routines.forEach(function(routine) {
var rel = path.relative(sourcePath, routine);
console.log('rel %s', rel);
entry.push(rel);
console.log('added rel %s', entry[entry.length-1]);
console.log('total array %a', entry);
});
//get string back
var manifestStr = JSON.stringify(manifest, null, 4);
console.log('new manifest: ' + manifestStr);
//update file
fs.writeFile(manifestPath, manifestStr, 'utf8', function(err) {
if (err) throw err;
//done
console.log('build done');
});
});
With manifest.json:
{ "name": "Name", "version": "0.0.1", "description": "", "content_scripts": [ { "matches": [ "http://www.example.com" ], "js": [ "routines\boot.js" ], "css": [ "prime.css" ], "run_at": "document_start" } ] }
Finally, I'm running this on Windows 7.