forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileLoader.js
128 lines (109 loc) · 3.68 KB
/
FileLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require, exports, module) {
"use strict";
var FileSystem = brackets.getModule("filesystem/FileSystem"),
spromise = require("libs/js/spromise"),
FileStream = require("FileStream"),
ProjectFiles = require("ProjectFiles");
var inProgress = {};
var httpCache = {};
/**
* Load file from a remote location via http
*
* @param {string} fileName is the name of the file to load
* @returns {spromise}
*/
function fromHttp (fileName) {
if (inProgress[fileName]) {
return inProgress[fileName];
}
if (httpCache[fileName]) {
return httpCache[fileName];
}
inProgress[fileName] = spromise.thenable($.ajax(fileName, {"contentType": "text"}))
.always(function() {
delete inProgress[fileName];
})
.then(function(content) {
return {
fileName: fileName,
fullPath: fileName,
content: content
};
});
return inProgress[fileName];
}
/**
* Load file from local filesystem
*
* @param {string} fileName is the name of the file to open
* @param {string} filePath is a fully resolved file path to search fileName in
* @returns {spromise} if successful, the file meta data is provider. If failed, the reason
* is provided.
*/
function fromDirectory (fileName, filePath) {
var handle = FileSystem.getFileForPath(filePath + fileName);
return spromise(function(resolve, reject) {
handle.exists(function(err, exists) {
if (err || !exists) {
reject(err);
}
else {
resolve(new FileStream({
handle: handle,
fileName: fileName,
filePath: filePath
}));
}
});
});
}
/**
* Interface to load a file
*
* @param {string} fileName is the name of the file to open
* @param {string} filePath is a fully resolved file path to search fileName in
* @returns {spromise} if successful, the file meta data is provider. If failed, the reason
* is provided.
*/
function readFile (fileName, filePath) {
if (/^https?:\/\//.test(fileName)) {
return fromHTTP(fileName);
}
return spromise(function(resolve, reject) {
openFile(fileName, filePath)
.done(function(file) {
file.read().done(resolve);
})
.fail(reject);
});
}
/**
* Opens a file from local filesystem.
*
* @param {string} fileName is the name of the file to open
* @param {string} filePath is a fully resolved file path to search fileName in
* @returns {spromise} if successful, the file meta data is provider. If failed, the reason
* is provided.
*/
function openFile (fileName, filePath) {
return spromise(function(resolve, reject) {
fromDirectory(fileName, filePath).done(resolve)
.fail(function() {
fromDirectory(fileName, ProjectFiles.currentProject.fullPath).done(resolve).fail(reject);
});
}).fail(function(err) {
//console.log("====> error", err, fileName, filePath);
});
}
return {
readFile: readFile,
openFile: openFile,
fromHttp: fromHttp,
fromDirectory: fromDirectory
};
});