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-05-23 06:15:47 +00:00
|
|
|
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-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
|
|
|
/**
|
|
|
|
* writes string or vinyl file to disk.
|
|
|
|
* @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
|
|
|
|
optionsArg.respectRelative ? filePath = plugins.path.join(filePath, fileContentArg.path) : null;
|
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');
|
|
|
|
}
|
2016-09-20 15:56:49 +00:00
|
|
|
plugins.fsExtra.writeFile(filePath, fileString, '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;
|
|
|
|
}
|
2016-09-20 15:56:49 +00:00
|
|
|
plugins.fsExtra.writeFileSync(filePath, fileString, '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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLDBCQUF1QjtBQUV2QiwrQ0FBK0M7QUFDL0MsK0VBQXlEO0FBQ3pELDhDQUE2QztBQUc3QyxnRUFBZ0U7QUFFaEU7Ozs7O0dBS0c7QUFDUSxRQUFBLFFBQVEsR0FBRyxVQUFVLGFBQXFCLEVBQUUsV0FBbUI7SUFDeEUsTUFBTSxDQUFDLG9CQUFvQixDQUFDLFVBQ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