smartfile/dist/smartfile.memory.js

88 lines
6.2 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 });
require("typings-global");
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* () {
2016-09-17 21:11:44 +00:00
let done = plugins.q.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
});
}
});
2017-07-30 20:53:54 +00:00
//# sourceMappingURL=data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