@atomist/automation-client
Version:
Atomist API for software low-level client
61 lines • 1.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs-extra");
const isBinaryFile = require("isbinaryfile");
const util_1 = require("util");
const __1 = require("../..");
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("/")) {
this.path = path.substr(1);
}
}
get realPath() {
return realPath(this.baseDir, this.path);
}
getContentSync() {
return fs.readFileSync(this.realPath).toString();
}
getContent() {
return fs.readFile(this.realPath)
.then(buf => buf.toString());
}
setContent(content) {
return fs.writeFile(this.realPath, content)
.then(_ => this);
}
setContentSync(content) {
fs.writeFileSync(this.realPath, content);
return this;
}
setPath(path) {
if (path !== this.path) {
__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 util_1.promisify(isBinaryFile)(this.realPath);
}
}
exports.NodeFsLocalFile = NodeFsLocalFile;
function realPath(baseDir, path) {
return baseDir + (path.startsWith("/") ? "" : "/") + path;
}
//# sourceMappingURL=NodeFsLocalFile.js.map