This question already has an answer here:
Here's the story: I'm actually coding in javascript using nodejs a little "todos retriever", this is to say a script that will look in all the files it finds the pattern "//todo: what I have to do" and write it in a file called "todo.txt".
Here's an example of what it should do:
a_script.js (path: ./project/js):
var variable = "string"; //todo: change value
After the execution of todo.js, the retriever:
todo.txt (path: ./):
./project/js/a_script.js: change value
I think you have understood what it is about. Now the code of todo.js (not yet finished, some tweaking and features needed):
var fs = require("fs");
fs.writeFile("todo.txt", "", "utf-8", function(err) { //I won't retrieve errors, as this is just a demo
retrieve_todos("./", false);
});
function retrieve_todos(dir, check_sub_folders) {
var index, todos, file;
fs.readdir(dir, function(err, data) {
for(index in data) {
file = data[index];
if(file != "todo.txt") { //yeah, this line may be useless, but I prefer to keep it
if(/.+\..+/gi.test(file)) { //is it a file
fs.readFile(dir + file, "utf-8", function(err, data) {
if(typeof data != "undefined") { //Ran some issues on some files that, I think, shouldn't be read, so I put that to skip this files that return no data
todos = data.match(/\/\/todo:.+/gi); //retrieve the "//todo: ..." pattern
for(index in todos) {
fs.appendFile("todo.txt", dir + file + ": " + todos[index].substring(8, todos[index].length) + "\n"); //write the todos to todo.txt after having removed the "//todo: "
}
}
});
} else { //if not a file
if(check_sub_folders && !(/^\./gi.test(data[index]))) retrieve_todos(dir + data[index] + "/", true); //read sub-directory, if check_sub_folders == true and if it is not a folder beginning by a dot
}
}
}
});
}
But when I run it, the output todo.txt looks like this:
./todo.txt: complete readme.md
./todo.txt: nav bar with access to profile, friends
./todo.txt: chat with other players (dynamic bottom bar)
./todo.txt: footer for each page
./todo.txt: change checking auth system to make possible observing game without being prompted to log in
./todo.txt: make it work
./todo.txt: make it work
./todo.txt: see if including a var for first is necessery
./todo.txt: make it work (w/ new account system)
./todo.txt: make it work
And that's make no sense, as todo.txt is re-created each todo.js' run. And that's the problem, I can't get the right filename. If it ran correctly it should had output:
./README.md: complete readme.md
./README.md: nav bar with access to profile, friends
./README.md: chat with other players (dynamic bottom bar)
./README.md: footer for each page
./server.js: change checking auth system to make possible observing game without being prompted to log in
./server.js: make it work
./server.js: make it work
./server.js: see if including a var for first is necessery
./server.js: make it work (w/ new account system)
./server.js: make it work
After some researches, I found that if I add a console.log(file);
before reading the file (fs.readFile(dir + file, "utf-8", function(err, data) {
), the variable file
doesn't have the same value if I put the console.log(file);
after reading the file:
Just before fs.readFile(dir + file, "utf-8", function(err, data) {
:
README.md
architecture.txt
package.json
server.js
todo.js
And after reading the file:
todo.txt
todo.txt
todo.txt
todo.txt
todo.txt
I really don't know why file change its value in the fs.readFile's callback. Please help.
Thank you. Regards, PLD.
Aucun commentaire:
Enregistrer un commentaire