smartfile/dist/smartfile.fs.js

272 lines
17 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:10:27 +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:10:27 +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-17 21:10:27 +00:00
var 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-17 21:10:27 +00:00
var 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-07-19 18:28:28 +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:10:27 +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:10:27 +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:10:27 +00:00
* @returns Promise<string[]>
*/
exports.listAllItems = function (pathArg, regexFilter) {
2016-09-17 21:10:27 +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:10:27 +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:10:27 +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:10:27 +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:10:27 +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-17 21:10:27 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLmZzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRmaWxlLmZzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFFeEIsTUFBTyxPQUFPLFdBQVcscUJBQXFCLENBQUMsQ0FBQztBQUNoRCxNQUFPLG9CQUFvQixXQUFXLHlCQUF5QixDQUFDLENBQUM7QUFFakU7O2lFQUVpRTtBQ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