Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* [ringo-fs] File and Directory.

  • Loading branch information...
commit bfa450ba45973866e40cb2875052b89aa9bcf4a9 1 parent a4bcff7
@keeto authored
Showing with 119 additions and 48 deletions.
  1. +119 −48 Source/engines/ringo/0.5.0/fs.js
View
167 Source/engines/ringo/0.5.0/fs.js
@@ -18,7 +18,7 @@ var removeTree = function removeTree(path) {
if (!file.isAbsolute()) file = file.getAbsoluteFile();
if (file.isDirectory() && !fs.isLink(file.getPath())) {
file.list().forEach(function(child){
- removeTree(join(file, child));
+ removeTree(fs.join(file, child));
});
}
if (!file['delete']()) {
@@ -27,40 +27,38 @@ var removeTree = function removeTree(path) {
};
-// File Class
+// Directory Class
-var _File = exports.File = new Class({
+var _Dir = exports.Directory = new Class({
- Extends: fscommon.File,
+ Extends: fscommon.Directory,
- resolve: function(path, skipCanon){
+ resolve: function(path){
var cwd = fs.workingDirectory();
- if (!skipCanon) path = fscommon.toCanonical(path || cwd, cwd);
+ path = fscommon.toCanonical(path || cwd, cwd);
this.$path = path;
return this;
},
remove: function(recursive){
- if (!this.exists()) return this.onRemoveError(new Error('File does not exists.'));
+ if (!this.exists()) return this.onRemoveError(new Error('Directory does not exists.'));
var self = this,
err = false,
path = this.$path;
- if (this.isDirectory()){
- var dir = this.clone();
- dir.addEvents({
- list: function(items){
- if (!recursive && items.length){
- err = true;
- self.onRemoveError('File cannot be removed; not empty.');
- }
- },
- listError: function(e){
+ var dir = this.clone();
+ dir.addEvents({
+ list: function(items){
+ if (!recursive && items.length){
err = true;
- self.onRemoveError(e);
+ self.onRemoveError('Directory cannot be removed; not empty.');
}
- }).list();
- }
+ },
+ listError: function(e){
+ err = true;
+ self.onRemoveError(e);
+ }
+ }).list();
if (!err){
try {
@@ -74,6 +72,19 @@ var _File = exports.File = new Class({
return this;
},
+ move: function(to){
+ if (to == null) return this.onMoveError(new Error('Destination argument `to` is required.'));
+ if (!this.exists()) return this.onMoveError(new Error('Directory does not exists.'));
+ to = fscommon.toCanonical(to, fs.workingDirectory());
+ try {
+ fs.move(this.$path, to);
+ this.onMove(to);
+ } catch(e){
+ this.onMoveError(e);
+ }
+ return this;
+ },
+
// Info Functions
exists: function(){
@@ -91,7 +102,7 @@ var _File = exports.File = new Class({
// Stat Functions
getStat: function(){
- if (!this.exists()) return this.onStatError(new Error('File does not exists.'));
+ if (!this.exists()) return this.onStatError(new Error('Directory does not exists.'));
try {
var path = this.$path,
size = fs.size(path),
@@ -104,12 +115,10 @@ var _File = exports.File = new Class({
return this;
},
-
// Directory Functions
list: function(){
- if (!this.exists()) return this.onListError(new Error('File does not exists.'));
- if (this.isFile()) return this.onListError(new Error('File is not a directory.'));
+ if (!this.exists()) return this.onListError(new Error('Directory does not exists.'));
var path = this.$path;
try {
var results = fs.list(path);
@@ -120,39 +129,115 @@ var _File = exports.File = new Class({
return this;
},
- createDir: function(recurse){
+ create: function(recurse){
var self = this;
if (this.exists()){
- if (this.isFile()) return this.onCreateDirError(new Error('File is not a directory'));
- return this.onCreateDir();
+ if (this.isFile()) return this.onCreateError(new Error('Directory is not a directory'));
+ return this.onCreate();
} else {
var path = this.$path,
parent = this.getParent();
- if (!recurse && !parent.exists()) return this.onCreateDirError(new Error('File cannot be created as directory.'));
+ if (!recurse && !parent.exists()) return this.onCreateError(new Error('Directory cannot be created.'));
try {
fs.makeTree(path);
- self.onCreateDir();
+ self.onCreate();
} catch(e){
- self.onCreateDirError(e);
+ self.onCreateError(e);
}
}
return this;
},
makeDir: function(dir, recurse){
+ if (dir == null) return this.onMakeDirError('Argument dirName is required.');
var file = new this.$constructor(fscommon.toCanonical(dir, this.$path));
file.addEvents({
- createDir: this.onMakeDir.bind(this),
- createDirError: this.onMakeDirError.bind(this)
- }).createDir(recurse);
+ create: this.onMakeDir.bind(this),
+ createError: this.onMakeDirError.bind(this)
+ }).create(recurse);
+ return this;
+ }
+
+});
+
+
+// File Class
+
+var _File = exports.File = new Class({
+
+ Extends: fscommon.File,
+
+ resolve: function(path){
+ var cwd = fs.workingDirectory();
+ path = fscommon.toCanonical(path || cwd, cwd);
+ this.$path = path;
return this;
},
+ remove: function(recursive){
+ if (!this.exists()) return this.onRemoveError(new Error('File does not exists.'));
+ var self = this,
+ err = false,
+ path = this.$path;
+
+ try {
+ removeTree(path);
+ this.onRemove();
+ } catch(e){
+ this.onRemoveError(e);
+ }
+
+ return this;
+ },
+
+ move: function(to){
+ if (to == null) return this.onMoveError(new Error('Destination argument `to` is required.'));
+ if (!this.exists()) return this.onMoveError(new Error('File does not exists.'));
+ to = fscommon.toCanonical(to, fs.workingDirectory());
+ try {
+ fs.move(this.$path, to);
+ this.onMove(to);
+ } catch(e){
+ this.onMoveError(e);
+ }
+ return this;
+ },
+
+ // Info Functions
+
+ exists: function(){
+ return fs.exists(this.$path);
+ },
+
+ isDirectory: function(){
+ return fs.isDirectory(this.$path);
+ },
+
+ isFile: function(){
+ return fs.isFile(this.$path);
+ },
+
+ // Stat Functions
+
+ getStat: function(){
+ if (!this.exists()) return this.onStatError(new Error('File does not exists.'));
+ try {
+ var path = this.$path,
+ size = fs.size(path),
+ modified = fs.lastModified(path);
+ if (size !== undefined && modified !== undefined) return this.onStat({size: size, modified: modified.getTime()});
+ throw new Error('Cannot read stats.');
+ } catch(e){
+ this.onStatError(e);
+ }
+ return this;
+ },
+
+
// File Functions
read: function(){
if (!this.exists()) return this.onReadError(new Error('File does not exists.'));
- if (this.isDirectory()) return this.onReadError(new Error('File is a directory.'));
var path = this.$path;
try {
var file = fs.open(path, {read: 1});
@@ -166,7 +251,6 @@ var _File = exports.File = new Class({
},
write: function(data, append){
- if (this.isDirectory()) return this.onReadError(new Error('File is a directory.'));
var path = this.$path;
try {
var options = {binary: data instanceof Binary};
@@ -181,19 +265,6 @@ var _File = exports.File = new Class({
this.onWriteError(e, data);
}
return this;
- },
-
- move: function(to){
- if (to == null) return this.onMoveError(new Error('Destination argument `to` is required.'));
- if (!this.exists()) return this.onMoveError(new Error('File does not exists.'));
- to = fscommon.toCanonical(to, fs.workingDirectory());
- try {
- fs.move(this.$path, to);
- this.onMove(to);
- } catch(e){
- this.onMoveError(e);
- }
- return this;
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.