add ensureFile and ensureFileSync

This commit is contained in:
2016-09-24 21:42:45 +02:00
parent 760913bad3
commit 38354d2944
4 changed files with 114 additions and 40 deletions

View File

@ -2,7 +2,7 @@ import 'typings-global'
import plugins = require('./smartfile.plugins')
import SmartfileInterpreter = require('./smartfile.interpreter')
import * as memory from './smartfile.memory'
/*===============================================================
============================ Checks =============================
===============================================================*/
@ -54,22 +54,6 @@ export let isFile = function(pathArg): boolean{
============================ FS ACTIONS =========================
===============================================================*/
/**
* ensures that a directory is in place
*/
export let ensureDir = (dirPathArg: string) => {
let done = plugins.q.defer()
plugins.fsExtra.ensureDir(dirPathArg,done.resolve)
return done.promise
}
/**
* ensures that a directory is in place
*/
export let ensureDirSync = (dirPathArg: string) => {
plugins.fsExtra.ensureDirSync(dirPathArg)
}
/**
* copies a file from A to B on the local disk
*/
@ -89,9 +73,54 @@ export let copySync = function(fromArg: string,toArg: string): boolean{
return true
}
/**
* removes a file or folder from local disk
*/
/**
* ensures that a directory is in place
*/
export let ensureDir = (dirPathArg: string) => {
let done = plugins.q.defer()
plugins.fsExtra.ensureDir(dirPathArg,done.resolve)
return done.promise
}
/**
* ensures that a directory is in place
*/
export let ensureDirSync = (dirPathArg: string) => {
plugins.fsExtra.ensureDirSync(dirPathArg)
}
/**
* ensures that a file is on disk
* @param filePath the filePath to ensureDir
* @param the fileContent to place into a new file in case it doesn't exist yet
* @returns Promise<void>
* @exec ASYNC
*/
export let ensureFile = (filePathArg, initFileStringArg): plugins.q.Promise<void> => {
let done = plugins.q.defer<void>()
ensureFileSync(filePathArg, initFileStringArg)
done.resolve()
return done.promise
}
/**
* ensures that a file is on disk
* @param filePath the filePath to ensureDir
* @param the fileContent to place into a new file in case it doesn't exist yet
* @returns Promise<void>
* @exec SYNC
*/
export let ensureFileSync = (filePathArg: string, initFileStringArg: string): void => {
if (fileExistsSync(filePathArg)) {
return null
} else {
memory.toFsSync(initFileStringArg, filePathArg)
}
}
/**
* removes a file or folder from local disk
*/
export let remove = function(pathArg: string){
let done = plugins.q.defer()
plugins.fsExtra.remove(pathArg,function(){