2016-09-20 15:56:49 +00:00
|
|
|
import 'typings-global'
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-09-20 15:56:49 +00:00
|
|
|
import plugins = require('./smartfile.plugins')
|
|
|
|
import SmartfileInterpreter = require('./smartfile.interpreter')
|
2017-03-04 20:10:46 +00:00
|
|
|
|
|
|
|
import { Smartfile } from './smartfile.classes.smartfile'
|
|
|
|
|
2016-09-24 19:42:45 +00:00
|
|
|
import * as memory from './smartfile.memory'
|
2016-06-24 01:36:51 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ Checks =============================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let fileExistsSync = function (filePath): boolean {
|
|
|
|
let fileExistsBool: boolean = false
|
|
|
|
try {
|
|
|
|
plugins.fsExtra.readFileSync(filePath)
|
|
|
|
fileExistsBool = true
|
|
|
|
} catch (err) {
|
|
|
|
fileExistsBool = false
|
|
|
|
}
|
|
|
|
return fileExistsBool
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if given path points to an existing directory
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let isDirectory = function (pathArg): boolean {
|
|
|
|
return plugins.fsExtra.statSync(pathArg).isDirectory()
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-24 01:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given path points to an existing file
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let isFile = function (pathArg): boolean {
|
|
|
|
return plugins.fsExtra.statSync(pathArg).isFile()
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-24 01:36:51 +00:00
|
|
|
|
2016-06-23 15:42:08 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ FS ACTIONS =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
2016-09-24 19:42:45 +00:00
|
|
|
/**
|
|
|
|
* copies a file from A to B on the local disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let copy = function (fromArg: string, toArg: string) {
|
|
|
|
let done = plugins.q.defer()
|
|
|
|
plugins.fsExtra.copy(fromArg, toArg, {}, function () {
|
|
|
|
done.resolve()
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-09-24 19:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* copies a file SYNCHRONOUSLY from A to B on the local disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let copySync = function (fromArg: string, toArg: string): boolean {
|
|
|
|
plugins.fsExtra.copySync(fromArg, toArg)
|
|
|
|
return true
|
2016-09-24 19:42:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 04:57:51 +00:00
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
2016-09-20 15:56:49 +00:00
|
|
|
export let ensureDir = (dirPathArg: string) => {
|
2017-03-04 20:10:46 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
plugins.fsExtra.ensureDir(dirPathArg, done.resolve)
|
|
|
|
return done.promise
|
2016-06-28 04:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
2016-09-20 15:56:49 +00:00
|
|
|
export let ensureDirSync = (dirPathArg: string) => {
|
2017-03-04 20:10:46 +00:00
|
|
|
plugins.fsExtra.ensureDirSync(dirPathArg)
|
2016-06-28 04:57:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 01:45:53 +00:00
|
|
|
/**
|
|
|
|
* ensure an empty directory
|
|
|
|
* @executes ASYNC
|
|
|
|
*/
|
|
|
|
export let ensureEmptyDir = (dirPathArg: string) => {
|
2017-03-04 20:10:46 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
plugins.fsExtra.ensureDir(dirPathArg, () => {
|
|
|
|
plugins.fsExtra.emptyDir(dirPathArg, done.resolve)
|
|
|
|
})
|
|
|
|
return done.promise
|
2017-01-01 01:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ensure an empty directory
|
|
|
|
* @executes SYNC
|
|
|
|
*/
|
|
|
|
export let ensureEmptyDirSync = (dirPathArg: string) => {
|
2017-03-04 20:10:46 +00:00
|
|
|
plugins.fsExtra.ensureDirSync(dirPathArg)
|
|
|
|
plugins.fsExtra.emptyDirSync(dirPathArg)
|
2017-01-01 01:45:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
2016-09-24 19:42:45 +00:00
|
|
|
* 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
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2017-01-20 23:47:48 +00:00
|
|
|
export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => {
|
2017-03-04 20:10:46 +00:00
|
|
|
let done = plugins.q.defer<void>()
|
|
|
|
ensureFileSync(filePathArg, initFileStringArg)
|
|
|
|
done.resolve()
|
|
|
|
return done.promise
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
2016-09-24 19:42:45 +00:00
|
|
|
* 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
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2016-09-24 19:42:45 +00:00
|
|
|
export let ensureFileSync = (filePathArg: string, initFileStringArg: string): void => {
|
2017-03-04 20:10:46 +00:00
|
|
|
if (fileExistsSync(filePathArg)) {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
memory.toFsSync(initFileStringArg, filePathArg)
|
|
|
|
}
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 19:42:45 +00:00
|
|
|
/**
|
|
|
|
* removes a file or folder from local disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let remove = function (pathArg: string): Promise<void> {
|
|
|
|
let done = plugins.q.defer<void>()
|
|
|
|
plugins.fsExtra.remove(pathArg, function () {
|
|
|
|
done.resolve()
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* removes a file SYNCHRONOUSLY from local disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let removeSync = function (pathArg: string): boolean {
|
|
|
|
plugins.fsExtra.removeSync(pathArg)
|
|
|
|
return true
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-09-29 12:17:46 +00:00
|
|
|
/**
|
|
|
|
* removes an array of filePaths from disk
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let removeMany = function (filePathArrayArg: string[]) {
|
|
|
|
let promiseArray: Promise<void>[] = []
|
|
|
|
for (let filePath of filePathArrayArg) {
|
|
|
|
promiseArray.push(remove(filePath))
|
|
|
|
}
|
|
|
|
return Promise.all(promiseArray)
|
2016-09-29 12:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* like removeFilePathArray but SYNCHRONOUSLY
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let removeManySync = function (filePathArrayArg: string[]): void {
|
|
|
|
for (let filePath of filePathArrayArg) {
|
|
|
|
removeSync(filePath)
|
|
|
|
}
|
2016-09-29 12:17:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 15:42:08 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ Write/Read =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @param fileTypeArg
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reads a file content to a String
|
|
|
|
* @param filePath
|
|
|
|
* @returns {string|Buffer|any}
|
|
|
|
*/
|
2017-03-11 23:06:56 +00:00
|
|
|
export let toStringSync = function (filePath: string) {
|
|
|
|
let fileString = plugins.fsExtra.readFileSync(filePath, 'utf8')
|
2017-03-04 20:10:46 +00:00
|
|
|
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,
|
2017-03-15 14:25:41 +00:00
|
|
|
contentBuffer: new Buffer(toStringSync(
|
|
|
|
plugins.path.join(dirPathArg, filePath)
|
|
|
|
))
|
2017-03-04 20:10:46 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
return smartfileArray
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @param options
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let toVinylSync = function (filePathArg, options = {}) {
|
|
|
|
return plugins.vinylFile.readSync(filePathArg, options)
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lets you reload files hot.
|
|
|
|
* @param path
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
export let requireReload = function (path: string) {
|
|
|
|
return plugins.requireReload(path)
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* lists Folders in a directory on local disk
|
2016-06-28 04:57:51 +00:00
|
|
|
* @returns Promise
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
})
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
done.resolve(folderArray)
|
|
|
|
return done.promise
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* lists Folders SYNCHRONOUSLY in a directory on local disk
|
2016-06-28 04:57:51 +00:00
|
|
|
* @returns an array with the folder names as strings
|
2016-06-23 16:39:02 +00:00
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
})
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
return folderArray
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists Files in a directory on local disk
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
})
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
done.resolve(fileArray)
|
|
|
|
return done.promise
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists Files SYNCHRONOUSLY in a directory on local disk
|
|
|
|
* @returns an array with the folder names as strings
|
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
})
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
return fileArray
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists all items (folders AND files) in a directory on local disk
|
2016-09-17 21:11:44 +00:00
|
|
|
* @returns Promise<string[]>
|
2016-06-28 06:40:22 +00:00
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-17 21:11:44 +00:00
|
|
|
* lists all items (folders AND files) in a directory on local disk
|
2016-06-28 06:40:22 +00:00
|
|
|
* @returns an array with the folder names as strings
|
2016-09-17 21:11:44 +00:00
|
|
|
* @executes SYNC
|
2016-06-28 06:40:22 +00:00
|
|
|
*/
|
2017-03-04 20:10:46 +00:00
|
|
|
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)
|
2016-09-20 15:56:49 +00:00
|
|
|
})
|
2017-03-04 20:10:46 +00:00
|
|
|
}
|
|
|
|
return allItmesArray
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|
2016-06-28 06:40:22 +00:00
|
|
|
|
2016-09-17 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* lists a file tree using a miniMatch filter
|
2016-09-30 14:16:11 +00:00
|
|
|
* note: if the miniMatch Filter is an absolute path, the cwdArg will be omitted
|
2016-09-17 21:11:44 +00:00
|
|
|
* @returns Promise<string[]> string array with the absolute paths of all matching files
|
|
|
|
*/
|
2017-01-20 23:47:48 +00:00
|
|
|
export let listFileTree = (dirPathArg: string, miniMatchFilter: string): Promise<string[]> => {
|
2017-03-04 20:10:46 +00:00
|
|
|
let done = plugins.q.defer<string[]>()
|
|
|
|
|
|
|
|
// handle absolute miniMatchFilter
|
|
|
|
let dirPath: string
|
|
|
|
if (plugins.path.isAbsolute(miniMatchFilter)) {
|
|
|
|
dirPath = '/'
|
|
|
|
} else {
|
|
|
|
dirPath = dirPathArg
|
|
|
|
}
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
cwd: dirPath
|
|
|
|
}
|
|
|
|
plugins.glob(miniMatchFilter, options, (err, files: string[]) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
done.reject(err)
|
2016-09-30 14:16:11 +00:00
|
|
|
}
|
2017-03-04 20:10:46 +00:00
|
|
|
done.resolve(files)
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-09-20 15:56:49 +00:00
|
|
|
}
|