handle gulp in seperate module

This commit is contained in:
Philipp Kunz 2017-04-29 16:50:06 +02:00
parent 7eed9dd6d3
commit ed01ebeee8
4 changed files with 69 additions and 152 deletions

View File

@ -5,14 +5,6 @@ export interface IVinylFile {
base: string;
path: string;
}
/**
* allows you to create a gulp stream
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
export declare let toGulpStream: (fileArg: string | string[] | IVinylFile | IVinylFile[], baseArg?: string) => any;
/**
* converts file to Object
* @param fileStringArg

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,10 @@ import { expect, tap } from 'tapbundle'
import * as vinyl from 'vinyl'
// ---------------------------
// smartfile.fs
// ---------------------------
tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => {
expect(smartfile.fs.fileExistsSync('./test/mytest.json')).to.be.true
expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false
@ -91,7 +95,7 @@ tap.test('smartfile.fs.removeManySync -> should remove and array of single files
})
// ---------------------------
// .interpreter
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
@ -125,11 +129,6 @@ tap.test('.fs.toVinylSync -> should read an ' + '.json OR .yaml' + ' file to an
expect(vinyl.isVinyl(testData)).to.be.true
})
tap.test('.memory.toGulpStream() -> should produce a valid gulp stream', async () => {
let localArray = [ 'test1', 'test2', 'test3' ]
smartfile.memory.toGulpStream(localArray)
})
tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' }

View File

@ -5,47 +5,9 @@ import SmartfileInterpreter = require('./smartfile.interpreter')
let vinyl = require('vinyl')
export interface IVinylFile {
contents: Buffer
base: string
path: string,
}
let Readable = require('stream').Readable
/**
* allows you to create a gulp stream
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
export let toGulpStream = function(fileArg: string|string[]|IVinylFile|IVinylFile[],baseArg: string = '/'){
let fileArray = []
if (typeof fileArg === 'string' || vinyl.isVinyl(fileArg)) { // make sure we work with an array later on
fileArray.push(fileArg)
} else if (Array.isArray(fileArg)) {
fileArray = fileArg
} else {
throw new Error('fileArg has unknown format')
}
let vinylFileArray: IVinylFile[] = [] // we want to have an array of vinylFiles
for (let fileIndexArg in fileArray) { // convert fileArray in vinylArray
let file = fileArray[fileIndexArg]
file instanceof vinyl ?
vinylFileArray.push(file) :
vinylFileArray.push(toVinylFileSync(file,{filename: fileIndexArg,base: baseArg}))
};
let stream = new Readable({ objectMode: true })
for (let vinylFileIndexArg in vinylFileArray) {
let vinylFile = vinylFileArray[vinylFileIndexArg]
stream.push(vinylFile)
};
stream.push(null) // signal end of stream;
return stream
contents: Buffer
base: string
path: string,
}
/**
@ -54,8 +16,8 @@ export let toGulpStream = function(fileArg: string|string[]|IVinylFile|IVinylFil
* @param fileTypeArg
* @returns {any|any}
*/
export let toObject = function(fileStringArg: string,fileTypeArg: string){
return SmartfileInterpreter.objectFile(fileStringArg,fileTypeArg)
export let toObject = function (fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg)
}
/**
@ -63,17 +25,17 @@ export let toObject = function(fileStringArg: string,fileTypeArg: string){
* @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
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
}
/**
@ -81,27 +43,27 @@ export let toVinylFileSync = function(fileArg: string,optionsArg?: {filename?: s
* @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
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 let vinylToStringSync = function (fileArg: IVinylFile): string {
return fileArg.contents.toString('utf8')
}
/**
@ -110,41 +72,41 @@ export let vinylToStringSync = function(fileArg: IVinylFile): string {
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = function(fileContentArg: string|IVinylFile,filePathArg){
let done = plugins.q.defer()
export let toFs = function (fileContentArg: string | IVinylFile, filePathArg) {
let done = plugins.q.defer()
// function checks to abort if needed
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
}
// function checks to abort if needed
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
}
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
if (vinyl.isVinyl(fileContentArg)) {
let fileContentArg2: any = fileContentArg
fileString = vinylToStringSync(fileContentArg2)
} else if (typeof fileContentArg === 'string') {
fileString = fileContentArg
}
plugins.fsExtra.writeFile(filePath,fileString,'utf8',done.resolve)
return done.promise
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
if (vinyl.isVinyl(fileContentArg)) {
let fileContentArg2: any = fileContentArg
fileString = vinylToStringSync(fileContentArg2)
} else if (typeof fileContentArg === 'string') {
fileString = fileContentArg
}
plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve)
return done.promise
}
export let toFsSync = function(fileArg,filePathArg: string){
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments')
}
export let toFsSync = function (fileArg, filePathArg: string) {
// function checks to abort if needed
if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments')
}
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg)
} else if (typeof fileArg === 'string') {
fileString = fileArg
}
plugins.fsExtra.writeFileSync(filePath,fileString,'utf8')
if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg)
} else if (typeof fileArg === 'string') {
fileString = fileArg
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
}