@fimbul/wotan
Version:
Pluggable TypeScript and JavaScript linter
68 lines • 2.51 kB
JavaScript
var NodeFileSystem_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeFileSystem = void 0;
const tslib_1 = require("tslib");
const ymir_1 = require("@fimbul/ymir");
const fs = require("fs");
const inversify_1 = require("inversify");
const utils_1 = require("../../utils");
const ts = require("typescript");
let NodeFileSystem = NodeFileSystem_1 = class NodeFileSystem {
constructor(logger) {
this.logger = logger;
}
static normalizePath(path) {
const normalized = utils_1.unixifyPath(ts.sys.useCaseSensitiveFileNames ? path : path.toLowerCase());
// handle windows drive names 'C:' -> 'C:/', because reading 'C:' actually reads '.'
return normalized.includes('/') ? normalized : normalized + '/';
}
normalizePath(path) {
return NodeFileSystem_1.normalizePath(path);
}
readFile(file) {
const buf = fs.readFileSync(file);
const len = buf.length;
// detect MPEG TS files and treat them as empty
outer: while (len > 188 && buf[0] === 0x47) {
for (let i = 188; i < len; i += 188)
if (buf[i] !== 0x47)
break outer;
this.logger.warn(`Detected MPEG TS file: '${file}'.`);
return '';
}
if (len >= 2) {
if (buf[0] === 0xFE && buf[1] === 0xFF) // UTF16BE BOM
return buf.swap16().toString('utf16le', 2);
if (buf[0] === 0xFF && buf[1] === 0xFE) // UTF16LE BOM
return buf.toString('utf16le', 2);
if (len >= 3 && buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF) // UTF8 with BOM
return buf.toString('utf8');
}
return buf.toString('utf8'); // default to UTF8 without BOM
}
readDirectory(dir) {
return fs.readdirSync(dir, { withFileTypes: true });
}
stat(path) {
return fs.statSync(path);
}
realpath(path) {
return fs.realpathSync(path);
}
writeFile(file, content) {
return fs.writeFileSync(file, content);
}
deleteFile(path) {
return fs.unlinkSync(path);
}
createDirectory(dir) {
return fs.mkdirSync(dir, { recursive: true });
}
};
NodeFileSystem = NodeFileSystem_1 = tslib_1.__decorate([
inversify_1.injectable(),
tslib_1.__metadata("design:paramtypes", [ymir_1.MessageHandler])
], NodeFileSystem);
exports.NodeFileSystem = NodeFileSystem;
//# sourceMappingURL=file-system.js.map
;