@feflow/cli
Version:
A front-end flow tool.
171 lines • 6.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = __importDefault(require("fs"));
var lockfile_1 = __importDefault(require("lockfile"));
var LockFile = /** @class */ (function () {
function LockFile(filePath, lockKey, logger) {
this.filePath = filePath;
this.lockKey = lockKey;
this.logger = logger;
this.tryMax = 50;
this.tryGap = 100;
this.tryCount = 0;
// 初始化的时候如果不存在也创建
this.ensureFileExists();
}
LockFile.prototype.read = function (key) {
var _this = this;
return new Promise(function (resolve, reject) {
// 文件不存在则创建文件后读取
_this.ensureFileExists();
fs_1.default.stat(_this.filePath, function (err, stats) {
if (err) {
_this.logger.error(err);
return reject(err);
}
if (!stats.isFile()) {
return reject('not a file');
}
_this.checkIfCanRead(function () {
fs_1.default.readFile(_this.filePath, {
encoding: 'utf8',
}, function (err, data) {
if (err) {
_this.logger.error(err);
return reject(err);
}
var finalData = data;
if (!data) {
// no data then turn it to {}
_this.resetFile();
finalData = '{}';
}
_this.logger.debug("get file: ".concat(_this.filePath, " => data: ").concat(finalData));
try {
var dataObj = JSON.parse(finalData);
if (key) {
var targetContent = dataObj[key];
resolve(targetContent);
}
else {
resolve(dataObj);
}
}
catch (e) {
// 写入的文件数据有问题,清空文件,下次重新写入。
_this.resetFile();
_this.logger.error(e);
reject(e);
}
});
});
});
});
};
LockFile.prototype.update = function (key, value) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
// 文件不存在则创建文件后插入
_this.ensureFileExists();
_this.lock(function (err) {
if (err) {
_this.unlock();
return reject(err);
}
var origData = fs_1.default.readFileSync(_this.filePath, 'utf-8');
if (!origData) {
// no data then turn it to {}
_this.resetFile();
origData = '{}';
}
try {
var origDataObj_1 = JSON.parse(origData);
_this.logger.debug("write file ".concat(_this.filePath, " key ").concat(key, " value ").concat(value));
origDataObj_1[key] = value;
fs_1.default.writeFile(_this.filePath, JSON.stringify(origDataObj_1, null, 2), function (err) {
_this.unlock();
if (err) {
_this.logger.error(err);
return reject(err);
}
resolve(origDataObj_1);
});
}
catch (e) {
// 文件数据有问题,清空文件,下次重新写入。
_this.resetFile();
_this.logger.error(e);
_this.unlock();
reject(e);
}
});
}
catch (err) {
_this.logger.error(err);
_this.unlock();
reject(err);
}
});
};
LockFile.prototype.resetFile = function () {
fs_1.default.writeFileSync(this.filePath, JSON.stringify({}, null, 2), 'utf-8');
};
LockFile.prototype.checkIfCanRead = function (cb) {
var _this = this;
lockfile_1.default.check(this.lockKey, function (err, isLocked) {
if (err)
return _this.logger.error("check ".concat(_this.lockKey, " error: "), err);
if (isLocked) {
// another writing is running
if (_this.tryCount >= _this.tryMax) {
_this.tryCount = 0;
// 解锁文件
_this.unlock();
cb();
}
_this.tryCount += 1;
setTimeout(function () {
_this.checkIfCanRead(cb);
}, _this.tryGap);
}
else {
// read immediately
_this.tryCount = 0;
cb();
}
});
};
LockFile.prototype.lock = function (cb) {
var _this = this;
lockfile_1.default.lock(this.lockKey, {
wait: this.tryGap,
retries: this.tryMax,
}, function (err) {
if (err) {
_this.logger.error("lock ".concat(_this.lockKey, " error: "), err);
_this.unlock();
cb(err);
return;
}
cb();
});
};
LockFile.prototype.unlock = function () {
var _this = this;
lockfile_1.default.unlock(this.lockKey, function (err) {
err && _this.logger.error("unlock ".concat(_this.lockKey, " error: "), err);
});
};
LockFile.prototype.ensureFileExists = function () {
if (!fs_1.default.existsSync(this.filePath)) {
this.resetFile();
}
};
return LockFile;
}());
exports.default = LockFile;
//# sourceMappingURL=lock-file.js.map