代码改变世界

node操作 windows的appdata本地缓存文件

2018-11-29 21:58  muamaker  阅读(1519)  评论(0编辑  收藏  举报

const os = require('os');
const path = require("path");
const fs = require("fs");
 
var homedir = os.homedir();
 
function mkdirs(dirpath) {
    if (!fs.existsSync(path.dirname(dirpath))) {
      mkdirs(path.dirname(dirpath));
    }
    fs.mkdirSync(dirpath);
}
 
function createDir(myPath){
    fs.existsSync(myPath) == false && mkdirs(myPath);
}
 

var _x = Symbol("x");

class AppData{
    constructor(dbnm){
        if(!dbnm){
            throw new Error("the database name is needless");
            return;
        }
        dbnm = dbnm + ".info";
        this.apppath = path.join(homedir,"/AppData/Local/excelMaster/", dbnm);     
        createDir(path.dirname(this.apppath));
 
    }
    connect(cd){
        cd = cd || function(){};
        fs.readFile(this.apppath,"utf-8",function(err,res){
            if(err){
                this[_x] = {};
            }else{
                var str = Buffer.from(res, 'base64').toString("utf8");
                if(str){
                    try{
                        this[_x] = JSON.parse(str);
                    }catch(e){
                        this[_x] = {};
                    }
                }else{
                    this[_x] = {};
                }
            }
            cd(err,res);
        });
    }
    connectSync(){
        try{
            var res = fs.readFileSync(this.apppath,"utf-8");
            var str = Buffer.from(res, 'base64').toString("utf8");
            if(str){
                try{
                    this[_x] = JSON.parse(str);
                }catch(e){
                    this[_x] = {};
                }
            }else{
                this[_x] = {};
            }   
        }catch(e){
            this[_x] = {};
        }
         
    }
    get(k){
        return this[_x][k];
    }
    set(k,val,callback){
        callback = callback || function(){};
        this[_x][k] = val;
        const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
        fs.writeFile(this.apppath,buf.toString('base64'),callback);
    }
    setSync(k,val){
        this[_x][k] = val;
        const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
        fs.writeFileSync(this.apppath,buf.toString('base64'));
    }
    setSyncObj(obj){
    	for(var i in obj){
    		this[_x][i] = obj[i];
    	}
    	const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
        fs.writeFileSync(this.apppath,buf.toString('base64'));
    }
    has(k){
        return k in this[_x];
    }
    keys(){
        return Object.keys(this[_x]);
    }
    values(){
        return Object.values(this[_x]);
    }
    drop(){
        this[_x] = {};
        const buf = Buffer.from(JSON.stringify({}), 'utf8');
        fs.writeFileSync(this.apppath,buf.toString('base64'));
    }
    disconnect(){
        this.apppath = null;
        delete this[_x];
    }
}
 
module.exports = AppData;

  

 

 

使用方式

var AppData = require("./appdata");

var p = new AppData("abc");

p.connectSync();

//p.setSync("manny","28");

console.log(p.get("manny"))

p.disconnect();