Example #1
0
 return function(path) {
     var i, dir, paths = [],
         fileGuesses = [],
         file,
         module = {
             exports: {}
         };
     if (phantomBuiltins.indexOf(path) !== -1) {
         return phantomRequire(path);
     }
     if (path[0] === '.') {
         paths.push.apply(paths, [
             fs.absolute(path),
             fs.absolute(fs.pathJoin(requireDir, path))
         ]);
     } else if (path[0] === '/') {
         paths.push(path);
     } else {
         dir = fs.absolute(requireDir);
         while (dir !== '' && dir.lastIndexOf(':') !== dir.length - 1) {
             // nodejs compatibility
             paths.push(fs.pathJoin(dir, 'node_modules', path));
             dir = fs.dirname(dir);
         }
         paths.push(fs.pathJoin(requireDir, 'lib', path));
         paths.push(fs.pathJoin(requireDir, 'modules', path));
     }
     paths.forEach(function(testPath) {
         fileGuesses.push.apply(fileGuesses, [
             testPath,
             testPath + '.js',
             testPath + '.coffee',
             fs.pathJoin(testPath, 'index.js'),
             fs.pathJoin(testPath, 'index.coffee'),
             fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'),
             fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee')
         ]);
     });
     file = null;
     for (i = 0; i < fileGuesses.length && !file; ++i) {
         if (fs.isFile(fileGuesses[i])) {
             file = fileGuesses[i];
         }
     }
     if (!file) {
         throw new Error("CasperJS couldn't find module " + path);
     }
     if (file in requireCache) {
         return requireCache[file].exports;
     }
     try {
         var scriptCode = phantom.getScriptCode(file);
         new Function('module', 'exports', scriptCode)(module, module.exports);
     } catch (e) {
         phantom.processScriptError(e, file);
     }
     requireCache[file] = module;
     return module.exports;
 };
Example #2
0
 tests.forEach(function(test) {
     var testDir = fs.absolute(fs.dirname(test));
     if (fs.isDirectory(testDir)) {
         if (fs.exists(fs.pathJoin(testDir, '.casper'))) {
             isCasperTest = true;
         }
     }
 });
casper.test.begin('fs.dirname() tests', 8, function(test) {
    var tests = {
        '/local/plop/foo.js':      '/local/plop',
        'local/plop/foo.js':       'local/plop',
        './local/plop/foo.js':     './local/plop',
        'c:\\local\\plop\\foo.js': 'c:/local/plop',
        'D:\\local\\plop\\foo.js': 'D:/local/plop',
        'D:\\local\\plop\\':       'D:/local/plop',
        'c:\\':                    'c:',
        'c:':                      'c:'
    };
    for (var testCase in tests) {
        test.assertEquals(fs.dirname(testCase), tests[testCase], 'fs.dirname() does its job for ' + testCase);
    }
    test.done();
});
Example #4
0
 function possiblePaths(path, requireDir) {
     var dir, paths = [];
     if (path[0] === '.') {
         paths.push.apply(paths, [
             fs.absolute(path),
             fs.absolute(fs.pathJoin(requireDir, path))
         ]);
     } else if (path[0] === '/') {
         paths.push(path);
     } else {
         dir = fs.absolute(requireDir);
         while (dir !== '' && dir.lastIndexOf(':') !== dir.length - 1) {
             paths.push(fs.pathJoin(dir, 'modules', path));
             // nodejs compatibility
             paths.push(fs.pathJoin(dir, 'node_modules', path));
             dir = fs.dirname(dir);
         }
         paths.push(fs.pathJoin(requireDir, 'lib', path));
         paths.push(fs.pathJoin(requireDir, 'modules', path));
     }
     return paths;
 }
Example #5
0
File: script.js Project: wesdu/Gin
		fileArray.map(function(file) {
			console.info(filePath, file);
			var absolutePath= path.resolve(path.dirname(filePath), file);
			result += fs.readFileSync(absolutePath).toString("utf8");
		});
Example #6
0
var phantomas = function(params) {
	var fs = require('fs');

	// store script CLI parameters
	this.params = params;

	// --url=http://example.com
	this.url = params.url;

	// --format
	this.format = params.format;

	// --verbose
	this.verboseMode = params.verbose === true;

	// --silent
	this.silentMode = params.silent === true;

	// --timeout (in seconds)
	this.timeout = (params.timeout > 0 && parseInt(params.timeout, 10)) || 15;

	// --modules=localStorage,cookies
	this.modules = (typeof params.modules === 'string') ? params.modules.split(',') : [];

	// --include-dirs=dirOne,dirTwo
	this.includeDirs = (typeof params['include-dirs'] === 'string') ? params['include-dirs'].split(',') : [];

	// --skip-modules=jQuery,domQueries
	this.skipModules = (typeof params['skip-modules'] === 'string') ? params['skip-modules'].split(',') : [];

	// disable JavaScript on the page that will be loaded
	this.disableJs = params['disable-js'] === true;

	// setup the stuff
	this.emitter = new(this.require('events').EventEmitter)();
	this.emitter.setMaxListeners(200);
	this.ipc = require('./ipc');

	this.util = this.require('util');

	this.page = require('webpage').create();

	// store the timestamp of responseEnd event
	// should be bound before modules
	this.on('responseEnd', this.proxy(function() {
		this.responseEndTime = Date.now();
	}));

	// setup logger
	var Logger = require('./logger'),
		logFile = params.log || '';

	this.logger = new Logger(logFile, {
		beVerbose: this.verboseMode,
		beSilent: this.silentMode
	});

	// detect phantomas working directory
	if (typeof module.dirname !== 'undefined') {
		this.dir = module.dirname.replace(/core$/, '');
	} else if (typeof slimer !== 'undefined') {
		var args = require('system').args;
		this.dir = fs.dirname(args[0]).replace(/scripts$/, '');
	}

	this.log('phantomas v%s: %s', this.getVersion(), this.dir);
	this.log('Options: %j', this.params);

	// queue of jobs that needs to be done before report can be generated
	var Queue = require('../lib/simple-queue');
	this.reportQueue = new Queue();

	// set up results wrapper
	var Results = require('./results');
	this.results = new Results();

	this.results.setGenerator('phantomas v' + this.getVersion());
	this.results.setUrl(this.url);

	// allow asserts to be provided via command-line options (#128)
	Object.keys(this.params).forEach(function(param) {
		var value = parseFloat(this.params[param]),
			name;

		if (!isNaN(value) && param.indexOf('assert-') === 0) {
			name = param.substr(7);

			if (name.length > 0) {
				this.results.setAssert(name, value);
			}
		}
	}, this);

	// load core modules
	this.log('Loading: core modules...');
	this.addCoreModule('navigationTiming');
	this.addCoreModule('requestsMonitor');
	this.addCoreModule('timeToFirstByte');

	// load extensions
	this.log('Loading: extensions...');
	var extensions = this.listExtensions();
	extensions.forEach(this.addExtension, this);

	// load modules
	this.log('Loading: modules...');
	var modules = (this.modules.length > 0) ? this.modules : this.listModules();
	modules.forEach(this.addModule, this);

	// load 3rd party modules
	this.log('Loading: 3rd party modules...');
	this.includeDirs.forEach(function(dirName) {
		var dirPath = fs.absolute(dirName),
			dirModules = this.listModulesInDir(dirPath);

		dirModules.forEach(function(moduleName) {
			this.addModuleInDir(dirPath, moduleName);
		}, this);

	}, this);
};