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; base: string;
path: 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 * converts file to Object
* @param fileStringArg * @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' import * as vinyl from 'vinyl'
// ---------------------------
// smartfile.fs
// ---------------------------
tap.test('.fs.fileExistsSync -> should return an accurate boolean', async () => { 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/mytest.json')).to.be.true
expect(smartfile.fs.fileExistsSync('./test/notthere.json')).be.false 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 () => { 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 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 () => { tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString' let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' } let localOptions = { filename: 'vinylfile2', base: '/someDir' }

View File

@ -10,52 +10,14 @@ export interface IVinylFile {
path: 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
}
/** /**
* converts file to Object * converts file to Object
* @param fileStringArg * @param fileStringArg
* @param fileTypeArg * @param fileTypeArg
* @returns {any|any} * @returns {any|any}
*/ */
export let toObject = function(fileStringArg: string,fileTypeArg: string){ export let toObject = function (fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg,fileTypeArg) return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg)
} }
/** /**
@ -63,14 +25,14 @@ export let toObject = function(fileStringArg: string,fileTypeArg: string){
* @param fileArg * @param fileArg
* @param optionsArg * @param optionsArg
*/ */
export let toVinylFileSync = function(fileArg: string,optionsArg?: {filename?: string,base?: string,relPath?: string}) { export let toVinylFileSync = function (fileArg: string, optionsArg?: { filename?: string, base?: string, relPath?: string }) {
optionsArg ? void(0) : optionsArg = {filename: 'vinylfile', base: '/'} optionsArg ? void (0) : optionsArg = { filename: 'vinylfile', base: '/' }
optionsArg.filename ? void(0) : optionsArg.filename = 'vinylfile' optionsArg.filename ? void (0) : optionsArg.filename = 'vinylfile'
optionsArg.base ? void(0) : optionsArg.base = '/' optionsArg.base ? void (0) : optionsArg.base = '/'
optionsArg.relPath ? void('0') : optionsArg.relPath = '' optionsArg.relPath ? void ('0') : optionsArg.relPath = ''
let vinylFile = new vinyl({ let vinylFile = new vinyl({
base: optionsArg.base, base: optionsArg.base,
path: plugins.path.join(optionsArg.base,optionsArg.relPath,optionsArg.filename), path: plugins.path.join(optionsArg.base, optionsArg.relPath, optionsArg.filename),
contents: new Buffer(fileArg) contents: new Buffer(fileArg)
}) })
return vinylFile return vinylFile
@ -81,18 +43,18 @@ export let toVinylFileSync = function(fileArg: string,optionsArg?: {filename?: s
* @param arrayArg * @param arrayArg
* @param optionsArg * @param optionsArg
*/ */
export let toVinylArraySync = function( export let toVinylArraySync = function (
arrayArg: string[], arrayArg: string[],
optionsArg?: { optionsArg?: {
filename?: string, filename?: string,
base?: string, base?: string,
relPath?: string relPath?: string
} }
){ ) {
let vinylArray = [] let vinylArray = []
for (let stringIndexArg in arrayArg) { for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg] let myString = arrayArg[ stringIndexArg ]
vinylArray.push(toVinylFileSync(myString,optionsArg)) vinylArray.push(toVinylFileSync(myString, optionsArg))
} }
return vinylArray return vinylArray
} }
@ -100,7 +62,7 @@ export let toVinylArraySync = function(
/** /**
* takes a vinylFile object and converts it to String * takes a vinylFile object and converts it to String
*/ */
export let vinylToStringSync = function(fileArg: IVinylFile): string { export let vinylToStringSync = function (fileArg: IVinylFile): string {
return fileArg.contents.toString('utf8') return fileArg.contents.toString('utf8')
} }
@ -110,7 +72,7 @@ export let vinylToStringSync = function(fileArg: IVinylFile): string {
* @param fileNameArg * @param fileNameArg
* @param fileBaseArg * @param fileBaseArg
*/ */
export let toFs = function(fileContentArg: string|IVinylFile,filePathArg){ export let toFs = function (fileContentArg: string | IVinylFile, filePathArg) {
let done = plugins.q.defer() let done = plugins.q.defer()
// function checks to abort if needed // function checks to abort if needed
@ -127,11 +89,11 @@ export let toFs = function(fileContentArg: string|IVinylFile,filePathArg){
} else if (typeof fileContentArg === 'string') { } else if (typeof fileContentArg === 'string') {
fileString = fileContentArg fileString = fileContentArg
} }
plugins.fsExtra.writeFile(filePath,fileString,'utf8',done.resolve) plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve)
return done.promise return done.promise
} }
export let toFsSync = function(fileArg,filePathArg: string){ export let toFsSync = function (fileArg, filePathArg: string) {
// function checks to abort if needed // function checks to abort if needed
if (!fileArg || !filePathArg) { if (!fileArg || !filePathArg) {
throw new Error('expected a valid arguments') throw new Error('expected a valid arguments')
@ -146,5 +108,5 @@ export let toFsSync = function(fileArg,filePathArg: string){
} else if (typeof fileArg === 'string') { } else if (typeof fileArg === 'string') {
fileString = fileArg fileString = fileArg
} }
plugins.fsExtra.writeFileSync(filePath,fileString,'utf8') plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
} }