简体   繁体   English

用Javascript编写的脚本不会自行终止吗?

[英]Script written in Javascript not terminating on its own?

Hi I have written a script that has a recursive function which takes a path and parses the directory structure. 嗨,我已经编写了一个脚本,该脚本具有递归功能,该功能采用路径并解析目录结构。 The script works as expected but it does not terminate on its own. 该脚本可以按预期工作,但不会自行终止。 Script is written in Javascript and it uses NodeJs modules like fs(file System) and path. 脚本是用Javascript编写的,它使用NodeJs模块(例如fs(文件系统)和path)。

My code : 我的代码:

var parseDirectory = function(currPath, tags, func){
    var util = util || require('./util');
    var fs = fs || require('fs');
    var path = path || require('path');
    if(!util.fileExist(currPath)) {
        throw new Error('Path is invalid, it does not exist.');
    }

    //get the stat and do operation according to file type
    var stat = fs.statSync(currPath);

    //if directory
    // - get all files inside the directory
    // - push the currPath in tags
    // - call parseDirectory for each file
    // - pop from the tags array
    if(stat.isDirectory()) {
        var files = fs.readdirSync(currPath);
        var fs = fs || require('fs');
        tags.push(path.parse(currPath).name);
        files.forEach(function(file) {
            parseDirectory(path.join(currPath,file),tags,func);
        });
        tags.pop();
    } else if(stat.isFile()) {
        func(currPath,tags);
    } else {
        throw new Error('Path is not a file or Directory');
    }
}

//connect to db
var mongoose = mongoose || require('mongoose');
mongoose.connect('mongodb://localhost:27017/iconrepo');

// a sample call to parseDirectory
parseDirectory('icon/png/',[],function(filePath, tags) {
    var path = path || require('path');
    var fs = fs || require('fs');
    var File = require('./models/file');
    var stat = fs.statSync(filePath);

    var file  = new File({
        name : path.parse(filePath).name,
        type : path.extname(filePath),
        size : stat.size,
        tags : tags,
        path : filePath
    });

    //console.log(file);
    //file.save(function(err) {
    //  if(err) {
    //      throw new Error(err);
    //  }

    //  console.log('Added ', filePath);
    //});

    console.log('Exiting Callback for filePath',filePath);
});

console.log('Last statement of the script');

File.js File.js

// a file schema file

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var fileSchema = new Schema({
    name: String,
    type: String,
    size: {type: Number},
    tags: [String],
    path: String
});

module.exports = mongoose.model('File',fileSchema);
// a file schema file

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var fileSchema = new Schema({
    name: String,
    type: String,
    size: {type: Number},
    tags: [String],
    path: String
});

module.exports = mongoose.model('File',fileSchema);

Can anyone please point me what mistake I am making. 谁能指出我犯的错误。

Mongoose established a connection which would keep your script running. 猫鼬建立了一个连接,可以使您的脚本保持运行。

Call mongoose.connection.close() end your script. 调用mongoose.connection.close()结束脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM