smartfile/dist/smartfile.classes.smartfile.js

118 lines
5.8 KiB
JavaScript
Raw Normal View History

"use strict";
2017-04-27 14:48:08 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartfile.plugins");
2018-02-16 20:57:44 +00:00
const memory = require("./smartfile.memory");
2017-04-27 14:48:08 +00:00
/**
* class Smartfile
* -> is vinyl file compatible
*/
2016-07-17 15:34:15 +00:00
class Smartfile {
2017-04-27 14:48:08 +00:00
/**
* the constructor of Smartfile
* @param optionsArg
*/
constructor(optionsArg) {
if (optionsArg.contentBuffer) {
2017-04-29 15:20:09 +00:00
this.contentBuffer = optionsArg.contentBuffer;
}
2017-04-29 15:20:09 +00:00
else if (optionsArg.contentString) {
2017-04-30 16:13:17 +00:00
this.setContentsFromString(optionsArg.contentString);
2017-04-29 15:20:09 +00:00
}
else {
console.log('created empty Smartfile?');
}
this.path = optionsArg.path;
2017-05-07 21:00:56 +00:00
this.parsedPath = plugins.path.parse(this.path);
2017-04-30 16:13:17 +00:00
this.base = optionsArg.base;
2017-04-27 14:48:08 +00:00
}
/**
* set contents from string
* @param contentString
*/
2017-04-27 14:48:08 +00:00
setContentsFromString(contentString) {
this.contents = new Buffer(contentString);
}
2017-04-27 14:48:08 +00:00
/**
* write file to disk
2018-02-16 20:57:44 +00:00
* Behaviours:
* - no argument write to exactly where the file was picked up
2017-04-27 14:48:08 +00:00
*/
2018-02-16 20:57:44 +00:00
write(pathArg) {
2017-04-27 14:48:08 +00:00
return __awaiter(this, void 0, void 0, function* () {
2018-02-16 20:57:44 +00:00
const stringToWrite = this.contentBuffer.toString();
yield memory.toFs(stringToWrite, this.path);
2017-04-27 14:48:08 +00:00
});
}
/**
* read file from disk
*/
read() {
2018-07-03 22:59:39 +00:00
return __awaiter(this, void 0, void 0, function* () { });
2017-04-27 14:48:08 +00:00
}
2017-04-30 16:13:17 +00:00
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents() {
return this.contentBuffer;
}
set contents(contentsArg) {
this.contentBuffer = contentsArg;
}
/**
* vinyl-compatibility
*/
get cwd() {
2017-05-01 20:27:15 +00:00
return process.cwd();
2017-04-30 16:13:17 +00:00
}
/**
* return relative path of file
*/
get relative() {
return plugins.path.relative(this.base, this.path);
2017-04-30 16:13:17 +00:00
}
/**
* return truw when the file has content
*/
isNull() {
if (!this.contentBuffer) {
return true;
}
return false;
}
/**
* return true if contents are Buffer
*/
isBuffer() {
if (this.contents instanceof Buffer) {
return true;
}
return false;
}
isDirectory() {
return false;
}
isStream() {
return false;
}
isSymbolic() {
return false;
}
2017-05-27 21:47:39 +00:00
// update things
updateFileName(fileNameArg) {
let oldFileName = this.parsedPath.base;
2017-05-27 22:12:02 +00:00
this.path = this.path.replace(new RegExp(oldFileName + '$'), fileNameArg);
2017-05-27 21:47:39 +00:00
}
2016-07-17 15:34:15 +00:00
}
exports.Smartfile = Smartfile;
2018-07-03 22:59:39 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSwrQ0FBK0M7QUFFL0MsNkNBQTZDO0FBUzdDOzs7R0FHRztBQUNIO0lBMkJFOzs7T0FHRztJQUVILFl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