smartfile/dist/smartfile.fs.js

267 lines
16 KiB
JavaScript
Raw Normal View History

2016-06-23 16:31:55 +00:00
"use strict";
require("typings-global");
2016-07-17 15:34:15 +00:00
const plugins = require("./smartfile.plugins");
const SmartfileInterpreter = require("./smartfile.interpreter");
2016-06-23 16:31:55 +00:00
/*===============================================================
2016-06-24 01:36:51 +00:00
============================ Checks =============================
===============================================================*/
/**
*
* @param filePath
* @returns {boolean}
*/
exports.fileExistsSync = function (filePath) {
2016-07-17 15:34:15 +00:00
let fileExistsBool = false;
2016-06-24 01:36:51 +00:00
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}
*/
exports.fileExists = function (filePath) {
2016-09-17 21:11:44 +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
*/
exports.isDirectory = function (pathArg) {
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
*/
exports.isFile = function (pathArg) {
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 16:31:55 +00:00
============================ FS ACTIONS =========================
===============================================================*/
2016-06-28 04:57:51 +00:00
/**
* ensures that a directory is in place
*/
2016-07-17 15:34:15 +00:00
exports.ensureDir = (dirPathArg) => {
2016-09-17 21:11:44 +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
*/
2016-07-17 15:34:15 +00:00
exports.ensureDirSync = (dirPathArg) => {
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 16:31:55 +00:00
exports.copy = function (fromArg, toArg) {
2016-09-20 15:56:49 +00:00
let done = plugins.q.defer();
2016-07-19 18:28:28 +00:00
plugins.fsExtra.copy(fromArg, toArg, {}, function () {
2016-06-23 16:31:55 +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 16:31:55 +00:00
exports.copySync = function (fromArg, toArg) {
2016-07-19 18:28:28 +00:00
plugins.fsExtra.copySync(fromArg, toArg);
2016-06-23 16:31:55 +00:00
return true;
};
2016-06-23 16:39:02 +00:00
/**
* removes a file or folder from local disk
*/
2016-06-23 16:31:55 +00:00
exports.remove = function (pathArg) {
2016-09-20 15:56:49 +00:00
let done = plugins.q.defer();
2016-07-19 18:28:28 +00:00
plugins.fsExtra.remove(pathArg, function () {
2016-06-23 16:31:55 +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 16:31:55 +00:00
exports.removeSync = function (pathArg) {
2016-07-19 18:28:28 +00:00
plugins.fsExtra.removeSync(pathArg);
2016-06-23 16:31:55 +00:00
return true;
};
/*===============================================================
============================ Write/Read =========================
===============================================================*/
/**
*
* @param filePathArg
* @returns {*}
*/
exports.toGulpStreamSync = function (filePathArg) {
2016-07-17 15:34:15 +00:00
let stream = plugins.gulp.src(filePathArg);
2016-06-23 16:31:55 +00:00
return stream;
};
exports.toGulpDestSync = function (folderPathArg) {
return plugins.gulp.dest(folderPathArg);
};
/**
*
* @param filePathArg
* @param fileTypeArg
* @returns {any}
*/
exports.toObjectSync = function (filePathArg, fileTypeArg) {
2016-07-19 18:28:28 +00:00
let fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8');
2016-07-17 15:34:15 +00:00
let fileType;
2016-06-23 16:31:55 +00:00
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileInterpreter.filetype(filePathArg);
return SmartfileInterpreter.objectFile(fileString, fileType);
};
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
exports.toStringSync = function (filePath) {
2016-07-17 15:34:15 +00:00
let fileString;
2016-09-20 15:56:49 +00:00
fileString = plugins.fsExtra.readFileSync(filePath, 'utf8');
2016-06-23 16:31:55 +00:00
return fileString;
};
/**
*
* @param filePathArg
* @param options
* @returns {number}
*/
2016-07-17 15:34:15 +00:00
exports.toVinylSync = function (filePathArg, options = {}) {
2016-06-23 16:31:55 +00:00
return plugins.vinylFile.readSync(filePathArg, options);
};
/**
* lets you reload files hot.
* @param path
* @returns {any}
*/
exports.requireReload = function (path) {
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
*/
exports.listFolders = function (pathArg, regexFilter) {
2016-09-17 21:11:44 +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 16:31:55 +00:00
});
if (regexFilter) {
2016-07-17 15:34:15 +00:00
folderArray = folderArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
2016-06-23 16:31:55 +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
*/
exports.listFoldersSync = function (pathArg, regexFilter) {
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
});
if (regexFilter) {
2016-07-17 15:34:15 +00:00
folderArray = folderArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
return folderArray;
};
/**
* lists Files in a directory on local disk
* @returns Promise
*/
exports.listFiles = function (pathArg, regexFilter) {
2016-09-17 21:11:44 +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();
});
if (regexFilter) {
2016-07-17 15:34:15 +00:00
fileArray = fileArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
done.resolve(fileArray);
2016-06-28 07:59:59 +00:00
return done.promise;
};
/**
* lists Files SYNCHRONOUSLY in a directory on local disk
* @returns an array with the folder names as strings
*/
exports.listFilesSync = function (pathArg, regexFilter) {
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();
});
if (regexFilter) {
2016-07-17 15:34:15 +00:00
fileArray = fileArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
return fileArray;
};
/**
* lists all items (folders AND files) in a directory on local disk
2016-09-17 21:11:44 +00:00
* @returns Promise<string[]>
*/
exports.listAllItems = function (pathArg, regexFilter) {
2016-09-17 21:11:44 +00:00
let done = plugins.q.defer();
2016-07-19 18:28:28 +00:00
let allItmesArray = plugins.fsExtra.readdirSync(pathArg);
if (regexFilter) {
2016-07-17 15:34:15 +00:00
allItmesArray = allItmesArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
;
done.resolve(allItmesArray);
2016-06-28 07:59:59 +00:00
return done.promise;
};
/**
2016-09-17 21:11:44 +00:00
* lists all items (folders AND files) in a directory on local disk
* @returns an array with the folder names as strings
2016-09-17 21:11:44 +00:00
* @executes SYNC
*/
exports.listAllItemsSync = function (pathArg, regexFilter) {
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();
});
if (regexFilter) {
2016-07-17 15:34:15 +00:00
allItmesArray = allItmesArray.filter((fileItem) => {
2016-06-28 07:59:59 +00:00
return regexFilter.test(fileItem);
});
}
return allItmesArray;
2016-06-23 16:39:02 +00:00
};
2016-09-17 21:11:44 +00:00
/**
* lists a file tree using a miniMatch filter
* @returns Promise<string[]> string array with the absolute paths of all matching files
*/
2016-07-17 15:34:15 +00:00
exports.listFileTree = (dirPath, miniMatchFilter) => {
2016-09-17 21:11:44 +00:00
let done = plugins.q.defer();
2016-07-17 15:34:15 +00:00
let options = {
2016-06-30 23:37:48 +00:00
cwd: dirPath
};
2016-07-17 15:34:15 +00:00
plugins.glob(miniMatchFilter, options, (err, files) => {
2016-06-30 23:37:48 +00:00
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;
};
2016-09-20 15:56:49 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLmZzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRmaWxlLmZzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwwQkFBdUI7QUFFdkIsK0NBQStDO0FBQy9DLGdFQUFnRTtBQUVoRTs7aUVBRWlFO0FBRWpFOzs7O0dBSUc7QUFDUSxRQUFBLGNBQWMsR0FBRyxVQUFTLFFBQVE7SUFDekMsSUFBSSxjQUFjLEdBQVksS0FBSyxDQ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