Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 492688 | Differences between
and this patch

Collapse All | Expand All

(-)a/bundles/org.eclipse.orion.client.javascript/web/javascript/plugins/javascriptPlugin.js (-31 / +79 lines)
Lines 248-254 define([ Link Here
248
			response.args.logical = _l;
248
			response.args.logical = _l;
249
			if(request.args.file.env === 'node') {
249
			if(request.args.file.env === 'node') {
250
				if (!/^[\.]+/.test(_l)) {
250
				if (!/^[\.]+/.test(_l)) {
251
					_nodeRead(response, _l, fileClient);
251
					_nodeRead(response, _l, request.args.file.file, fileClient, null, 0, true);
252
				} else {
252
				} else {
253
					_readRelative(request, response, _l, fileClient);
253
					_readRelative(request, response, _l, fileClient);
254
				}
254
				}
Lines 331-336 define([ Link Here
331
	 * @since 12.0
378
	 * @since 12.0
332
	 */
379
	 */
333
	function _failedRead(response, fileName, err) {
380
	function _failedRead(response, fileName, err) {
381
		console.log(fileName);
334
		response.args.message = err.toString();
382
		response.args.message = err.toString();
335
		response.args.error = i18nUtil.formatMessage(javascriptMessages['failedToReadFile'], fileName);
383
		response.args.error = i18nUtil.formatMessage(javascriptMessages['failedToReadFile'], fileName);
336
		ternWorker.postMessage(response);
384
		ternWorker.postMessage(response);
Lines 274-313 define([ Link Here
274
	/**
274
	/**
275
	 * @since 12.0
275
	 * @since 12.0
276
	 */
276
	 */
277
	function _nodeRead(response, filePath, fileclient) {
277
	function _nodeRead(response, moduleName, filePath, fileclient, error, depth, subModules) {
278
		var project = jsProject.getProjectPath();
278
		if (depth > 2) {
279
		if(project) {
279
			console.log("Don't read '" + moduleName + "': too deep");
280
			return fileclient.read(project+"node_modules/"+filePath+"/package.json", false, false, {readIfExists: true}).then(function(json) {
280
			return _failedRead(response, moduleName, "Too deep");
281
				if(json) {
281
		}
282
					var val = JSON.parse(json);
282
		var index = filePath.lastIndexOf('/', filePath.length - 2);
283
					var mainPath = null;
283
		if (index === -1) {
284
					var main = val.main;
284
			return _failedRead(response, moduleName, error === null ? 'Could not read module ' + moduleName : error);
285
					if (main) {
285
		}
286
						if (!/(\.js)$/.test(main)) {
286
		var parentFolder = filePath.substr(0, index + 1); // include the trailing / in the folder path
287
							main += ".js";
287
		if (parentFolder === filePath) {
288
						}
288
			console.log("Infinite loop reading '" + filePath);
289
						mainPath = project + "node_modules/" + filePath + "/" + main;
289
			return _failedRead(response, moduleName, "Infinite loop");
290
					} else {
290
		}
291
						main = "index.js";
291
		var modulePath = parentFolder + "node_modules/" + moduleName;
292
						mainPath = project + "node_modules/" + filePath + "/index.js";
292
		if (moduleName.indexOf('/') !== -1 && subModules) {
293
					}
293
			// module name contains /
294
					return fileclient.read(mainPath).then(function(contents) {
294
			var fileToLoad = modulePath;
295
						response.args.contents = contents;
295
			if (!/(\.js)$/.test(modulePath)) {
296
						response.args.file = mainPath;
296
				fileToLoad += ".js";
297
						response.args.path = main;
297
			}/*
298
						ternWorker.postMessage(response);
298
			return fileclient.read(fileToLoad, false, false, {readIfExists: true}).then(function(contents) {
299
					},
299
				if (contents) {
300
					function(err) {
300
					response.args.contents = contents;
301
						_failedRead(response, "node_modules", err);
301
					response.args.file = fileToLoad;
302
					});
302
					response.args.path = fileToLoad;
303
					console.log(fileToLoad);
304
					ternWorker.postMessage(response);
305
				} else {
306
					_nodeRead(response, moduleName, filePath, fileclient, "No contents", depth, false);
303
				}
307
				}
304
				_failedRead(response, filePath, "No contents");
305
			},
308
			},
306
			function(err) {
309
			function(err) {
307
				_failedRead(response, filePath, err);
310
				_nodeRead(response, moduleName, filePath, fileclient, err, depth, false);
311
			});*/
312
			if (!/(\.js)$/.test(modulePath)) {
313
				fileToLoad += ".js";
314
			}
315
			return fileclient.read(filePath).then(function(contents) {
316
				if (contents) {
317
					response.args.contents = contents;
318
					ternWorker.postMessage(response);
319
				} else {
320
					_nodeRead(response, moduleName, filePath, fileclient, "No contents", depth, false);
321
				}
322
			},
323
			function(err) {
324
				_nodeRead(response, moduleName, filePath, fileclient, err, depth, false);
308
			});
325
			});
309
		} 
326
		}
310
		_failedRead(response, filePath, "No project context");
327
		return fileclient.read(modulePath+"/package.json", false, false, {readIfExists: true}).then(function(json) {
328
			if(json) {
329
				var val = JSON.parse(json);
330
				var mainPath = null;
331
				var main = val.main;
332
				if (main) {
333
					if (!/(\.js)$/.test(main)) {
334
						main += ".js";
335
					}
336
				} else {
337
					main = "index.js";
338
				}
339
				mainPath = modulePath + "/" + main;
340
				return fileclient.read(mainPath).then(function(contents) {
341
					response.args.contents = contents;
342
					response.args.file = mainPath;
343
					response.args.path = main;
344
					console.log(mainPath);
345
					ternWorker.postMessage(response);
346
				},
347
				function(err) {
348
					console.log("Fail to read " + mainPath);
349
					_failedRead(response, "node_modules", err);
350
				});
351
			}
352
			_nodeRead(response, moduleName, parentFolder, fileclient, "No contents", depth + 1, true);
353
		},
354
		function(err) {
355
			// if it fails, try to parent folder
356
			_nodeRead(response, moduleName, parentFolder, fileclient, err, depth + 1, true);
357
		});
311
	}
358
	}
312
	/**
359
	/**
313
	 * @since 12.0
360
	 * @since 12.0

Return to bug 492688