smartfile/dist/smartfile.memory.js
2017-08-02 13:10:30 +02:00

88 lines
6.2 KiB
JavaScript

"use strict";
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 });
require("typings-global");
const plugins = require("./smartfile.plugins");
const smartfile_classes_smartfile_1 = require("./smartfile.classes.smartfile");
const smartfileFs = require("./smartfile.fs");
const SmartfileInterpreter = require("./smartfile.interpreter");
/**
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
exports.toObject = function (fileStringArg, fileTypeArg) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
};
/**
* writes string or Smartfile to disk.
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
exports.toFs = (fileContentArg, filePathArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
let done = plugins.q.defer();
// check args
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments');
}
// prepare actual write action
let fileString;
let filePath = filePathArg;
// handle Smartfile
if (fileContentArg instanceof smartfile_classes_smartfile_1.Smartfile) {
let fileContentArg2 = fileContentArg;
fileString = fileContentArg.contentBuffer.toString();
// handle options
if (optionsArg.respectRelative) {
filePath = plugins.path.join(filePath, fileContentArg.path);
}
}
else if (typeof fileContentArg === 'string') {
fileString = fileContentArg;
}
else {
throw new Error('fileContent is neither string nor Smartfile');
}
yield smartfileFs.ensureDir(plugins.path.parse(filePath).dir);
plugins.fsExtra.writeFile(filePath, fileString, { encoding: 'utf8' }, done.resolve);
return yield done.promise;
});
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
*/
exports.toFsSync = function (fileArg, filePathArg) {
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments');
}
// prepare actual write action
let fileString;
let filePath = filePathArg;
if (typeof fileArg !== 'string') {
throw new Error('fileArg is not of type String.');
}
else if (typeof fileArg === 'string') {
fileString = fileArg;
}
plugins.fsExtra.writeFileSync(filePath, fileString, { encoding: 'utf8' });
};
exports.smartfileArrayToFs = (smartfileArrayArg, dirArg) => __awaiter(this, void 0, void 0, function* () {
yield smartfileFs.ensureDir(dirArg);
for (let smartfile of smartfileArrayArg) {
yield exports.toFs(smartfile, dirArg, {
respectRelative: true
});
}
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLDBCQUF1QjtBQUV2QiwrQ0FBK0M7QUFDL0MsK0VBQXlEO0FBQ3pELDhDQUE2QztBQUc3QyxnRUFBZ0U7QUFFaEU7Ozs7O0dBS0c7QUFDUSxRQUFBLFFBQVEsR0FBRyxVQUFVLGFBQXFCLEVBQUUsV0FBbUI7SUFDeEUsTUFBTSxDQUFDLG9CQUFvQixDQUFDLFVBQVUsQ0FBQyxhQUFhLEVBQUUsV0FBVyxDQUFDLENBQUE7QUFDcEUsQ0FBQyxDQUFBO0FBTUQ7Ozs7O0dBS0c7QUFDUSxRQUFBLElBQUksR0FBRyxDQUFPLGNBQWtDLEVBQUUsV0FBVyxFQUFFLGFBQTJCLEVBQUU7SUFDckcsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUU1QixhQUFhO0lBQ2IsRUFBRSxDQUFDLENBQUMsQ0FBQyxjQUFjLEl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