diff options
author | Phie <phie@phie.ovh> | 2018-08-18 20:09:23 +0300 |
---|---|---|
committer | Phie <phie@phie.ovh> | 2018-08-18 20:09:23 +0300 |
commit | d492a1e2482e762f44796292757d342766f3795d (patch) | |
tree | ce45ee9de63741006a208e52e65d83d630ed19c6 /utils | |
parent | 31e4e84ed4f8cbb5e1c2c9cde0d8bad4817d07df (diff) |
in the future we shouldn't need compatibility libs
Diffstat (limited to 'utils')
-rw-r--r-- | utils/file_utils.js | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/utils/file_utils.js b/utils/file_utils.js index 4f80946..8e25f19 100644 --- a/utils/file_utils.js +++ b/utils/file_utils.js @@ -42,12 +42,12 @@ FileUtils.geMimetypeFromExtension = function (extension) { } } -FileUtils.getExtensionFromPath = function(path){ +FileUtils.getExtensionFromPath = function (path) { return path.split('.').pop().toLowerCase(); } -var fs = require('fs'); FileUtils.deleteFolderRecursive = function (path) { + var fs = require('fs'); var utils = this; if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function (file, index) { @@ -83,6 +83,31 @@ FileUtils.stripExtensionFromName = function (name) { return name.replace(/\.[^/.]+$/, "") } -FileUtils.getParentFolderFromPath = require('path').dirname; + +FileUtils.splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; + + +FileUtils.posixSplitPath = function (filename) { + return FileUtils.splitPathRe.exec(filename).slice(1); +} + +FileUtils.getParentFolderFromPath = function (path) { + var result = FileUtils.posixSplitPath(path), + root = result[0], + dir = result[1]; + + if (!root && !dir) { + // No dirname whatsoever + return '.'; + } + + if (dir) { + // It has a dirname, strip trailing slash + dir = dir.substr(0, dir.length - 1); + } + + return root + dir; +}; exports.FileUtils = FileUtils;
\ No newline at end of file |