I wrote the following code to create an object with KEYS settings for header comments, and VALUES for files that share these headers. If there is no header, the file name is written to the file. I would like to write this header object to a file, but it is written before the completion of the object. I understand why this is happening, but I cannot solve the problem. Here is the code to write the object to a file:
stringObj = JSON.stringify(allHeaders, null, 4);
fs.writeFile(currentHeads, stringObj, function(err) {
if (err) {
console.log(err);
}
});
console.log('Complete!');
And here is the rest of the script:
"use strict";
var fs = require('fs'),
dive = require('dive'),
subdirs = require('subdirs'),
cpFile = __dirname + "/" + "header.txt",
noCopy = __dirname + "/" + "noCopyright.txt",
currentHeads = __dirname + "/" + "currentHeaders.txt",
re = /(\/\*(?:(?!\*\/).|[\n\r])*\*\/)/,
walkDir,
allHeaders = {},
stringObj,
top;
fs.readFile(cpFile, 'utf8', function(err, copyRight) {
subdirs(__dirname, 1, function(err, dirs) {
if (err) {
return console.log(err);
}
dirs.forEach(function(dir) {
if (dir.match(/.*\/src$/)) {
dive(dir, { all: false }, function(err, file) {
if (err) {
return console.log(err);
}
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
return console.log(err);
}
if (data.match(re)) {
top = data.match(re);
if (allHeaders[top[0]]) {
allHeaders[top[0]].push(file);
} else {
allHeaders[top[0]] = [file];
}
} else {
fs.appendFile(noCopy, file + "\n", function(err) {
if (err) {
return console.log(err);
}
});
}
});
});
}
});
});
});