2016-06-23 15:42:08 +00:00
|
|
|
import "typings-global";
|
|
|
|
|
|
|
|
import plugins = require("./smartfile.plugins");
|
|
|
|
import SmartfileInterpreter = require("./smartfile.interpreter");
|
|
|
|
|
2016-06-24 01:36:51 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ Checks =============================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export let fileExistsSync = function(filePath):boolean {
|
|
|
|
let fileExistsBool:boolean = false;
|
|
|
|
try {
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.readFileSync(filePath);
|
2016-06-24 01:36:51 +00:00
|
|
|
fileExistsBool = true
|
|
|
|
}
|
|
|
|
catch(err){
|
|
|
|
fileExistsBool = false;
|
|
|
|
}
|
|
|
|
return fileExistsBool;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
export let fileExists = function(filePath){
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-06-24 01:36:51 +00:00
|
|
|
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
|
2016-07-01 04:07:58 +00:00
|
|
|
err ? done.reject(err) : done.resolve();
|
2016-06-24 01:36:51 +00:00
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if given path points to an existing directory
|
|
|
|
*/
|
|
|
|
export let isDirectory = function(pathArg):boolean{
|
2016-07-19 18:28:28 +00:00
|
|
|
return plugins.fsExtra.statSync(pathArg).isDirectory();
|
2016-06-24 01:36:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given path points to an existing file
|
|
|
|
*/
|
|
|
|
export let isFile = function(pathArg):boolean{
|
2016-07-19 18:28:28 +00:00
|
|
|
return plugins.fsExtra.statSync(pathArg).isFile();
|
2016-06-24 01:36:51 +00:00
|
|
|
};
|
|
|
|
|
2016-06-23 15:42:08 +00:00
|
|
|
/*===============================================================
|
|
|
|
============================ FS ACTIONS =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
2016-06-28 04:57:51 +00:00
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
|
|
|
export let ensureDir = (dirPathArg:string) => {
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.ensureDir(dirPathArg,done.resolve);
|
2016-06-28 04:57:51 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ensures that a directory is in place
|
|
|
|
*/
|
|
|
|
export let ensureDirSync = (dirPathArg:string) => {
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.ensureDirSync(dirPathArg);
|
2016-06-28 04:57:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* copies a file from A to B on the local disk
|
|
|
|
*/
|
2016-06-23 15:42:08 +00:00
|
|
|
export let copy = function(fromArg:string, toArg:string){
|
2016-07-01 04:07:58 +00:00
|
|
|
var done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.copy(fromArg,toArg,{},function(){
|
2016-06-23 15:42:08 +00:00
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* copies a file SYNCHRONOUSLY from A to B on the local disk
|
|
|
|
*/
|
2016-06-23 15:42:08 +00:00
|
|
|
export let copySync = function(fromArg:string,toArg:string):boolean{
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.copySync(fromArg,toArg);
|
2016-06-23 15:42:08 +00:00
|
|
|
return true;
|
|
|
|
};
|
2016-06-23 16:39:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* removes a file or folder from local disk
|
|
|
|
*/
|
2016-06-23 15:42:08 +00:00
|
|
|
export let remove = function(pathArg:string){
|
2016-07-01 04:07:58 +00:00
|
|
|
var done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.remove(pathArg,function(){
|
2016-06-23 15:42:08 +00:00
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
2016-06-23 16:39:02 +00:00
|
|
|
/**
|
|
|
|
* removes a file SYNCHRONOUSLY from local disk
|
|
|
|
*/
|
2016-06-23 15:42:08 +00:00
|
|
|
export let removeSync = function(pathArg:string):boolean{
|
2016-07-19 18:28:28 +00:00
|
|
|
plugins.fsExtra.removeSync(pathArg);
|
2016-06-23 15:42:08 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*===============================================================
|
|
|
|
============================ Write/Read =========================
|
|
|
|
===============================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export let toGulpStreamSync = function(filePathArg:string){
|
|
|
|
let stream = plugins.gulp.src(filePathArg);
|
|
|
|
return stream;
|
|
|
|
};
|
|
|
|
|
|
|
|
export let toGulpDestSync = function(folderPathArg:string){
|
|
|
|
return plugins.gulp.dest(folderPathArg);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @param fileTypeArg
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
export let toObjectSync = function(filePathArg,fileTypeArg?) {
|
2016-07-19 18:28:28 +00:00
|
|
|
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8');
|
2016-06-23 15:42:08 +00:00
|
|
|
let fileType;
|
2016-06-23 16:31:55 +00:00
|
|
|
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileInterpreter.filetype(filePathArg);
|
|
|
|
return SmartfileInterpreter.objectFile(fileString,fileType);
|
2016-06-23 15:42:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reads a file content to a String
|
|
|
|
* @param filePath
|
|
|
|
* @returns {string|Buffer|any}
|
|
|
|
*/
|
|
|
|
export let toStringSync = function(filePath) {
|
|
|
|
let fileString;
|
2016-07-19 18:28:28 +00:00
|
|
|
fileString = plugins.fsExtra.readFileSync(filePath, "utf8");
|
2016-06-23 15:42:08 +00:00
|
|
|
return fileString;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param filePathArg
|
|
|
|
* @param options
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
export let toVinylSync = function(filePathArg,options = {}) {
|
|
|
|
return plugins.vinylFile.readSync(filePathArg,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lets you reload files hot.
|
|
|
|
* @param path
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
export let requireReload = function(path:string){
|
|
|
|
return plugins.requireReload(path);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2016-06-28 06:40:22 +00:00
|
|
|
export let listFolders = function(pathArg:string,regexFilter?:RegExp){
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory();
|
2016-06-23 15:42:08 +00:00
|
|
|
});
|
2016-06-28 06:40:22 +00:00
|
|
|
if(regexFilter){
|
|
|
|
folderArray = folderArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
}
|
2016-06-23 15:42:08 +00:00
|
|
|
done.resolve(folderArray);
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2016-06-28 06:40:22 +00:00
|
|
|
export let listFoldersSync = function(pathArg:string,regexFilter?:RegExp):string[]{
|
2016-07-19 18:28:28 +00:00
|
|
|
let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory();
|
2016-06-23 16:39:02 +00:00
|
|
|
});
|
2016-06-28 06:40:22 +00:00
|
|
|
if(regexFilter){
|
|
|
|
folderArray = folderArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
return folderArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lists Files in a directory on local disk
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
|
|
|
export let listFiles = function(pathArg:string, regexFilter?:RegExp){
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
if(regexFilter){
|
|
|
|
fileArray = fileArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
done.resolve(fileArray);
|
2016-06-28 07:59:59 +00:00
|
|
|
return done.promise;
|
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
|
|
|
|
*/
|
|
|
|
export let listFilesSync = function(pathArg:string, regexFilter?:RegExp):string[]{
|
2016-07-19 18:28:28 +00:00
|
|
|
let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
if(regexFilter){
|
|
|
|
fileArray = fileArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
return fileArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lists all items (folders AND files) in a directory on local disk
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
|
|
|
export let listAllItems = function(pathArg:string, regexFilter?:RegExp){
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-07-19 18:28:28 +00:00
|
|
|
let allItmesArray = plugins.fsExtra.readdirSync(pathArg);
|
2016-06-28 06:40:22 +00:00
|
|
|
if(regexFilter){
|
|
|
|
allItmesArray = allItmesArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
done.resolve(allItmesArray);
|
2016-06-28 07:59:59 +00:00
|
|
|
return done.promise;
|
2016-06-23 16:39:02 +00:00
|
|
|
};
|
2016-06-28 06:40:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lists all items (folders AND files) SYNCHRONOUSLY in a directory on local disk
|
|
|
|
* @returns an array with the folder names as strings
|
|
|
|
*/
|
|
|
|
export let listAllItemsSync = function(pathArg:string, regexFilter?:RegExp):string[]{
|
2016-07-19 18:28:28 +00:00
|
|
|
let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) {
|
|
|
|
return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile();
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
if(regexFilter){
|
|
|
|
allItmesArray = allItmesArray.filter((fileItem) => {
|
2016-06-28 07:59:59 +00:00
|
|
|
return regexFilter.test(fileItem);
|
2016-06-28 06:40:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
return allItmesArray;
|
|
|
|
};
|
|
|
|
|
2016-06-30 23:37:48 +00:00
|
|
|
export let listFileTree = (dirPath:string, miniMatchFilter:string) => {
|
2016-07-01 04:07:58 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-06-30 23:37:48 +00:00
|
|
|
let options = {
|
|
|
|
cwd:dirPath
|
|
|
|
}
|
|
|
|
plugins.glob(miniMatchFilter,options,(err,files:string[]) => {
|
|
|
|
if(err){
|
|
|
|
console.log(err);
|
2016-07-01 04:07:58 +00:00
|
|
|
done.reject(err);
|
2016-06-30 23:37:48 +00:00
|
|
|
};
|
|
|
|
done.resolve(files);
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|