smartfile/dist/smartfile.fs.js

255 lines
25 KiB
JavaScript
Raw Normal View History

2016-06-23 16:31:55 +00:00
"use strict";
require("typings-global");
var plugins = require("./smartfile.plugins");
var SmartfileInterpreter = require("./smartfile.interpreter");
/*===============================================================
2016-06-24 01:36:51 +00:00
============================ Checks =============================
===============================================================*/
/**
*
* @param filePath
* @returns {boolean}
*/
exports.fileExistsSync = function (filePath) {
var fileExistsBool = false;
try {
plugins.fs.readFileSync(filePath);
fileExistsBool = true;
}
catch (err) {
fileExistsBool = false;
}
return fileExistsBool;
};
/**
*
* @param filePath
* @returns {any}
*/
exports.fileExists = function (filePath) {
var done = plugins.q.defer();
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
err ? done.reject() : done.resolve();
});
return done.promise;
};
/**
* Checks if given path points to an existing directory
*/
exports.isDirectory = function (pathArg) {
return plugins.fs.statSync(pathArg).isDirectory();
};
/**
* Checks if a given path points to an existing file
*/
exports.isFile = function (pathArg) {
return plugins.fs.statSync(pathArg).isFile();
};
/*===============================================================
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
*/
exports.ensureDir = function (dirPathArg) {
var done = plugins.q.defer();
plugins.fs.ensureDir(dirPathArg, done.resolve);
return done.promise;
};
/**
* ensures that a directory is in place
*/
exports.ensureDirSync = function (dirPathArg) {
plugins.fs.ensureDirSync(dirPathArg);
};
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) {
var done = plugins.q.defer();
plugins.fs.copy(fromArg, toArg, {}, function () {
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) {
plugins.fs.copySync(fromArg, toArg);
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) {
var done = plugins.q.defer();
plugins.fs.remove(pathArg, function () {
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) {
plugins.fs.removeSync(pathArg);
return true;
};
/*===============================================================
============================ Write/Read =========================
===============================================================*/
/**
*
* @param filePathArg
* @returns {*}
*/
exports.toGulpStreamSync = function (filePathArg) {
var stream = plugins.gulp.src(filePathArg);
return stream;
};
exports.toGulpDestSync = function (folderPathArg) {
return plugins.gulp.dest(folderPathArg);
};
/**
*
* @param filePathArg
* @param fileTypeArg
* @returns {any}
*/
exports.toObjectSync = function (filePathArg, fileTypeArg) {
var fileString = plugins.fs.readFileSync(filePathArg, 'utf8');
var fileType;
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) {
var fileString;
fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString;
};
/**
*
* @param filePathArg
* @param options
* @returns {number}
*/
exports.toVinylSync = function (filePathArg, options) {
if (options === void 0) { options = {}; }
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-06-23 16:31:55 +00:00
var done = plugins.q.defer();
var folderArray = plugins.fs.readdirSync(pathArg).filter(function (file) {
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isDirectory();
});
if (regexFilter) {
folderArray = folderArray.filter(function (fileItem) {
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) {
var folderArray = plugins.fs.readdirSync(pathArg).filter(function (file) {
2016-06-23 16:39:02 +00:00
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isDirectory();
});
if (regexFilter) {
folderArray = folderArray.filter(function (fileItem) {
regexFilter.test(fileItem);
});
}
;
return folderArray;
};
/**
* lists Files in a directory on local disk
* @returns Promise
*/
exports.listFiles = function (pathArg, regexFilter) {
var done = plugins.q.defer();
var fileArray = plugins.fs.readdirSync(pathArg).filter(function (file) {
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
fileArray = fileArray.filter(function (fileItem) {
regexFilter.test(fileItem);
});
}
;
done.resolve(fileArray);
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) {
var fileArray = plugins.fs.readdirSync(pathArg).filter(function (file) {
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
fileArray = fileArray.filter(function (fileItem) {
regexFilter.test(fileItem);
});
}
;
return fileArray;
};
/**
* lists all items (folders AND files) in a directory on local disk
* @returns Promise
*/
exports.listAllItems = function (pathArg, regexFilter) {
var done = plugins.q.defer();
var allItmesArray = plugins.fs.readdirSync(pathArg);
if (regexFilter) {
allItmesArray = allItmesArray.filter(function (fileItem) {
regexFilter.test(fileItem);
});
}
;
done.resolve(allItmesArray);
return done.promise();
};
/**
* lists all items (folders AND files) SYNCHRONOUSLY in a directory on local disk
* @returns an array with the folder names as strings
*/
exports.listAllItemsSync = function (pathArg, regexFilter) {
var allItmesArray = plugins.fs.readdirSync(pathArg).filter(function (file) {
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isFile();
});
if (regexFilter) {
allItmesArray = allItmesArray.filter(function (fileItem) {
regexFilter.test(fileItem);
});
}
;
return allItmesArray;
2016-06-23 16:39:02 +00:00
};
2016-06-23 16:31:55 +00:00
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNtYXJ0ZmlsZS5mcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBRXhCLElBQU8sT0FBTyxXQUFXLHFCQUFxQixDQUFDLENBQUM7QUFDaEQsSUFBTyxvQkFBb0IsV0FBVyx5QkFBeUIsQ0FBQyxDQUFDO0FBRWpFOztpRUFFaUU7QUFFakU7Ozs7R0FJRztBQUNRLHNCQUFjLEdBQUcsVUFBUyxRQUFRO0lBQ3pDLElBQUksY0FBYyxHQUFXLEtBQUssQ0FBQztJQUNuQyxJQUFJLENBQUM7UUFDRCxPQUFPLENBQUMsRUFBRSxDQUFDLFlBQVksQ0FBQyxRQUFRLENBQUMsQ0FBQztRQUNsQyxjQUFjLEdBQUcsSUFBSSxDQUFBO0lBQ3pCLENBQ0E7SUFBQSxLQUFLLENBQUEsQ0FBQyxHQUFHLENBQUMsQ0FBQSxDQUFDO1FBQ1AsY0FBYyxHQUFHLEtBQUssQ0FBQztJQUMzQixDQUFDO0lBQ0QsTUFBTSxDQUFDLGNBQWMsQ0FBQztBQUMxQixDQUFDLENBQUM7QUFFRjs7OztHQUlHO0FBQ1Esa0JBQVUsR0FBRyxVQUFTLFFBQVE7SUFDckMsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixPQUFPLENBQUMsRUFBRSxDQUFDLE1BQU0sQ0FBQyxRQUFRLEVBQUUsT0FBTyxDQUFDLEVBQUUsQ0FBQyxJQUFJLEVBQUUsVUFBVSxHQUFHO1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