UNPKG

3.26 kBJavaScriptView Raw
1"use strict";
2
3var 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
12function 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
49function 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, // TODO: Set real path if symbolic link.
60 hash: stats.mtime.getTime()
61 });
62 });
63}
64
65function readdir(path, callback) {
66 fs.readdir(path, callback);
67}
68
69function mkdir(path, mode, callback) {
70 mkdirp(path, { mode: mode }, callback);
71}
72
73function rename(oldPath, newPath, callback) {
74 fs.rename(oldPath, newPath, callback);
75}
76
77function readFile(path, encoding, callback) {
78 fs.readFile(path, { encoding: encoding }, callback);
79}
80
81function writeFile(path, data, encoding, callback) {
82 fs.writeFile(path, data, { encoding: encoding }, callback);
83}
84
85function unlink(path, callback) {
86 rimraf(path, callback);
87}
88
89function moveToTrash(path, callback) {
90 rimraf(path, callback);
91}
92
93function watchPath(req, callback) {
94 callback();
95}
96
97function unwatchPath(req, callback) {
98 callback();
99}
100
101function unwatchAll(req, callback) {
102 callback();
103}
104
105function copyFile(src, dest, callback) {
106 ncp(src, dest, callback);
107}
108
109exports.resolvePath = resolvePath;
110exports.stat = stat;
111exports.readdir = readdir;
112exports.mkdir = mkdir;
113exports.rename = rename;
114exports.readFile = readFile;
115exports.writeFile = writeFile;
116exports.unlink = unlink;
117exports.moveToTrash = moveToTrash;
118exports.watchPath = watchPath;
119exports.unwatchPath = unwatchPath;
120exports.unwatchAll = unwatchAll;
121exports.copyFile = copyFile;