forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectory.js
More file actions
1 lines (1 loc) · 3.99 KB
/
Directory.js
File metadata and controls
1 lines (1 loc) · 3.99 KB
1
define(function(require,exports,module){const FileSystemEntry=require("filesystem/FileSystemEntry"),FileSystem=require("filesystem/FileSystem");function Directory(fullPath,fileSystem){this._isDirectory=!0,FileSystemEntry.call(this,fullPath,fileSystem)}function _applyAllCallbacks(callbacks,args){if(callbacks.length>0){var callback=callbacks.pop();try{callback.apply(void 0,args)}finally{_applyAllCallbacks(callbacks,args)}}}Directory.prototype=Object.create(FileSystemEntry.prototype),Directory.prototype.constructor=Directory,Directory.prototype.parentClass=FileSystemEntry.prototype,Directory.prototype._contents=null,Directory.prototype._contentsStats=null,Directory.prototype._contentsStatsErrors=null,Directory.prototype._clearCachedData=function(preserveImmediateChildren){if(FileSystemEntry.prototype._clearCachedData.apply(this),!preserveImmediateChildren)if(this._contents)this._contents.forEach(function(child){child._clearCachedData(!0)});else{var dirPath=this.fullPath;this._fileSystem._index.visitAll(function(entry){entry.parentPath===dirPath&&entry._clearCachedData(!0)})}this._contents=void 0,this._contentsStats=void 0,this._contentsStatsErrors=void 0},Directory.prototype.isEmptyAsync=function(){let that=this;return new Promise((resolve,reject)=>{that.getContents((err,contents)=>{err?reject(err):resolve(0===contents.length)})})},Directory.prototype.unlinkEmptyDirectoryAsync=async function(){let that=this,{entries:entries}=await that.getContentsAsync(),isEmpty;for(let entry of entries)entry.isDirectory&&await entry.unlinkEmptyDirectoryAsync();await that.isEmptyAsync()&&await that.unlinkAsync()},Directory.prototype.getContentsAsync=function(filterNothing=!1){let that=this;return new Promise((resolve,reject)=>{that.getContents((err,contents,entriesStats,entriesStatsErrors)=>{err?reject(err):resolve({entries:contents,entriesStats:entriesStats,entriesStatsErrors:entriesStatsErrors})},filterNothing)})},Directory.prototype.getContents=function(callback,filterNothing=!1){if(!filterNothing){if(this._contentsCallbacks)return void this._contentsCallbacks.push(callback);if(this._contents)return void callback(null,this._contents,this._contentsStats,this._contentsStatsErrors);this._contentsCallbacks=[callback]}this._impl.readdir(this.fullPath,function(err,names,stats){var contents=[],contentsStats=[],contentsStatsErrors;if(err)this._clearCachedData();else{var watched=this._isWatched(!0);names.forEach(function(name,index){var entryPath=this.fullPath+name,entryStats=stats[index],entry;(FileSystem.fileTreeFilter(name)||filterNothing)&&("string"==typeof entryStats?(void 0===contentsStatsErrors&&(contentsStatsErrors={}),contentsStatsErrors[entryPath]=entryStats):(entry=entryStats.isFile?this._fileSystem.getFileForPath(entryPath):this._fileSystem.getDirectoryForPath(entryPath),watched&&(entry._stat=entryStats),contents.push(entry),contentsStats.push(entryStats)))},this),watched&&!filterNothing&&(this._contents=contents,this._contentsStats=contentsStats,this._contentsStatsErrors=contentsStatsErrors)}if(filterNothing)callback(err,contents,contentsStats,contentsStatsErrors);else{var currentCallbacks=this._contentsCallbacks,callbackArgs;this._contentsCallbacks=null,_applyAllCallbacks(currentCallbacks,[err,contents,contentsStats,contentsStatsErrors])}}.bind(this))},Directory.prototype.createAsync=function(){let that=this;return new Promise((resolve,reject)=>{that.create((err,stat)=>{err?reject(err):resolve(stat)})})},Directory.prototype.create=function(callback){callback=callback||function(){},this._fileSystem._beginChange(),this._impl.mkdir(this._path,function(err,stat){if(err){this._clearCachedData();try{return void callback(err)}finally{this._fileSystem._endChange()}}var parent=this._fileSystem.getDirectoryForPath(this.parentPath);this._isWatched()&&(this._stat=stat),this._fileSystem._handleDirectoryChange(parent,function(added,removed){try{callback(null,stat)}finally{parent._isWatched()&&this._fileSystem._fireChangeEvent(parent,added,removed),this._fileSystem._endChange()}}.bind(this))}.bind(this))},module.exports=Directory});