-
Notifications
You must be signed in to change notification settings - Fork 20
/
fileReader.js
80 lines (65 loc) · 2.17 KB
/
fileReader.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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var FileSystem = brackets.getModule("filesystem/FileSystem");
var Promise = require("node_modules/spromise/dist/spromise.min");
var reportError = require("reportError");
var FileStream = require("FileStream");
var ProjectFiles = require("ProjectFiles");
/**
* Load file from local filesystem
*
* @param {string} filePath - is the file path to read
* @returns {spromise} if successful, the file meta data is provider. If failed, the reason
* is provided.
*/
function fromDirectory(filePath) {
return new Promise(function(resolve, reject) {
var handle = FileSystem.getFileForPath(filePath);
handle.exists(function(err, exists) {
if (err || !exists) {
err = err ? err : new Error(exists ? "Unknown file error" : filePath + " was not found");
reject(err);
}
else {
resolve(new FileStream({handle: handle}));
}
});
});
}
/**
* Reads a file based on the project directory.
*/
function fromProject(fileName) {
return fromDirectory(ProjectFiles.currentProject.fullPath + fileName);
}
/**
* Interface to load a file
*
* @param {string} filePath is the name of the file to open
* @returns {spromise} if successful, the file meta data is provider. If failed, the reason
* is provided.
*/
function readFile(filePath) {
var reader = isAbsolute(filePath) ? fromDirectory(filePath) : fromProject(filePath);
return reader.then(function(stream) {
return stream.read();
}, reportError);
}
/**
* Tests if the path is absolute or relative.
*/
function isAbsolute(path) {
return path[0] === '/' || /^[a-zA-Z]:[\/\\]+/.test(path);
}
return {
isAbsolute: isAbsolute,
readFile: readFile,
fromDirectory: fromDirectory,
fromProject: fromProject
};
});