Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit c1055d3

Browse files
committed
fix(ngr/node): make script runnable in NodeJS env
1 parent 64b4d09 commit c1055d3

File tree

3 files changed

+173
-163
lines changed

3 files changed

+173
-163
lines changed

src/ngDefine.js

Lines changed: 64 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -11,188 +11,102 @@
1111

1212
(function(window) {
1313

14-
var ngDefine;
15-
16-
var MODULE_DEPENDENCY = /^module:([^:]*)(:(.*))?$/;
17-
var INTERNAL = /^ng/;
18-
19-
function isFunction(value){ return typeof value == 'function'; }
20-
21-
function isInternal(module) {
22-
return INTERNAL.test(module);
23-
}
24-
25-
function asFileDependency(module) {
26-
return module.replace(/\./g, "/");
27-
}
28-
29-
function toArray(arrayLike) {
30-
return Array.prototype.slice.call(arrayLike, 0);
31-
}
32-
33-
/**
34-
* For each implementation as used by AngularJS
35-
*/
36-
function forEach(obj, iterator, context) {
37-
var key;
38-
if (obj) {
39-
if (isFunction(obj)){
40-
for (key in obj) {
41-
if (key != 'prototype' && key != 'length' && key != 'name' && obj.hasOwnProperty(key)) {
42-
iterator.call(context, obj[key], key);
43-
}
44-
}
45-
} else if (obj.forEach && obj.forEach !== forEach) {
46-
obj.forEach(iterator, context);
47-
} else if (isArrayLike(obj)) {
48-
for (key = 0; key < obj.length; key++)
49-
iterator.call(context, obj[key], key);
50-
} else {
51-
for (key in obj) {
52-
if (obj.hasOwnProperty(key)) {
53-
iterator.call(context, obj[key], key);
54-
}
55-
}
56-
}
57-
}
58-
return obj;
59-
}
60-
61-
function parseNgModule(name, dependencies) {
14+
define([ 'angular', 'ngParse' ], function(angular, ngParse) {
6215

63-
var files = [],
64-
modules = [];
16+
//////// utilities /////////
6517

66-
forEach(dependencies, function(d) {
67-
var moduleMatch = d.match(MODULE_DEPENDENCY);
68-
if (moduleMatch) {
69-
var module = moduleMatch[1],
70-
path = moduleMatch[3];
18+
function toArray(arrayLike) {
19+
return Array.prototype.slice.call(arrayLike, 0);
20+
}
7121

72-
if (!path && !isInternal(module)) {
73-
// infer path from module name
74-
path = asFileDependency(module);
75-
}
7622

77-
// add module dependency
78-
modules.push(module);
23+
///////// main /////////
24+
25+
function internalModule(angular, name, dependencies, body) {
7926

80-
if (path) {
81-
// add path dependency if it exists
82-
files.push(path);
83-
}
84-
} else {
85-
files.push(d);
27+
if (!body) {
28+
body = dependencies;
29+
dependencies = null;
8630
}
87-
});
8831

89-
return { name: name, fileDependencies: files, moduleDependencies: modules };
90-
}
32+
var definition = ngParse.parseNgModule(name, dependencies || []);
9133

92-
function internalModule(angular, name, dependencies, body) {
34+
var module, exists;
9335

94-
if (!body) {
95-
body = dependencies;
96-
dependencies = null;
97-
}
36+
var moduleDependencies = definition.moduleDependencies,
37+
fileDependencies = definition.fileDependencies;
9838

99-
var definition = parseNgModule(name, dependencies || []);
39+
try {
40+
angular.module(name);
41+
exists = true;
42+
} catch (e) {
43+
exists = false;
44+
}
10045

101-
var module, exists;
46+
if (moduleDependencies.length && exists) {
47+
throw new Error(
48+
"Cannot re-define angular module " + name + " with new dependencies [" + moduleDependencies + "]. " +
49+
"Make sure the module is not defined else where or define a sub-module with additional angular module dependencies instead.");
50+
}
10251

103-
var moduleDependencies = definition.moduleDependencies,
104-
fileDependencies = definition.fileDependencies;
52+
if (moduleDependencies.length || !exists) {
53+
module = angular.module(name, moduleDependencies);
54+
debugLog(name, "defined with dependencies", moduleDependencies);
55+
} else {
56+
module = angular.module(name);
57+
debugLog(name, "looked up");
58+
}
10559

106-
try {
107-
angular.module(name);
108-
exists = true;
109-
} catch (e) {
110-
exists = false;
111-
}
60+
define(fileDependencies, function() {
61+
var results = toArray(arguments);
62+
results.unshift(module);
11263

113-
if (moduleDependencies.length && exists) {
114-
throw new Error(
115-
"Cannot re-define angular module " + name + " with new dependencies [" + moduleDependencies + "]. " +
116-
"Make sure the module is not defined else where or define a sub-module with additional angular module dependencies instead.");
117-
}
64+
body.apply(window, results);
11865

119-
if (moduleDependencies.length || !exists) {
120-
module = angular.module(name, moduleDependencies);
121-
debugLog(name, "defined with dependencies", moduleDependencies);
122-
} else {
123-
module = angular.module(name);
124-
debugLog(name, "looked up");
66+
debugLog(name, "loaded");
67+
return module;
68+
});
12569
}
12670

127-
define(fileDependencies, function() {
128-
var results = toArray(arguments);
129-
results.unshift(module);
130-
131-
body.apply(window, results);
132-
133-
debugLog(name, "loaded");
134-
return module;
135-
});
136-
}
137-
138-
/**
139-
* Angular module definition / lookup
140-
*
141-
* @param {string} name of the module
142-
* @param {string} (optional) dependencies of the module
143-
* @param {Function} body function defining the module
144-
*/
145-
function angularDefine(angular) {
146-
var fn = function(name, dependencies, body) {
71+
72+
//////// module exports /////////
73+
74+
var exports = function(name, dependencies, body) {
14775
if (!dependencies) {
14876
throw new Error("wrong number of arguments, expected name[, dependencies], body");
14977
}
15078
internalModule(angular, name, dependencies, body);
15179
};
15280

153-
fn.parseNgModule = parseNgModule;
81+
if (typeof window !== undefined && !window.ngDefine) {
82+
window.ngDefine = exports;
83+
}
15484

155-
return fn;
156-
}
15785

158-
var debugLog;
86+
///////// logging /////////
15987

160-
// for logging only
161-
(function() {
162-
var log;
88+
var debugLog = (function() {
89+
var log;
16390

164-
// IE 9 logging #!?.
165-
if (Function.prototype.bind && window.console && window.console.log) {
166-
log = Function.prototype.bind.call(window.console.log, window.console);
167-
}
168-
169-
debugLog = function() {
170-
if (!ngDefine.debug || !log) {
171-
return;
91+
// IE 9 logging #!?.
92+
if (Function.prototype.bind && window.console && window.console.log) {
93+
log = Function.prototype.bind.call(window.console.log, window.console);
17294
}
17395

174-
var args = toArray(arguments);
175-
args.unshift("[ngDefine]");
176-
177-
log.apply(log, args);
178-
};
179-
})();
96+
return function() {
97+
if (!exports.debug || !log) {
98+
return;
99+
}
180100

181-
define([ 'angular' ], function(angular) {
101+
var args = toArray(arguments);
102+
args.unshift("[ngDefine]");
182103

183-
ngDefine = ngDefine || angularDefine(angular);
104+
log.apply(log, args);
105+
};
106+
})();
184107

185-
if (!window.ngDefine) {
186-
window.ngDefine = ngDefine;
187-
}
188108

189-
// publish as requireJS module
190-
return ngDefine;
109+
///////// export //////////
110+
return exports;
191111
});
192-
193-
// publish statically in case we use the module outside
194-
// a requirejs context (e.g. during testing)
195-
if (window.angular) {
196-
window.ngDefine = ngDefine = (ngDefine || angularDefine(window.angular));
197-
}
198112
})(window);

src/ngParse.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* ngParse - utility for parsing ngDefine module definitions
3+
*
4+
* @version 1.1.0
5+
* @author Nico Rehwaldt <http://github.com/Nikku>
6+
*
7+
* @license (c) 2013 Nico Rehwaldt, MIT
8+
*/
9+
10+
define(function() {
11+
12+
var MODULE_DEPENDENCY = /^module:([^:]*)(:(.*))?$/;
13+
var INTERNAL = /^ng/;
14+
15+
function isFunction(value){ return typeof value == 'function'; }
16+
17+
function isInternal(module) {
18+
return INTERNAL.test(module);
19+
}
20+
21+
function asFileDependency(module) {
22+
return module.replace(/\./g, "/");
23+
}
24+
25+
function toArray(arrayLike) {
26+
return Array.prototype.slice.call(arrayLike, 0);
27+
}
28+
29+
/**
30+
* For each implementation as used by AngularJS
31+
*/
32+
function forEach(obj, iterator, context) {
33+
var key;
34+
if (obj) {
35+
if (isFunction(obj)){
36+
for (key in obj) {
37+
if (key != 'prototype' && key != 'length' && key != 'name' && obj.hasOwnProperty(key)) {
38+
iterator.call(context, obj[key], key);
39+
}
40+
}
41+
} else if (obj.forEach && obj.forEach !== forEach) {
42+
obj.forEach(iterator, context);
43+
} else if (isArrayLike(obj)) {
44+
for (key = 0; key < obj.length; key++)
45+
iterator.call(context, obj[key], key);
46+
} else {
47+
for (key in obj) {
48+
if (obj.hasOwnProperty(key)) {
49+
iterator.call(context, obj[key], key);
50+
}
51+
}
52+
}
53+
}
54+
return obj;
55+
}
56+
57+
function parseNgModule(name, dependencies) {
58+
59+
var files = [],
60+
modules = [];
61+
62+
forEach(dependencies, function(d) {
63+
var moduleMatch = d.match(MODULE_DEPENDENCY);
64+
if (moduleMatch) {
65+
var module = moduleMatch[1],
66+
path = moduleMatch[3];
67+
68+
if (!path && !isInternal(module)) {
69+
// infer path from module name
70+
path = asFileDependency(module);
71+
}
72+
73+
// add module dependency
74+
modules.push(module);
75+
76+
if (path) {
77+
// add path dependency if it exists
78+
files.push(path);
79+
}
80+
} else {
81+
files.push(d);
82+
}
83+
});
84+
85+
return { name: name, fileDependencies: files, moduleDependencies: modules };
86+
}
87+
88+
return { parseNgModule: parseNgModule };
89+
});

0 commit comments

Comments
 (0)