1 | "use strict";
|
2 | function init(d) {
|
3 | root = d;
|
4 | return root;
|
5 | }
|
6 | exports.init = init;
|
7 | var root = {};
|
8 | function 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 | }
|
28 | exports.addFile = addFile;
|
29 | function getFile(filename) {
|
30 | return reslove(filename);
|
31 | }
|
32 | exports.getFile = getFile;
|
33 | function basename(filename) {
|
34 | return filename.substr(filename.lastIndexOf("/") + 1);
|
35 | }
|
36 | function normalize(filename) {
|
37 | return filename.split("/").filter(d => !!d).join("/");
|
38 | }
|
39 | function dirname(path) {
|
40 | return path.substr(0, path.lastIndexOf("/"));
|
41 | }
|
42 | function 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 | }
|
54 | function 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 | }
|
65 | exports.mkdir = mkdir;
|
66 | function 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 | }
|
80 | exports.exists = exists;
|