-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathAbstractReader.js
More file actions
130 lines (122 loc) · 3.66 KB
/
AbstractReader.js
File metadata and controls
130 lines (122 loc) · 3.66 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import randomInt from "random-int";
import Trace from "./tracing/Trace.js";
/**
* Abstract resource locator implementing the general API for <b>reading</b> resources
*
* @abstract
* @public
* @class
* @alias @ui5/fs/AbstractReader
*/
class AbstractReader {
/**
* The constructor.
*
* @public
* @param {string} name Name of the reader. Typically used for tracing purposes
*/
constructor(name) {
if (new.target === AbstractReader) {
throw new TypeError("Class 'AbstractReader' is abstract");
}
this._name = name;
}
/*
* Returns the name of the reader instance. This can be used for logging/tracing purposes.
*
* @returns {string} Name of the reader
*/
getName() {
return this._name || `<unnamed ${this.constructor.name} Reader>`;
}
/**
* Locates resources by matching glob patterns.
*
* ```js
* byGlob("**/*.{html,htm}");
* byGlob("**/.library");
* byGlob("/pony/*");
* ```
*
* @public
* @param {string|string[]} virPattern glob pattern as string or array of glob patterns for
* virtual directory structure
* @param {object} [options] glob options
* @param {boolean} [options.nodir=true] Do not match directories
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving to list of resources
*/
byGlob(virPattern, options = {nodir: true}) {
const trace = new Trace(virPattern);
return this._byGlob(virPattern, options, trace).then(function(result) {
trace.printReport();
return result;
}).then((resources) => {
if (resources.length > 1) {
// Pseudo randomize result order to prevent consumers from relying on it:
// Swap the first object with a randomly chosen one
const x = 0;
const y = randomInt(0, resources.length - 1);
// Swap object at index "x" with "y"
resources[x] = [resources[y], resources[y]=resources[x]][0];
}
return resources;
});
}
/**
* Locates resources by matching a given path.
*
* @public
* @param {string} virPath Virtual path
* @param {object} [options] Options
* @param {boolean} [options.nodir=true] Do not match directories
* @returns {Promise<@ui5/fs/Resource>} Promise resolving to a single resource
*/
byPath(virPath, options = {nodir: true}) {
const trace = new Trace(virPath);
return this._byPath(virPath, options, trace).then(function(resource) {
trace.printReport();
return resource;
});
}
/**
* Locates resources by one or more glob patterns.
*
* @abstract
* @protected
* @param {string|string[]} virPattern glob pattern as string or an array of
* glob patterns for virtual directory structure
* @param {object} options glob options
* @param {@ui5/fs/tracing.Trace} trace Trace instance
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving to list of resources
*/
_byGlob(virPattern, options, trace) {
throw new Error("Function '_byGlob' is not implemented");
}
/**
* Locate resources by matching a single glob pattern.
*
* @abstract
* @protected
* @param {string} pattern glob pattern
* @param {object} options glob options
* @param {@ui5/fs/tracing.Trace} trace Trace instance
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving to list of resources
*/
_runGlob(pattern, options, trace) {
throw new Error("Function '_runGlob' is not implemented");
}
/**
* Locates resources by path.
*
* @abstract
* @protected
* @param {string} virPath Virtual path
* @param {object} options Options
* @param {@ui5/fs/tracing.Trace} trace Trace instance
* @returns {Promise<@ui5/fs/Resource>} Promise resolving to a single resource
*/
_byPath(virPath, options, trace) {
throw new Error("Function '_byPath' is not implemented");
}
}
export default AbstractReader;