@atomist/automation-client
Version:
Atomist API for software low-level client
65 lines • 2.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs-extra");
const isbinaryfile_1 = require("isbinaryfile");
const nodePath = require("path");
const logger_1 = require("../../util/logger");
const AbstractFile_1 = require("../support/AbstractFile");
/**
* Implementation of File interface on node file system
*/
class NodeFsLocalFile extends AbstractFile_1.AbstractFile {
constructor(baseDir, path) {
super();
this.baseDir = baseDir;
this.path = path;
if (path.startsWith(nodePath.sep)) {
this.path = path.substr(1);
}
}
get realPath() {
return realPath(this.baseDir, this.path);
}
getContentSync(encoding = "utf8") {
return fs.readFileSync(this.realPath).toString(encoding);
}
getContent(encoding = "utf8") {
return this.getContentBuffer()
.then(buf => buf.toString(encoding));
}
getContentBuffer() {
return fs.readFile(this.realPath);
}
setContent(content) {
return fs.writeFile(this.realPath, content)
.then(() => this.invalidateCache());
}
setContentSync(content) {
fs.writeFileSync(this.realPath, content);
this.invalidateCache();
return this;
}
setPath(path) {
if (path !== this.path) {
logger_1.logger.debug(`setPath: from ${this.path} to ${path}: Unlinking ${this.realPath}`);
const oldPath = this.realPath;
this.path = path;
return fs.move(oldPath, this.realPath).then(_ => this);
}
return Promise.resolve(this);
}
isExecutable() {
return fs.access(this.realPath, fs.constants.X_OK).then(() => true).catch(_ => false);
}
isReadable() {
return fs.access(this.realPath, fs.constants.R_OK).then(() => true).catch(_ => false);
}
isBinary() {
return isbinaryfile_1.isBinaryFile(this.realPath);
}
}
exports.NodeFsLocalFile = NodeFsLocalFile;
function realPath(baseDir, path) {
return baseDir + (path.startsWith("/") ? "" : "/") + path;
}
//# sourceMappingURL=NodeFsLocalFile.js.map