smartfile/ts/smartfile.memory.ts

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2016-09-20 15:56:49 +00:00
import 'typings-global'
import plugins = require('./smartfile.plugins')
2017-05-26 12:47:41 +00:00
import { Smartfile } from './smartfile.classes.smartfile'
import * as smartfileFs from './smartfile.fs'
2017-05-26 12:47:41 +00:00
import SmartfileInterpreter = require('./smartfile.interpreter')
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
}
2017-05-26 12:47:41 +00:00
export interface IToFsOptions {
respectRelative?: boolean
}
2016-04-02 21:03:18 +00:00
2016-04-04 13:44:00 +00:00
/**
* writes string or vinyl file to disk.
2017-05-26 12:47:41 +00:00
* @param fileArg
2016-04-04 13:44:00 +00:00
* @param fileNameArg
* @param fileBaseArg
*/
2017-05-26 12:47:41 +00:00
export let toFs = async (fileContentArg: string | Smartfile, filePathArg, optionsArg: IToFsOptions = {} ) => {
2017-04-29 14:50:06 +00:00
let done = plugins.q.defer()
2017-05-26 12:47:41 +00:00
// check args
2017-04-29 14:50:06 +00:00
if (!fileContentArg || !filePathArg) {
throw new Error('expected valid arguments')
}
// prepare actual write action
let fileString: string
let filePath: string = filePathArg
2017-05-26 12:47:41 +00:00
// handle Smartfile
if (fileContentArg instanceof Smartfile) {
2017-04-29 14:50:06 +00:00
let fileContentArg2: any = fileContentArg
2017-05-26 12:47:41 +00:00
fileString = fileContentArg.contentBuffer.toString()
// handle options
optionsArg.respectRelative ? filePath = plugins.path.join(filePath, fileContentArg.path) : null
2017-04-29 14:50:06 +00:00
} else if (typeof fileContentArg === 'string') {
fileString = fileContentArg
2017-05-26 12:47:41 +00:00
} else {
throw new Error('fileContent is neither string nor Smartfile')
2017-04-29 14:50:06 +00:00
}
plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve)
2017-05-26 12:47:41 +00:00
return await done.promise
2016-09-20 15:56:49 +00:00
}
2017-05-26 12:47:41 +00:00
/**
* writes a string or a Smartfile to disk synchronously, only supports string
* @param fileArg
* @param filePathArg
*/
export let toFsSync = function (fileArg: string, filePathArg: string) {
2017-04-29 14:50:06 +00:00
// 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') {
2017-05-26 12:47:41 +00:00
throw new Error('fileArg is not of type String.')
2017-04-29 14:50:06 +00:00
} else if (typeof fileArg === 'string') {
fileString = fileArg
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8')
2016-09-20 15:56:49 +00:00
}
2017-05-26 12:47:41 +00:00
export let smartfileArrayToFs = async (smartfileArrayArg: Smartfile[], dirArg: string) => {
await smartfileFs.ensureDir(dirArg)
for(let smartfile of smartfileArrayArg) {
await toFs(smartfile, dirArg, {
respectRelative: true
})
}
}