smartfile/dist/smartfile.memory.js

87 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-04-02 21:03:18 +00:00
"use strict";
2017-05-26 12:47:41 +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 });
2016-07-17 15:34:15 +00:00
const plugins = require("./smartfile.plugins");
2017-05-26 12:47:41 +00:00
const smartfile_classes_smartfile_1 = require("./smartfile.classes.smartfile");
const smartfileFs = require("./smartfile.fs");
2016-07-17 15:34:15 +00:00
const SmartfileInterpreter = require("./smartfile.interpreter");
/**
2016-04-09 20:51:30 +00:00
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
exports.toObject = function (fileStringArg, fileTypeArg) {
2016-06-23 16:31:55 +00:00
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
};
2016-04-04 13:44:00 +00:00
/**
2017-08-02 11:10:30 +00:00
* writes string or Smartfile to disk.
2016-04-04 13:44:00 +00:00
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
2017-05-26 12:47:41 +00:00
exports.toFs = (fileContentArg, filePathArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
let done = plugins.smartpromise.defer();
2017-05-26 12:47:41 +00:00
// check args
2016-09-20 15:56:49 +00:00
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments');
}
2016-06-24 01:36:51 +00:00
// prepare actual write action
2016-07-17 15:34:15 +00:00
let fileString;
let filePath = filePathArg;
2017-05-26 12:47:41 +00:00
// handle Smartfile
if (fileContentArg instanceof smartfile_classes_smartfile_1.Smartfile) {
2017-01-20 23:47:48 +00:00
let fileContentArg2 = fileContentArg;
2017-05-26 12:47:41 +00:00
fileString = fileContentArg.contentBuffer.toString();
// handle options
if (optionsArg.respectRelative) {
filePath = plugins.path.join(filePath, fileContentArg.path);
}
2016-04-04 13:44:00 +00:00
}
2016-09-20 15:56:49 +00:00
else if (typeof fileContentArg === 'string') {
2016-06-24 01:36:51 +00:00
fileString = fileContentArg;
2016-04-02 21:03:18 +00:00
}
2017-05-26 12:47:41 +00:00
else {
throw new Error('fileContent is neither string nor Smartfile');
}
yield smartfileFs.ensureDir(plugins.path.parse(filePath).dir);
2017-07-30 20:53:54 +00:00
plugins.fsExtra.writeFile(filePath, fileString, { encoding: 'utf8' }, done.resolve);
2017-05-26 12:47:41 +00:00
return yield done.promise;
});
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
*/
2016-06-24 01:36:51 +00:00
exports.toFsSync = function (fileArg, filePathArg) {
2016-09-20 15:56:49 +00:00
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments');
}
2016-06-24 01:36:51 +00:00
// prepare actual write action
2016-07-17 15:34:15 +00:00
let fileString;
let filePath = filePathArg;
if (typeof fileArg !== 'string') {
2017-05-26 12:47:41 +00:00
throw new Error('fileArg is not of type String.');
2016-04-04 13:44:00 +00:00
}
2016-09-20 15:56:49 +00:00
else if (typeof fileArg === 'string') {
2016-04-04 13:44:00 +00:00
fileString = fileArg;
}
2017-07-30 20:53:54 +00:00
plugins.fsExtra.writeFileSync(filePath, fileString, { encoding: 'utf8' });
2016-04-02 21:03:18 +00:00
};
2017-05-26 12:47:41 +00:00
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
});
}
});
2018-07-03 22:59:39 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9u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