Skip to content

Commit 52e8ff4

Browse files
authored
Merge pull request #53 from aicore/fs
improved pheonix vfs handling
2 parents 2e8b95b + db118ab commit 52e8ff4

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

src/file/FileUtils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ define(function (require, exports, module) {
308308
*/
309309
function getNativeBracketsDirectoryPath() {
310310
var pathname = decodeURI(window.location.pathname);
311-
var directory = pathname.substr(0, pathname.lastIndexOf("/"));
312-
return convertToNativePath(directory);
311+
return pathname.substr(0, pathname.lastIndexOf("/")); // In the web, native path is the base url
313312
}
314313

315314
/**

src/filesystem/impls/appshell/AppshellFileSystem.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ define(function (require, exports, module) {
6666
_domainPath = [_bracketsPath, _modulePath, _nodePath].join("/"),
6767
_nodeDomain = new NodeDomain("fileWatcher", _domainPath);
6868

69-
var _isRunningOnWindowsXP = window.navigator.userAgent.indexOf("Windows NT 5.") >= 0;
70-
7169

7270
// If the connection closes, notify the FileSystem that watchers have gone offline.
7371
_nodeDomain.connection.on("close", function (event, promise) {
@@ -177,6 +175,17 @@ define(function (require, exports, module) {
177175
return FileSystemError.UNKNOWN;
178176
}
179177

178+
/**
179+
* Normalises path.
180+
*
181+
* @param {string} path The path to normalise
182+
* @return {string} Normalised path.
183+
* @private
184+
*/
185+
function _normalise_path(path) {
186+
return window.Phoenix.VFS.path.normalize(path);
187+
}
188+
180189
/**
181190
* Convert a callback to one that transforms its first parameter from an
182191
* appshell error code to a FileSystemError string.
@@ -233,6 +242,7 @@ define(function (require, exports, module) {
233242
*/
234243
function stat(path, callback) {
235244
console.log('stat: ', path);
245+
path = _normalise_path(path);
236246
appshell.fs.stat(path, function (err, stats) {
237247
if (err) {
238248
callback(_mapError(err));
@@ -264,6 +274,7 @@ define(function (require, exports, module) {
264274
*/
265275
function exists(path, callback) {
266276
console.log('exists: ', path);
277+
path = _normalise_path(path);
267278
stat(path, function (err) {
268279
if (err) {
269280
if (err === FileSystemError.NOT_FOUND) {
@@ -291,6 +302,7 @@ define(function (require, exports, module) {
291302
*/
292303
function readdir(path, callback) {
293304
console.log('readdir: ', path);
305+
path = _normalise_path(path);
294306
appshell.fs.readdir(path, function (err, contents) {
295307
if (err) {
296308
callback(_mapError(err));
@@ -305,7 +317,7 @@ define(function (require, exports, module) {
305317

306318
var stats = [];
307319
contents.forEach(function (val, idx) {
308-
stat(path + "/" + val, function (err, stat) {
320+
stat(_normalise_path(path + "/" + val), function (err, stat) {
309321
stats[idx] = err || stat;
310322
count--;
311323
if (count <= 0) {
@@ -328,11 +340,12 @@ define(function (require, exports, module) {
328340
*/
329341
function mkdir(path, mode, callback) {
330342
console.log('mkdir: ', path);
343+
path = _normalise_path(path);
331344
if (typeof mode === "function") {
332345
callback = mode;
333346
mode = parseInt("0755", 8);
334347
}
335-
appshell.fs.mkdir(path, mode, function (err) {
348+
appshell.fs.mkdirs(path, mode, true, function (err) {
336349
if (err) {
337350
callback(_mapError(err));
338351
} else {
@@ -353,6 +366,8 @@ define(function (require, exports, module) {
353366
*/
354367
function rename(oldPath, newPath, callback) {
355368
console.log('rename: ', oldPath, ' to ', newPath);
369+
oldPath = _normalise_path(oldPath);
370+
newPath = _normalise_path(newPath);
356371
appshell.fs.rename(oldPath, newPath, _wrap(callback));
357372
}
358373

@@ -374,6 +389,7 @@ define(function (require, exports, module) {
374389
*/
375390
function readFile(path, options, callback) {
376391
console.log('Reading file: ', path);
392+
path = _normalise_path(path);
377393
var encoding = window.Phoenix.VFS.getFsEncoding(options.encoding) || "utf8";
378394

379395
// callback to be executed when the call to stat completes
@@ -423,6 +439,7 @@ define(function (require, exports, module) {
423439
*/
424440
function writeFile(path, data, options, callback) {
425441
console.log('Write file: ', path);
442+
path = _normalise_path(path);
426443
var encoding = window.Phoenix.VFS.getFsEncoding(options.encoding) || "utf8",
427444
preserveBOM = options.preserveBOM;
428445

@@ -483,6 +500,7 @@ define(function (require, exports, module) {
483500
*/
484501
function unlink(path, callback) {
485502
console.log('delete file: ', path);
503+
path = _normalise_path(path);
486504
appshell.fs.unlink(path, function (err) {
487505
callback(_mapError(err));
488506
});
@@ -498,6 +516,7 @@ define(function (require, exports, module) {
498516
*/
499517
function moveToTrash(path, callback) {
500518
console.log('Trash file: ', path);
519+
path = _normalise_path(path);
501520
appshell.fs.moveToTrash(path, function (err) {
502521
callback(_mapError(err));
503522
});
@@ -523,7 +542,7 @@ define(function (require, exports, module) {
523542
_changeCallback = changeCallback;
524543
_offlineCallback = offlineCallback;
525544

526-
if (_isRunningOnWindowsXP && _offlineCallback) {
545+
if (_offlineCallback) {
527546
_offlineCallback();
528547
}
529548
}
@@ -542,6 +561,7 @@ define(function (require, exports, module) {
542561
*/
543562
function watchPath(path, ignored, callback) {
544563
console.log('Watch path: ', path);
564+
path = _normalise_path(path);
545565
callback(FileSystemError.NOT_SUPPORTED);
546566
return;
547567
// TODO: Phoenix. file watch fix.
@@ -571,6 +591,7 @@ define(function (require, exports, module) {
571591
*/
572592
function unwatchPath(path, ignored, callback) {
573593
console.log('unwatch path: ', path);
594+
path = _normalise_path(path);
574595
_nodeDomain.exec("unwatchPath", path)
575596
.then(callback, callback);
576597
}

src/phoenix/init_vfs.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2021 - present core.ai . All rights reserved.
3+
* Acknowledgements: https://github.com/bpedro/node-fs
34
*
45
* Permission is hereby granted, free of charge, to any person obtaining a
56
* copy of this software and associated documentation files (the "Software"),
@@ -136,12 +137,66 @@ const _createDefaultProject = function (vfs) {
136137
});
137138
};
138139

140+
// Adds recursive dir creation support to fs.mkdirs()
141+
const _patchFSLib = function(fsLib, pathLib) {
142+
/**
143+
* Offers functionality similar to mkdir -p
144+
*
145+
* Asynchronous operation. No arguments other than a possible exception
146+
* are given to the completion callback.
147+
*/
148+
function mkdir_p (path, mode, callback, position) {
149+
const osSep = '/';
150+
const parts = pathLib.normalize(path).split(osSep);
151+
152+
mode = mode || process.umask();
153+
position = position || 0;
154+
155+
if (position >= parts.length) {
156+
return callback();
157+
}
158+
159+
var directory = parts.slice(0, position + 1).join(osSep) || osSep;
160+
fsLib.stat(directory, function(err) {
161+
if (err === null) {
162+
mkdir_p(path, mode, callback, position + 1);
163+
} else {
164+
fsLib.mkdir(directory, mode, function (err) {
165+
if (err && err.code != 'EEXIST') {
166+
return callback(err);
167+
} else {
168+
mkdir_p(path, mode, callback, position + 1);
169+
}
170+
});
171+
}
172+
});
173+
}
174+
175+
fsLib.mkdirs = function (path, mode, recursive, callback) {
176+
if (typeof recursive !== 'boolean') {
177+
callback = recursive;
178+
recursive = false;
179+
}
180+
181+
if (typeof callback !== 'function') {
182+
callback = function () {};
183+
}
184+
185+
if (!recursive) {
186+
fsLib.mkdir(path, mode, callback);
187+
} else {
188+
mkdir_p(path, mode, callback);
189+
}
190+
}
191+
}
192+
139193

140194
export default function init(Phoenix, FilerLib) {
141195
if(!FilerLib || !Phoenix){
142196
alertError(_FS_ERROR_MESSAGE);
143197
}
144198

199+
_patchFSLib(FilerLib.fs, FilerLib.path);
145200
const vfs = _setupVFS(Phoenix, FilerLib.fs, FilerLib.path);
146201
_createAppDirs(vfs);
147202
_createDefaultProject(vfs);

src/phoenix/shell.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let Phoenix = {};
3939

4040
window.Phoenix = Phoenix;
4141
window.fs = Filer.fs;
42+
window.path = Filer.path;
4243

4344
init(Phoenix, Filer);
4445

0 commit comments

Comments
 (0)