molstar
Version:
A comprehensive macromolecular library.
79 lines • 2.8 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Taken/adapted from DensityServer (https://github.com/dsehnal/DensityServer)
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import * as fs from 'fs';
import * as path from 'path';
import { SimpleBuffer } from '../../../mol-io/common/simple-buffer';
export function openRead(filename) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (res, rej) {
fs.open(filename, 'r', function (err, file) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (err) {
rej(err);
return [2 /*return*/];
}
try {
res(file);
}
catch (e) {
fs.closeSync(file);
}
return [2 /*return*/];
});
}); });
})];
});
});
}
function makeDir(path, root) {
var dirs = path.split(/\/|\\/g), dir = dirs.shift();
root = (root || '') + dir + '/';
try {
fs.mkdirSync(root);
}
catch (e) {
if (!fs.statSync(root).isDirectory())
throw new Error(e);
}
return !dirs.length || makeDir(dirs.join('/'), root);
}
export function exists(filename) {
return fs.existsSync(filename);
}
export function createFile(filename) {
return new Promise(function (res, rej) {
if (fs.existsSync(filename))
fs.unlinkSync(filename);
makeDir(path.dirname(filename));
fs.open(filename, 'w', function (err, file) {
if (err)
rej(err);
else
res(file);
});
});
}
var smallBuffer = SimpleBuffer.fromBuffer(Buffer.alloc(8));
export function writeInt(file, value, position) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
smallBuffer.writeInt32LE(value, 0);
return [4 /*yield*/, file.writeBuffer(position, smallBuffer, 4)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
//# sourceMappingURL=file.js.map