1 const ROOT_NAMESPACE_NAME = '__rootNamespace__';
4 constructor(name, parentNamespace) {
6 this.parentNamespace = parentNamespace;
7 this.childNamespaces = {};
10 this.path = this.getPath();
14 return this._getFullName();
18 this.tasks[task.name] = task;
19 task.namespace = this;
27 let taskPath = name.split(':');
28 let taskName = taskPath.pop();
32 // Namespaced, return either relative to current, or from root
33 if (taskPath.length) {
34 taskPath = taskPath.join(':');
35 ns = this.resolveNamespace(taskPath) ||
36 Namespace.ROOT_NAMESPACE.resolveNamespace(taskPath);
37 task = (ns && ns.resolveTask(taskName));
39 // Bare task, return either local, or top-level
41 task = this.tasks[name] || Namespace.ROOT_NAMESPACE.tasks[name];
48 resolveNamespace(relativeName) {
53 let parts = relativeName.split(':');
56 for (let i = 0, ii = parts.length; (ns && i < ii); i++) {
57 ns = ns.childNamespaces[parts[i]];
63 matchRule(relativeName) {
64 let parts = relativeName.split(':');
66 let ns = this.resolveNamespace(parts.join(':'));
67 let rules = ns ? ns.rules : [];
71 for (let p in rules) {
73 if (r.match(relativeName)) {
78 return (ns && match) ||
79 (this.parentNamespace &&
80 this.parentNamespace.matchRule(relativeName));
85 let next = this.parentNamespace;
87 parts.push(next.name);
88 next = next.parentNamespace;
90 parts.pop(); // Remove '__rootNamespace__'
91 return parts.reverse().join(':');
96 path = (path && path.split(':')) || [];
98 return path.join(':');
102 return !this.parentNamespace;
106 class RootNamespace extends Namespace {
108 super(ROOT_NAMESPACE_NAME, null);
109 Namespace.ROOT_NAMESPACE = this;
113 module.exports.Namespace = Namespace;
114 module.exports.RootNamespace = RootNamespace;