smartfile/ts/smartfile.memory.ts

113 lines
3.0 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')
2017-01-20 23:47:48 +00:00
export interface IVinylFile {
2017-04-29 14:50:06 +00:00
contents: Buffer
base: string
path: string,
2016-09-20 15:56:49 +00:00
}
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}
*/
2017-04-29 14:50:06 +00:00
export let toObject = function (fileStringArg: string, fileTypeArg: string) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg)
2016-09-20 15:56:49 +00:00
}
2016-04-02 21:03:18 +00:00
/**
* takes a string and converts it to vinyl file
* @param fileArg
* @param optionsArg
*/
2017-04-29 14:50:06 +00:00
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
}
2016-04-02 21:03:18 +00:00
/**
* takes a string array and some options and returns a vinylfile array
* @param arrayArg
* @param optionsArg
*/
2017-04-29 14:50:06 +00:00
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
2016-09-20 15:56:49 +00:00
}
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
*/
2017-04-29 14:50:06 +00:00
export let vinylToStringSync = function (fileArg: IVinylFile): string {
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
*/
2017-04-29 14:50:06 +00:00
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')
}
// 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
2016-09-20 15:56:49 +00:00
}
2017-04-29 14:50:06 +00:00
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
if (typeof fileArg !== 'string') {
fileString = vinylToStringSync(fileArg)
} else if (typeof fileArg === 'string') {
fileString = fileArg
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
2016-09-20 15:56:49 +00:00
}