smartfile/ts/smartfile.memory.ts

150 lines
4.3 KiB
TypeScript
Raw Normal View History

2016-09-20 15:56:49 +00:00
import 'typings-global'
import plugins = require('./smartfile.plugins')
import SmartfileInterpreter = require('./smartfile.interpreter')
let vinyl = require('vinyl')
export interface vinyl {
contents: Buffer
base: string
path: string,
}
2016-09-20 15:56:49 +00:00
let Readable = require('stream').Readable
2016-04-02 21:03:18 +00:00
/**
* allows you to create a gulp stream
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
2016-04-02 21:03:18 +00:00
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
export let toGulpStream = function(fileArg: string|string[]|vinyl|vinyl[],baseArg: string = '/'){
2016-09-20 15:56:49 +00:00
let fileArray = []
2016-04-02 21:03:18 +00:00
if (typeof fileArg === 'string' || fileArg instanceof vinyl) { // make sure we work with an array later on
2016-09-20 15:56:49 +00:00
fileArray.push(fileArg)
} else if (Array.isArray(fileArg)) {
fileArray = fileArg
2016-04-02 21:03:18 +00:00
} else {
2016-09-20 15:56:49 +00:00
throw new Error('fileArg has unknown format')
2016-04-02 21:03:18 +00:00
}
let vinylFileArray: vinyl[] = [] // we want to have an array of vinylFiles
2016-04-02 21:03:18 +00:00
2016-09-20 15:56:49 +00:00
for (let fileIndexArg in fileArray) { // convert fileArray in vinylArray
let file = fileArray[fileIndexArg]
file instanceof vinyl ?
2016-04-02 21:03:18 +00:00
vinylFileArray.push(file) :
2016-09-20 15:56:49 +00:00
vinylFileArray.push(toVinylFileSync(file,{filename: fileIndexArg,base: baseArg}))
2016-04-02 21:03:18 +00:00
};
2016-09-20 15:56:49 +00:00
let stream = new Readable({ objectMode: true })
for (let vinylFileIndexArg in vinylFileArray) {
let vinylFile = vinylFileArray[vinylFileIndexArg]
stream.push(vinylFile)
2016-04-02 21:03:18 +00:00
};
2016-09-20 15:56:49 +00:00
stream.push(null) // signal end of stream;
return stream
}
2016-04-02 21:03:18 +00:00
/**
2016-04-09 20:51:30 +00:00
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
2016-09-20 15:56:49 +00:00
export let toObject = function(fileStringArg: string,fileTypeArg: string){
return SmartfileInterpreter.objectFile(fileStringArg,fileTypeArg)
}
2016-04-02 21:03:18 +00:00
/**
* 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}) {
2016-09-20 15:56:49 +00:00
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({
2016-04-02 21:03:18 +00:00
base: optionsArg.base,
path: plugins.path.join(optionsArg.base,optionsArg.relPath,optionsArg.filename),
contents: new Buffer(fileArg)
2016-09-20 15:56:49 +00:00
})
return vinylFile
}
2016-04-02 21:03:18 +00:00
/**
* 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
}
){
2016-09-20 15:56:49 +00:00
let vinylArray = []
for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg]
vinylArray.push(toVinylFileSync(myString,optionsArg))
2016-04-02 21:03:18 +00:00
}
2016-09-20 15:56:49 +00:00
return vinylArray
}
2016-04-02 21:03:18 +00:00
2016-04-04 13:44:00 +00:00
/**
* takes a vinylFile object and converts it to String
*/
export let vinylToStringSync = function(fileArg: vinyl){
2016-09-20 15:56:49 +00:00
return fileArg.contents.toString('utf8')
}
2016-04-02 21:03:18 +00:00
2016-04-04 13:44:00 +00:00
/**
* writes string or vinyl file to disk.
* @param fileArg
* @param fileNameArg
* @param fileBaseArg
*/
2016-09-20 15:56:49 +00:00
export let toFs = function(fileContentArg: string|vinyl,filePathArg){
let done = plugins.q.defer()
2016-04-04 13:44:00 +00:00
2016-09-20 15:56:49 +00:00
// function checks to abort if needed
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
}
2016-04-04 13:44:00 +00:00
2016-06-24 01:36:51 +00:00
// prepare actual write action
2016-09-20 15:56:49 +00:00
let fileString: string
let filePath: string = filePathArg
if (fileContentArg instanceof vinyl) {
fileString = vinylToStringSync(fileContentArg)
2016-09-20 15:56:49 +00:00
} 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')
2016-04-02 21:03:18 +00:00
}
2016-04-04 13:44:00 +00:00
2016-06-24 01:36:51 +00:00
// prepare actual write action
2016-09-20 15:56:49 +00:00
let fileString: string
let filePath: string = filePathArg
2016-04-04 13:44:00 +00:00
if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg)
2016-09-20 15:56:49 +00:00
} else if (typeof fileArg === 'string') {
fileString = fileArg
2016-04-04 13:44:00 +00:00
}
2016-09-20 15:56:49 +00:00
plugins.fsExtra.writeFileSync(filePath,fileString,'utf8')
}