1 | "use strict";
|
2 |
|
3 | var fs = require("fs"),
|
4 | ncp = require("ncp"),
|
5 | mkdirp = require("mkdirp"),
|
6 | rimraf = require("rimraf"),
|
7 | pathUtil = require("path"),
|
8 | suppRoot = "/support/",
|
9 | sampRoot = "/samples/",
|
10 | projRoot = "/projects/";
|
11 |
|
12 | function resolvePath(reqPath, context, callback) {
|
13 | var extRoot = context.httpRoot + "/extensions/",
|
14 | userExt = context.httpRoot + "/extensions/user",
|
15 | root,
|
16 | err,
|
17 | res;
|
18 |
|
19 | if (reqPath.startsWith(projRoot)) {
|
20 | root = context.projectsDir;
|
21 | res = pathUtil.join(root, reqPath.substr(projRoot.length));
|
22 | } else if (reqPath.startsWith(userExt)) {
|
23 | root = context.supportDir + "/extensions/user";
|
24 | res = pathUtil.join(root, reqPath.substr(userExt.length));
|
25 | } else if (reqPath.startsWith(extRoot)) {
|
26 | root = context.defaultExtensions;
|
27 | res = pathUtil.join(context.defaultExtensions, reqPath.substr(extRoot.length));
|
28 | } else if (reqPath.startsWith(suppRoot)) {
|
29 | root = context.supportDir;
|
30 | res = pathUtil.join(context.supportDir, reqPath.substr(suppRoot.length));
|
31 | } else if (reqPath.startsWith(sampRoot)) {
|
32 | root = context.samplesDir;
|
33 | res = pathUtil.join(context.samplesDir, reqPath.substr(sampRoot.length));
|
34 | } else {
|
35 | err = new Error("No such file or directory.");
|
36 | err.code = "ENOENT";
|
37 | return callback(err);
|
38 | }
|
39 |
|
40 | if (res.substr(0, root.length) !== root) {
|
41 | err = new Error("Permission denied.");
|
42 | err.code = "EACCES";
|
43 | callback(err);
|
44 | } else {
|
45 | callback(null, res);
|
46 | }
|
47 | }
|
48 |
|
49 | function stat(path, callback) {
|
50 | fs.stat(path, function (err, stats) {
|
51 | if (err) {
|
52 | return callback(err);
|
53 | }
|
54 |
|
55 | callback(null, {
|
56 | isFile: stats.isFile(),
|
57 | mtime: stats.mtime,
|
58 | size: stats.size,
|
59 | realPath: null,
|
60 | hash: stats.mtime.getTime()
|
61 | });
|
62 | });
|
63 | }
|
64 |
|
65 | function readdir(path, callback) {
|
66 | fs.readdir(path, callback);
|
67 | }
|
68 |
|
69 | function mkdir(path, mode, callback) {
|
70 | mkdirp(path, { mode: mode }, callback);
|
71 | }
|
72 |
|
73 | function rename(oldPath, newPath, callback) {
|
74 | fs.rename(oldPath, newPath, callback);
|
75 | }
|
76 |
|
77 | function readFile(path, encoding, callback) {
|
78 | fs.readFile(path, { encoding: encoding }, callback);
|
79 | }
|
80 |
|
81 | function writeFile(path, data, encoding, callback) {
|
82 | fs.writeFile(path, data, { encoding: encoding }, callback);
|
83 | }
|
84 |
|
85 | function unlink(path, callback) {
|
86 | rimraf(path, callback);
|
87 | }
|
88 |
|
89 | function moveToTrash(path, callback) {
|
90 | rimraf(path, callback);
|
91 | }
|
92 |
|
93 | function watchPath(req, callback) {
|
94 | callback();
|
95 | }
|
96 |
|
97 | function unwatchPath(req, callback) {
|
98 | callback();
|
99 | }
|
100 |
|
101 | function unwatchAll(req, callback) {
|
102 | callback();
|
103 | }
|
104 |
|
105 | function copyFile(src, dest, callback) {
|
106 | ncp(src, dest, callback);
|
107 | }
|
108 |
|
109 | exports.resolvePath = resolvePath;
|
110 | exports.stat = stat;
|
111 | exports.readdir = readdir;
|
112 | exports.mkdir = mkdir;
|
113 | exports.rename = rename;
|
114 | exports.readFile = readFile;
|
115 | exports.writeFile = writeFile;
|
116 | exports.unlink = unlink;
|
117 | exports.moveToTrash = moveToTrash;
|
118 | exports.watchPath = watchPath;
|
119 | exports.unwatchPath = unwatchPath;
|
120 | exports.unwatchAll = unwatchAll;
|
121 | exports.copyFile = copyFile;
|