start implementation of Smartfile class
This commit is contained in:
@ -1,5 +1,28 @@
|
||||
export class Smartfile {
|
||||
constructor() {
|
||||
import * as plugins from './smartfile.plugins'
|
||||
|
||||
}
|
||||
export interface ISmartfileConstructorOptions {
|
||||
path?: string
|
||||
contentsString?: string
|
||||
contentBuffer?: Buffer
|
||||
}
|
||||
|
||||
export class Smartfile {
|
||||
path: string
|
||||
contents: Buffer
|
||||
constructor(optionsArg: ISmartfileConstructorOptions) {
|
||||
if (optionsArg.contentBuffer) {
|
||||
this.contents = optionsArg.contentBuffer
|
||||
} else if(optionsArg.contentsString) {
|
||||
this.contents = new Buffer(optionsArg.contentsString)
|
||||
}
|
||||
this.path = optionsArg.path
|
||||
}
|
||||
|
||||
/**
|
||||
* set contents from string
|
||||
* @param contentString
|
||||
*/
|
||||
setContentFromString(contentString: string) {
|
||||
this.contents = new Buffer(contentString)
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ import 'typings-global'
|
||||
|
||||
import plugins = require('./smartfile.plugins')
|
||||
import SmartfileInterpreter = require('./smartfile.interpreter')
|
||||
|
||||
import { Smartfile } from './smartfile.classes.smartfile'
|
||||
|
||||
import * as memory from './smartfile.memory'
|
||||
/*===============================================================
|
||||
============================ Checks =============================
|
||||
@ -12,15 +15,15 @@ import * as memory from './smartfile.memory'
|
||||
* @param filePath
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export let fileExistsSync = function(filePath): boolean {
|
||||
let fileExistsBool: boolean = false
|
||||
try {
|
||||
plugins.fsExtra.readFileSync(filePath)
|
||||
fileExistsBool = true
|
||||
} catch (err) {
|
||||
fileExistsBool = false
|
||||
}
|
||||
return fileExistsBool
|
||||
export let fileExistsSync = function (filePath): boolean {
|
||||
let fileExistsBool: boolean = false
|
||||
try {
|
||||
plugins.fsExtra.readFileSync(filePath)
|
||||
fileExistsBool = true
|
||||
} catch (err) {
|
||||
fileExistsBool = false
|
||||
}
|
||||
return fileExistsBool
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,26 +31,26 @@ export let fileExistsSync = function(filePath): boolean {
|
||||
* @param filePath
|
||||
* @returns {any}
|
||||
*/
|
||||
export let fileExists = function(filePath){
|
||||
let done = plugins.q.defer()
|
||||
plugins.fs.access(filePath, 4, function (err) {
|
||||
err ? done.reject(err) : done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
export let fileExists = function (filePath) {
|
||||
let done = plugins.q.defer()
|
||||
plugins.fs.access(filePath, 4, function (err) {
|
||||
err ? done.reject(err) : done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if given path points to an existing directory
|
||||
*/
|
||||
export let isDirectory = function(pathArg): boolean{
|
||||
return plugins.fsExtra.statSync(pathArg).isDirectory()
|
||||
export let isDirectory = function (pathArg): boolean {
|
||||
return plugins.fsExtra.statSync(pathArg).isDirectory()
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given path points to an existing file
|
||||
*/
|
||||
export let isFile = function(pathArg): boolean{
|
||||
return plugins.fsExtra.statSync(pathArg).isFile()
|
||||
export let isFile = function (pathArg): boolean {
|
||||
return plugins.fsExtra.statSync(pathArg).isFile()
|
||||
}
|
||||
|
||||
/*===============================================================
|
||||
@ -57,36 +60,36 @@ export let isFile = function(pathArg): boolean{
|
||||
/**
|
||||
* copies a file from A to B on the local disk
|
||||
*/
|
||||
export let copy = function(fromArg: string, toArg: string){
|
||||
let done = plugins.q.defer()
|
||||
plugins.fsExtra.copy(fromArg,toArg,{},function(){
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
export let copy = function (fromArg: string, toArg: string) {
|
||||
let done = plugins.q.defer()
|
||||
plugins.fsExtra.copy(fromArg, toArg, {}, function () {
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* copies a file SYNCHRONOUSLY from A to B on the local disk
|
||||
*/
|
||||
export let copySync = function(fromArg: string,toArg: string): boolean{
|
||||
plugins.fsExtra.copySync(fromArg,toArg)
|
||||
return true
|
||||
export let copySync = function (fromArg: string, toArg: string): boolean {
|
||||
plugins.fsExtra.copySync(fromArg, toArg)
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
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)
|
||||
plugins.fsExtra.ensureDirSync(dirPathArg)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,11 +97,11 @@ export let ensureDirSync = (dirPathArg: string) => {
|
||||
* @executes ASYNC
|
||||
*/
|
||||
export let ensureEmptyDir = (dirPathArg: string) => {
|
||||
let done = plugins.q.defer()
|
||||
plugins.fsExtra.ensureDir(dirPathArg,() => {
|
||||
plugins.fsExtra.emptyDir(dirPathArg, done.resolve)
|
||||
})
|
||||
return done.promise
|
||||
let done = plugins.q.defer()
|
||||
plugins.fsExtra.ensureDir(dirPathArg, () => {
|
||||
plugins.fsExtra.emptyDir(dirPathArg, done.resolve)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,8 +109,8 @@ export let ensureEmptyDir = (dirPathArg: string) => {
|
||||
* @executes SYNC
|
||||
*/
|
||||
export let ensureEmptyDirSync = (dirPathArg: string) => {
|
||||
plugins.fsExtra.ensureDirSync(dirPathArg)
|
||||
plugins.fsExtra.emptyDirSync(dirPathArg)
|
||||
plugins.fsExtra.ensureDirSync(dirPathArg)
|
||||
plugins.fsExtra.emptyDirSync(dirPathArg)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,10 +121,10 @@ export let ensureEmptyDirSync = (dirPathArg: string) => {
|
||||
* @exec ASYNC
|
||||
*/
|
||||
export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => {
|
||||
let done = plugins.q.defer<void>()
|
||||
ensureFileSync(filePathArg, initFileStringArg)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
let done = plugins.q.defer<void>()
|
||||
ensureFileSync(filePathArg, initFileStringArg)
|
||||
done.resolve()
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
@ -132,50 +135,50 @@ export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => {
|
||||
* @exec SYNC
|
||||
*/
|
||||
export let ensureFileSync = (filePathArg: string, initFileStringArg: string): void => {
|
||||
if (fileExistsSync(filePathArg)) {
|
||||
return null
|
||||
} else {
|
||||
memory.toFsSync(initFileStringArg, filePathArg)
|
||||
}
|
||||
if (fileExistsSync(filePathArg)) {
|
||||
return null
|
||||
} else {
|
||||
memory.toFsSync(initFileStringArg, filePathArg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a file or folder from local disk
|
||||
*/
|
||||
export let remove = function(pathArg: string): Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
plugins.fsExtra.remove(pathArg,function(){
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
export let remove = function (pathArg: string): Promise<void> {
|
||||
let done = plugins.q.defer<void>()
|
||||
plugins.fsExtra.remove(pathArg, function () {
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a file SYNCHRONOUSLY from local disk
|
||||
*/
|
||||
export let removeSync = function(pathArg: string): boolean{
|
||||
plugins.fsExtra.removeSync(pathArg)
|
||||
return true
|
||||
export let removeSync = function (pathArg: string): boolean {
|
||||
plugins.fsExtra.removeSync(pathArg)
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* removes an array of filePaths from disk
|
||||
*/
|
||||
export let removeMany = function(filePathArrayArg: string[]){
|
||||
let promiseArray: Promise<void>[] = []
|
||||
for (let filePath of filePathArrayArg) {
|
||||
promiseArray.push(remove(filePath))
|
||||
}
|
||||
return Promise.all(promiseArray)
|
||||
export let removeMany = function (filePathArrayArg: string[]) {
|
||||
let promiseArray: Promise<void>[] = []
|
||||
for (let filePath of filePathArrayArg) {
|
||||
promiseArray.push(remove(filePath))
|
||||
}
|
||||
return Promise.all(promiseArray)
|
||||
}
|
||||
|
||||
/**
|
||||
* like removeFilePathArray but SYNCHRONOUSLY
|
||||
*/
|
||||
export let removeManySync = function(filePathArrayArg: string[]): void {
|
||||
for (let filePath of filePathArrayArg) {
|
||||
removeSync(filePath)
|
||||
}
|
||||
export let removeManySync = function (filePathArrayArg: string[]): void {
|
||||
for (let filePath of filePathArrayArg) {
|
||||
removeSync(filePath)
|
||||
}
|
||||
}
|
||||
|
||||
/*===============================================================
|
||||
@ -188,11 +191,11 @@ export let removeManySync = function(filePathArrayArg: string[]): void {
|
||||
* @param fileTypeArg
|
||||
* @returns {any}
|
||||
*/
|
||||
export let toObjectSync = function(filePathArg,fileTypeArg?) {
|
||||
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8')
|
||||
let fileType
|
||||
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileInterpreter.filetype(filePathArg)
|
||||
return SmartfileInterpreter.objectFile(fileString,fileType)
|
||||
export let toObjectSync = function (filePathArg, fileTypeArg?) {
|
||||
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8')
|
||||
let fileType
|
||||
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileInterpreter.filetype(filePathArg)
|
||||
return SmartfileInterpreter.objectFile(fileString, fileType)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -200,10 +203,22 @@ export let toObjectSync = function(filePathArg,fileTypeArg?) {
|
||||
* @param filePath
|
||||
* @returns {string|Buffer|any}
|
||||
*/
|
||||
export let toStringSync = function(filePath) {
|
||||
let fileString
|
||||
fileString = plugins.fsExtra.readFileSync(filePath, 'utf8')
|
||||
return fileString
|
||||
export let toStringSync = function (filePath) {
|
||||
let fileString
|
||||
fileString = plugins.fsExtra.readFileSync(filePath, 'utf8')
|
||||
return fileString
|
||||
}
|
||||
|
||||
export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string) => {
|
||||
let fileTree = await listFileTree(dirPathArg, miniMatchFilter)
|
||||
let smartfileArray: Smartfile[] = []
|
||||
for (let filePath of fileTree) {
|
||||
smartfileArray.push(new Smartfile({
|
||||
path: filePath,
|
||||
contentBuffer: new Buffer(toStringSync(filePath))
|
||||
}))
|
||||
}
|
||||
return smartfileArray
|
||||
}
|
||||
|
||||
/**
|
||||
@ -212,8 +227,8 @@ export let toStringSync = function(filePath) {
|
||||
* @param options
|
||||
* @returns {number}
|
||||
*/
|
||||
export let toVinylSync = function(filePathArg,options = {}) {
|
||||
return plugins.vinylFile.readSync(filePathArg,options)
|
||||
export let toVinylSync = function (filePathArg, options = {}) {
|
||||
return plugins.vinylFile.readSync(filePathArg, options)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -221,92 +236,92 @@ export let toVinylSync = function(filePathArg,options = {}) {
|
||||
* @param path
|
||||
* @returns {any}
|
||||
*/
|
||||
export let requireReload = function(path: string){
|
||||
return plugins.requireReload(path)
|
||||
export let requireReload = function (path: string) {
|
||||
return plugins.requireReload(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* lists Folders in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export let listFolders = function(pathArg: string,regexFilter?: RegExp){
|
||||
let done = plugins.q.defer()
|
||||
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
|
||||
export let listFolders = function (pathArg: string, regexFilter?: RegExp) {
|
||||
let done = plugins.q.defer()
|
||||
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
|
||||
})
|
||||
if (regexFilter) {
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
if (regexFilter) {
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
}
|
||||
done.resolve(folderArray)
|
||||
return done.promise
|
||||
}
|
||||
done.resolve(folderArray)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* lists Folders SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export let listFoldersSync = function(pathArg: string,regexFilter?: RegExp): string[]{
|
||||
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
|
||||
export let listFoldersSync = function (pathArg: string, regexFilter?: RegExp): string[] {
|
||||
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory()
|
||||
})
|
||||
if (regexFilter) {
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
if (regexFilter) {
|
||||
folderArray = folderArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
}
|
||||
return folderArray
|
||||
}
|
||||
return folderArray
|
||||
}
|
||||
|
||||
/**
|
||||
* lists Files in a directory on local disk
|
||||
* @returns Promise
|
||||
*/
|
||||
export let listFiles = function(pathArg: string, regexFilter?: RegExp){
|
||||
let done = plugins.q.defer()
|
||||
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
export let listFiles = function (pathArg: string, regexFilter?: RegExp) {
|
||||
let done = plugins.q.defer()
|
||||
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
})
|
||||
if (regexFilter) {
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
if (regexFilter) {
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
}
|
||||
done.resolve(fileArray)
|
||||
return done.promise
|
||||
}
|
||||
done.resolve(fileArray)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* lists Files SYNCHRONOUSLY in a directory on local disk
|
||||
* @returns an array with the folder names as strings
|
||||
*/
|
||||
export let listFilesSync = function(pathArg: string, regexFilter?: RegExp): string[]{
|
||||
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
export let listFilesSync = function (pathArg: string, regexFilter?: RegExp): string[] {
|
||||
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
})
|
||||
if (regexFilter) {
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
if (regexFilter) {
|
||||
fileArray = fileArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
}
|
||||
return fileArray
|
||||
}
|
||||
return fileArray
|
||||
}
|
||||
|
||||
/**
|
||||
* lists all items (folders AND files) in a directory on local disk
|
||||
* @returns Promise<string[]>
|
||||
*/
|
||||
export let listAllItems = function(pathArg: string, regexFilter?: RegExp): Promise<string[]> {
|
||||
let done = plugins.q.defer<string[]>()
|
||||
let allItmesArray = plugins.fsExtra.readdirSync(pathArg)
|
||||
if (regexFilter) {
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
};
|
||||
done.resolve(allItmesArray)
|
||||
return done.promise
|
||||
export let listAllItems = function (pathArg: string, regexFilter?: RegExp): Promise<string[]> {
|
||||
let done = plugins.q.defer<string[]>()
|
||||
let allItmesArray = plugins.fsExtra.readdirSync(pathArg)
|
||||
if (regexFilter) {
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
};
|
||||
done.resolve(allItmesArray)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
@ -314,16 +329,16 @@ export let listAllItems = function(pathArg: string, regexFilter?: RegExp): Promi
|
||||
* @returns an array with the folder names as strings
|
||||
* @executes SYNC
|
||||
*/
|
||||
export let listAllItemsSync = function(pathArg: string, regexFilter?: RegExp): string[]{
|
||||
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
export let listAllItemsSync = function (pathArg: string, regexFilter?: RegExp): string[] {
|
||||
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function (file) {
|
||||
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile()
|
||||
})
|
||||
if (regexFilter) {
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
if (regexFilter) {
|
||||
allItmesArray = allItmesArray.filter((fileItem) => {
|
||||
return regexFilter.test(fileItem)
|
||||
})
|
||||
}
|
||||
return allItmesArray
|
||||
}
|
||||
return allItmesArray
|
||||
}
|
||||
|
||||
/**
|
||||
@ -332,25 +347,25 @@ export let listAllItemsSync = function(pathArg: string, regexFilter?: RegExp): s
|
||||
* @returns Promise<string[]> string array with the absolute paths of all matching files
|
||||
*/
|
||||
export let listFileTree = (dirPathArg: string, miniMatchFilter: string): Promise<string[]> => {
|
||||
let done = plugins.q.defer<string[]>()
|
||||
let done = plugins.q.defer<string[]>()
|
||||
|
||||
// handle absolute miniMatchFilter
|
||||
let dirPath: string
|
||||
if (plugins.path.isAbsolute(miniMatchFilter)) {
|
||||
dirPath = '/'
|
||||
} else {
|
||||
dirPath = dirPathArg
|
||||
}
|
||||
// handle absolute miniMatchFilter
|
||||
let dirPath: string
|
||||
if (plugins.path.isAbsolute(miniMatchFilter)) {
|
||||
dirPath = '/'
|
||||
} else {
|
||||
dirPath = dirPathArg
|
||||
}
|
||||
|
||||
let options = {
|
||||
cwd: dirPath
|
||||
let options = {
|
||||
cwd: dirPath
|
||||
}
|
||||
plugins.glob(miniMatchFilter, options, (err, files: string[]) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
done.reject(err)
|
||||
}
|
||||
plugins.glob(miniMatchFilter,options,(err,files: string[]) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
done.reject(err)
|
||||
}
|
||||
done.resolve(files)
|
||||
})
|
||||
return done.promise
|
||||
done.resolve(files)
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
Reference in New Issue
Block a user