now writes smartfile arrays to disk

This commit is contained in:
Philipp Kunz 2017-05-26 14:47:41 +02:00
parent 7694cc9c08
commit 72bb3f33ac
5 changed files with 100 additions and 175 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,5 @@
/// <reference types="node" />
import 'typings-global';
export interface IVinylFile {
contents: Buffer;
base: string;
path: string;
}
import { Smartfile } from './smartfile.classes.smartfile';
/**
* converts file to Object
* @param fileStringArg
@ -12,35 +7,20 @@ export interface IVinylFile {
* @returns {any|any}
*/
export declare let toObject: (fileStringArg: string, fileTypeArg: string) => any;
/**
* takes a string and converts it to vinyl file
* @param fileArg
* @param optionsArg
*/
export declare let toVinylFileSync: (fileArg: string, optionsArg?: {
filename?: string;
base?: string;
relPath?: string;
}) => any;
/**
* takes a string array and some options and returns a vinylfile array
* @param arrayArg
* @param optionsArg
*/
export declare let toVinylArraySync: (arrayArg: string[], optionsArg?: {
filename?: string;
base?: string;
relPath?: string;
}) => any[];
/**
* takes a vinylFile object and converts it to String
*/
export declare let vinylToStringSync: (fileArg: IVinylFile) => string;
export interface IToFsOptions {
respectRelative?: boolean;
}
/**
* writes string or vinyl file to disk.
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
export declare let toFs: (fileContentArg: string | IVinylFile, filePathArg: any) => Promise<{}>;
export declare let toFsSync: (fileArg: any, filePathArg: string) => void;
export declare let toFs: (fileContentArg: string | Smartfile, filePathArg: any, optionsArg?: IToFsOptions) => Promise<{}>;
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
*/
export declare let toFsSync: (fileArg: string, filePathArg: string) => void;
export declare let smartfileArrayToFs: (smartfileArrayArg: Smartfile[], dirArg: string) => Promise<void>;

View File

@ -1,9 +1,18 @@
"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");
let vinyl = require('vinyl');
/**
* converts file to Object
* @param fileStringArg
@ -13,67 +22,42 @@ let vinyl = require('vinyl');
exports.toObject = function (fileStringArg, fileTypeArg) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
};
/**
* takes a string and converts it to vinyl file
* @param fileArg
* @param optionsArg
*/
exports.toVinylFileSync = function (fileArg, optionsArg) {
optionsArg ? void (0) : optionsArg = { filename: 'vinylfile', base: '/' };
optionsArg.filename ? void (0) : optionsArg.filename = 'vinylfile';
optionsArg.base ? void (0) : optionsArg.base = '/';
optionsArg.relPath ? void ('0') : optionsArg.relPath = '';
let vinylFile = new vinyl({
base: optionsArg.base,
path: plugins.path.join(optionsArg.base, optionsArg.relPath, optionsArg.filename),
contents: new Buffer(fileArg)
});
return vinylFile;
};
/**
* takes a string array and some options and returns a vinylfile array
* @param arrayArg
* @param optionsArg
*/
exports.toVinylArraySync = function (arrayArg, optionsArg) {
let vinylArray = [];
for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg];
vinylArray.push(exports.toVinylFileSync(myString, optionsArg));
}
return vinylArray;
};
/**
* takes a vinylFile object and converts it to String
*/
exports.vinylToStringSync = function (fileArg) {
return fileArg.contents.toString('utf8');
};
/**
* writes string or vinyl file to disk.
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
exports.toFs = function (fileContentArg, filePathArg) {
exports.toFs = (fileContentArg, filePathArg, optionsArg = {}) => __awaiter(this, void 0, void 0, function* () {
let done = plugins.q.defer();
// function checks to abort if needed
// check args
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments');
}
// prepare actual write action
let fileString;
let filePath = filePathArg;
if (vinyl.isVinyl(fileContentArg)) {
// handle Smartfile
if (fileContentArg instanceof smartfile_classes_smartfile_1.Smartfile) {
let fileContentArg2 = fileContentArg;
fileString = exports.vinylToStringSync(fileContentArg2);
fileString = fileContentArg.contentBuffer.toString();
// handle options
optionsArg.respectRelative ? filePath = plugins.path.join(filePath, fileContentArg.path) : null;
}
else if (typeof fileContentArg === 'string') {
fileString = fileContentArg;
}
else {
throw new Error('fileContent is neither string nor Smartfile');
}
plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve);
return done.promise;
};
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) {
@ -83,11 +67,19 @@ exports.toFsSync = function (fileArg, filePathArg) {
let fileString;
let filePath = filePathArg;
if (typeof fileArg !== 'string') {
fileString = exports.vinylToStringSync(fileArg);
throw new Error('fileArg is not of type String.');
}
else if (typeof fileArg === 'string') {
fileString = fileArg;
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8');
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwwQkFBdUI7QUFFdkIsK0NBQStDO0FBQy9DLGdFQUFnRTtBQUNoRSxJQUFJLEtBQUssR0FBRyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUE7QUFRNUI7Ozs7O0dBS0c7QUFDUSxRQUFBLFFBQVEsR0FBRyxVQUFVLGFBQXFCLEVBQUUsV0FBbUI7SUFDeEUsTUFBTSxDQUFDLG9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xDQUFDLE9BQU8sQ0FBQyxhQUFhLENBQUMsUUFBUSxFQUFFLFVBQVUsRUFBRSxNQUFNLENBQUMsQ0FBQTtBQUM3RCxDQUFDLENBQUEifQ==
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLDBCQUF1QjtBQUV2QiwrQ0FBK0M7QUFDL0MsK0VBQXlEO0FBQ3pELDhDQUE2QztBQUc3QyxnRUFBZ0U7QUFFaEU7Ozs7O0dBS0c7QUFDUSxRQUFBLFFBQVEsR0FBRyxVQUFVLGFBQXFCLEVBQUUsV0FBbUI7SUFDeEUsTUFBTSxDQUFDLG9CQUFvQixDQUFDLFVBQVUsQ0FBQyxhQUFhLEVBQUUsV0FBVyxDQUFDLENBQUE7QUFDcEUsQ0FBQyxDQUFBO0FBTUQ7Ozs7O0dBS0c7QUFDUSxRQUFBLElBQUksR0FBRyxDQUFPLGNBQWtDLEVBQUUsV0FBVyxFQUFFLGFBQTJCLEVBQUU7SUFDckcsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUU1QixhQUFhO0lBQ2IsRUFBRSxDQUFDLENBQUMsQ0FBQyxjQUFjLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQyxDQUFDO1FBQ3BDLE1BQU0sSUFBSSxLQUFLLENBQUMsMEJBQTBCLENBQUMsQ0FBQTtJQUM3Qyx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yxJQUFJLENBQUMsV0FBVyxDQUFDLENBQUMsQ0FBQztRQUM3QixNQUFNLElBQUksS0FBSyxDQUFDLDRCQUE0QixDQUFDLENBQUE7SUFDL0MsQ0FBQztJQUVELDhCQUE4QjtJQUM5QixJQUFJLFVBQWtCLENBQUE7SUFDdEIsSUFBSSxRQUFRLEdBQVcsV0FBVyxDQUFBO0lBRWxDLEVBQUUsQ0FBQyxDQUFDLE9BQU8sT0FBTyxLQUFLLFFBQVEsQ0FBQyxDQUFDLENBQUM7UUFDaEMsTUFBTSxJQUFJLEtBQUssQ0FBQyxnQ0FBZ0MsQ0FBQyxDQUFBO0lBQ25ELENBQUM7SUFBQyxJQUFJLENBQUMsRUFBRSxDQUFDLENBQUMsT0FBTyxPQUFPLEtBQUssUUFBUSxDQUFDLENBQUMsQ0FBQztRQUN2QyxVQUFVLEdBQUcsT0FBTyxDQUFBO0lBQ3RCLENBQUM7SUFDRCxPQUFPLENBQUMsT0FBTyxDQUFDLGFBQWEsQ0FBQyxRQUFRLEVBQUUsVUFBVSxFQUFFLE1BQU0sQ0FBQyxDQUFBO0FBQzdELENBQUMsQ0FBQTtBQUVVLFFBQUEsa0JBQWtCLEdBQUcsQ0FBTyxpQkFBOEIsRUFBRSxNQUFjO0lBQ25GLE1BQU0sV0FBVyxDQUFDLFNBQVMsQ0FBQyxNQUFNLENBQUMsQ0FBQTtJQUNuQyxHQUFHLENBQUEsQ0FBQyxJQUFJLFNBQVMsSUFBSSxpQkFBaUIsQ0FBQyxDQUFDLENBQUM7UUFDdkMsTUFBTSxZQUFJLENBQUMsU0FBUyxFQUFFLE1BQU0sRUFBRTtZQUM1QixlQUFlLEVBQUUsSUFBSTtTQUN0QixDQUFDLENBQUE7SUFDSixDQUFDO0FBQ0gsQ0FBQyxDQUFBLENBQUEifQ==

View File

@ -3,8 +3,6 @@ import path = require('path')
import { expect, tap } from 'tapbundle'
import * as vinyl from 'vinyl'
// ---------------------------
// smartfile.fs
// ---------------------------
@ -53,8 +51,8 @@ tap.test('.fs.listFileTree() -> should get a file tree', async () => {
tap.test('.fs.fileTreeToObject -> should read a file tree into an Object', async () => {
let fileArrayArg = await smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
expect(fileArrayArg[0]).to.be.instanceof(smartfile.Smartfile)
expect(fileArrayArg[0].contents.toString()).to.equal(fileArrayArg[0].contentBuffer.toString())
expect(fileArrayArg[ 0 ]).to.be.instanceof(smartfile.Smartfile)
expect(fileArrayArg[ 0 ].contents.toString()).to.equal(fileArrayArg[ 0 ].contentBuffer.toString())
})
tap.test('.fs.copy() -> should copy a directory', async () => {
@ -117,11 +115,6 @@ tap.test('.fs.toStringSync() -> should read a file to a string', async () => {
.to.equal('Some TestString &&%$')
})
tap.test('.fs.toVinylSync -> should read an ' + '.json OR .yaml' + ' file to an ' + 'vinyl file object', async () => {
let testData = smartfile.fs.toVinylSync('./test/mytest.json')
expect(vinyl.isVinyl(testData)).to.be.true
})
// ---------------------------
// smartfile.interpreter
// ---------------------------
@ -134,32 +127,6 @@ tap.test('.interpreter.filetype() -> should get the file type from a string', as
// smartfile.memory
// ---------------------------
tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' }
expect(smartfile.memory.toVinylFileSync(localString, localOptions) instanceof vinyl).to.be.true
})
tap.test('.memory.toVinylArraySync() -> should produce a an array of vinylfiles', async () => {
let localStringArray = [ 'string1', 'string2', 'string3' ]
let localOptions = { filename: 'vinylfile2', base: '/someDir' }
let testResult = smartfile.memory.toVinylArraySync(localStringArray, localOptions)
expect(testResult).to.be.a('array')
expect(testResult.length === 3).to.be.true
for (let myKey in testResult) {
expect(testResult[ myKey ] instanceof vinyl).to.be.true
}
})
tap.test('.memory.vinylToStringSync() -> should produce a String from vinyl file', async () => {
let localString = smartfile.memory.vinylToStringSync(new vinyl({
base: '/',
path: '/test.txt',
contents: new Buffer('myString')
}))
expect(localString).equal('myString')
})
tap.test('.memory.toFs() -> should write a file to disk and return a promise', async () => {
let localString = 'myString'
await smartfile.memory.toFs(
@ -194,7 +161,7 @@ tap.test('.remote.toString() -> should reject a Promise when the link is false',
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject(process.cwd(), './test/testfolder/**/*')
let localSmartfile = smartfileArray[0]
let localSmartfile = smartfileArray[ 0 ]
expect(localSmartfile).to.be.instanceof(smartfile.Smartfile)
expect(localSmartfile.contents).to.be.instanceof(Buffer)
expect(localSmartfile.isBuffer()).to.be.true
@ -202,4 +169,15 @@ tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
expect(localSmartfile.isNull()).to.be.false
})
tap.test('should output a smartfile array to disk', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject('./test/testfolder/', '*')
for (let smartfile of smartfileArray) {
// console.log(smartfile.relative)
// console.log(smartfile.path)
// console.log(smartfile.base)
// console.log(smartfile.parsedPath)
}
await smartfile.memory.smartfileArrayToFs(smartfileArray, path.resolve('./test/temp/testoutput/'))
})
tap.start()

View File

@ -1,14 +1,11 @@
import 'typings-global'
import plugins = require('./smartfile.plugins')
import SmartfileInterpreter = require('./smartfile.interpreter')
let vinyl = require('vinyl')
import { Smartfile } from './smartfile.classes.smartfile'
import * as smartfileFs from './smartfile.fs'
export interface IVinylFile {
contents: Buffer
base: string
path: string,
}
import SmartfileInterpreter = require('./smartfile.interpreter')
/**
* converts file to Object
@ -20,50 +17,8 @@ export let toObject = function (fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg)
}
/**
* takes a string and converts it to vinyl file
* @param fileArg
* @param optionsArg
*/
export let toVinylFileSync = function (fileArg: string, optionsArg?: { filename?: string, base?: string, relPath?: string }) {
optionsArg ? void (0) : optionsArg = { filename: 'vinylfile', base: '/' }
optionsArg.filename ? void (0) : optionsArg.filename = 'vinylfile'
optionsArg.base ? void (0) : optionsArg.base = '/'
optionsArg.relPath ? void ('0') : optionsArg.relPath = ''
let vinylFile = new vinyl({
base: optionsArg.base,
path: plugins.path.join(optionsArg.base, optionsArg.relPath, optionsArg.filename),
contents: new Buffer(fileArg)
})
return vinylFile
}
/**
* takes a string array and some options and returns a vinylfile array
* @param arrayArg
* @param optionsArg
*/
export let toVinylArraySync = function (
arrayArg: string[],
optionsArg?: {
filename?: string,
base?: string,
relPath?: string
}
) {
let vinylArray = []
for (let stringIndexArg in arrayArg) {
let myString = arrayArg[ stringIndexArg ]
vinylArray.push(toVinylFileSync(myString, optionsArg))
}
return vinylArray
}
/**
* takes a vinylFile object and converts it to String
*/
export let vinylToStringSync = function (fileArg: IVinylFile): string {
return fileArg.contents.toString('utf8')
export interface IToFsOptions {
respectRelative?: boolean
}
/**
@ -72,10 +27,10 @@ export let vinylToStringSync = function (fileArg: IVinylFile): string {
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = function (fileContentArg: string | IVinylFile, filePathArg) {
export let toFs = async (fileContentArg: string | Smartfile, filePathArg, optionsArg: IToFsOptions = {} ) => {
let done = plugins.q.defer()
// function checks to abort if needed
// check args
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
}
@ -83,17 +38,29 @@ export let toFs = function (fileContentArg: string | IVinylFile, filePathArg) {
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
if (vinyl.isVinyl(fileContentArg)) {
// handle Smartfile
if (fileContentArg instanceof Smartfile) {
let fileContentArg2: any = fileContentArg
fileString = vinylToStringSync(fileContentArg2)
fileString = fileContentArg.contentBuffer.toString()
// handle options
optionsArg.respectRelative ? filePath = plugins.path.join(filePath, fileContentArg.path) : null
} else if (typeof fileContentArg === 'string') {
fileString = fileContentArg
} else {
throw new Error('fileContent is neither string nor Smartfile')
}
plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve)
return done.promise
return await done.promise
}
export let toFsSync = function (fileArg, filePathArg: string) {
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
*/
export let toFsSync = function (fileArg: string, filePathArg: string) {
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments')
@ -104,9 +71,18 @@ export let toFsSync = function (fileArg, filePathArg: string) {
let filePath: string = filePathArg
if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg)
throw new Error('fileArg is not of type String.')
} else if (typeof fileArg === 'string') {
fileString = fileArg
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
}
export let smartfileArrayToFs = async (smartfileArrayArg: Smartfile[], dirArg: string) => {
await smartfileFs.ensureDir(dirArg)
for(let smartfile of smartfileArrayArg) {
await toFs(smartfile, dirArg, {
respectRelative: true
})
}
}