节点搜索和存储功能

时间:2015-07-29 22:48:56

标签: javascript node.js

我试图编写一个节点脚本,它将从目录/子目录中的文件内搜索并获取模式中的所有代码,并使用文件名/位置存储代码,但似乎遇到了一些麻烦。有人能指出我正确的方向吗?

var fs = require('fs');
var migrationCode = [];
var migrationFiles = [];
var useDirectory = '.';

fs.readdir(useDirectory, function(err, files) {
    files.filter(function(file) {
        return file;
    })
    .forEach(function(file) {
        fs.readFile(file, 'utf-8', function(err, contents) {
            inspectFile(contents);
        });
    });

    function inspectFile(contents, filename) {
        var contents = contents;
        var pat = contents.match(/^\/\/.*\*custom\*(.|\n)*\*custom\*/g);
        if (contents.indexOf(pat) != -1) {
            for (var index in pat) {
                migrationFiles.push({migrationCode[index], filename});
            }
        }
    }

    if (err)
        throw err;

    for (var index in files) {
        this.inspectFile(file[contents], files[index]);
    }
});

示例 - file.js

的输入示例
// *custom code*

all of this including comment above and below

// *custom code*

none of this

示例输出(不是特定于脚本的变量)

var exampleOutput = [{'captured code', 'example-file.js'}];

0 个答案:

没有答案