UNPKG

1.78 kBJavaScriptView Raw
1"use strict";
2function init(d) {
3 root = d;
4 return root;
5}
6exports.init = init;
7var root = {};
8function addFile(r) {
9 var type = r.type;
10 var filename = r.name;
11 var url = r.url;
12 if (!type)
13 type = "";
14 filename = normalize(filename);
15 let basefilename = basename(filename);
16 let folder = dirname(filename);
17 if (!exists(folder)) {
18 mkdir(folder);
19 }
20 let d = reslove(folder);
21 if (!type) {
22 d[basefilename] = url;
23 }
24 else {
25 d[basefilename] = { url, type };
26 }
27}
28exports.addFile = addFile;
29function getFile(filename) {
30 return reslove(filename);
31}
32exports.getFile = getFile;
33function basename(filename) {
34 return filename.substr(filename.lastIndexOf("/") + 1);
35}
36function normalize(filename) {
37 return filename.split("/").filter(d => !!d).join("/");
38}
39function dirname(path) {
40 return path.substr(0, path.lastIndexOf("/"));
41}
42function reslove(dirpath) {
43 if (dirpath == "") {
44 return root;
45 }
46 dirpath = normalize(dirpath);
47 let list = dirpath.split("/");
48 let current = root;
49 for (let f of list) {
50 current = current[f];
51 }
52 return current;
53}
54function mkdir(dirpath) {
55 dirpath = normalize(dirpath);
56 let list = dirpath.split("/");
57 let current = root;
58 for (let f of list) {
59 if (!current[f]) {
60 current[f] = {};
61 }
62 current = current[f];
63 }
64}
65exports.mkdir = mkdir;
66function exists(dirpath) {
67 if (dirpath == "")
68 return true;
69 dirpath = normalize(dirpath);
70 let list = dirpath.split("/");
71 let current = root;
72 for (let f of list) {
73 if (!current[f]) {
74 return false;
75 }
76 current = current[f];
77 }
78 return true;
79}
80exports.exists = exists;