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());
|
|
|
|
});
|
|
|
|
};
|
2017-03-04 20:10:46 +00:00
|
|
|
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-07 21:38:24 +00:00
|
|
|
/**
|
2016-04-09 20:51:30 +00:00
|
|
|
* converts file to Object
|
2016-04-07 21:38:24 +00:00
|
|
|
* @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-07 21:38:24 +00:00
|
|
|
};
|
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* () {
|
2018-07-02 22:15:00 +00:00
|
|
|
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
|
2017-05-27 00:05:31 +00:00
|
|
|
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');
|
|
|
|
}
|
2017-05-27 00:05:31 +00:00
|
|
|
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;
|
2017-01-01 01:45:53 +00:00
|
|
|
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-02 22:15:00 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywrRUFBeUQ7QUFDekQsOENBQTZDO0FBRzdDLGdFQUFnRTtBQUVoRTs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