function serveFile(filepath, request, response) {
	var ext = path.extname(filepath);

	// this might be turn out to be a really bad idea. But let's try it and see
	if (ext === ".js" || ext === ".css") {
		return sander.readFile(filepath).then(function (data) {
			// this takes the auto-generated absolute sourcemap path, and turns
			// it into what you'd get with `gobble build` or `gobble watch`
			var sourcemapComment = sourcemap.getSourcemapComment(path.basename(filepath) + ".map", ext);
			data = data.toString().replace(sourcemap.SOURCEMAP_COMMENT, sourcemapComment);

			response.statusCode = 200;
			response.setHeader("Content-Type", mime.lookup(filepath));

			response.write(data);
			response.end();
		});
	}

	return sander.stat(filepath).then(function (stats) {
		response.statusCode = 200;
		response.setHeader("Content-Type", mime.lookup(filepath));
		response.setHeader("Content-Length", stats.size);

		sander.createReadStream(filepath).pipe(response);
	});
}
function tryPath ( base, filename, userModules ) {
	const absolutePath = path.resolve( base, filename );

	if ( hasOwnProp.call( userModules, absolutePath ) ) {
		return Promise.resolve( absolutePath );
	}
	return sander.stat( absolutePath ).then( () => absolutePath );
}
				return resolvedPromise.then( resolvedPath => {
					if ( !resolvedPath ) {
						// hack but whatevs, it saves handling real ENOENTs differently
						let err = new Error();
						err.code = 'ENOENT';
						throw err;
					}

					return sander.stat( resolvedPath ).then( () => path.resolve( base, resolvedPath ) );
				});
Example #4
0
module.exports = function handleRequest ( srcDir, error, request, response ) {
	var join = require( 'path' ).join,
		sander = require( 'sander' ),
		serveFile = require( './serveFile' ),
		serveDir = require( './serveDir' ),
		serveError = require( './serveError' ),
		message,
		parsedUrl = require( 'url' ).parse( request.url ),
		pathname = parsedUrl.pathname,
		filepath;

	if ( error ) {
		if ( pathname.substr( 0, 11 ) === '/__gobble__' ) {
			message = ( error.original && error.original.message ) || error.message || '';
			filepath = pathname.substring( 11 );

			// only allow links to files that we're actually interested in, not
			// the whole damn filesystem
			if ( ~message.indexOf( filepath ) || ~error.stack.indexOf( filepath ) ) {
				return serveFile( pathname.substring( 11 ), request, response );
			}
		}

		serveError( error, request, response );
		return sander.Promise.resolve();
	}

	filepath = join( srcDir, pathname );

	return sander.stat( filepath ).then( function ( stats ) {
		if ( stats.isDirectory() ) {
			// might need to redirect from `foo` to `foo/`
			if ( pathname.slice( -1 ) !== '/' ) {
				response.setHeader( 'Location', pathname + '/' + ( parsedUrl.search || '' ) );
				response.writeHead( 301 );

				response.end();
			} else {
				return serveDir( filepath, request, response );
			}
		}

		else {
			return serveFile( filepath, request, response );
		}
	}, function ( err ) {
		return serveError( err, request, response );
	});
};
function handleRequest(srcDir, error, sourcemapPromises, request, response) {
	var parsedUrl = url.parse(request.url);
	var pathname = parsedUrl.pathname;

	var filepath = undefined;

	if (error) {
		if (pathname.substr(0, 11) === "/__gobble__") {
			var message = error.original && error.original.message || error.message || "";
			filepath = pathname.substring(11);

			// only allow links to files that we're actually interested in, not
			// the whole damn filesystem
			if (~message.indexOf(filepath) || ~error.stack.indexOf(filepath)) {
				return serveFile['default'](pathname.substring(11), request, response);
			}
		}

		serveError['default'](error, request, response);
		return sander.Promise.resolve();
	}

	filepath = path.join(srcDir, pathname);

	if (path.extname(filepath) === ".map") {
		return serveSourcemap['default'](filepath, sourcemapPromises, request, response)["catch"](function (err) {
			return serveError['default'](err, request, response);
		});
	}

	return sander.stat(filepath).then(function (stats) {
		if (stats.isDirectory()) {
			// might need to redirect from `foo` to `foo/`
			if (pathname.slice(-1) !== "/") {
				response.setHeader("Location", pathname + "/" + (parsedUrl.search || ""));
				response.writeHead(301);

				response.end();
			} else {
				return serveDir['default'](filepath, request, response);
			}
		} else {
			return serveFile['default'](filepath, request, response);
		}
	}, function (err) {
		return serveError['default'](err, request, response);
	});
}
Example #6
0
}

else if ( process.argv.length <= 2 && process.stdin.isTTY ) {
	showHelp( process.stderr );
}

else if ( command.version ) {
	console.log( 'Sorcery version ' + require( '../package.json' ).version );
}

else if ( !command.input ) {
	console.error( 'Error: You must supply an --input (-i) argument. Type sorcery --help for more info' );
}

else {
	sander.stat( command.input ).then( function ( stats ) {
		if ( stats.isDirectory() ) {
			return sander.lsr( command.input ).then( function ( files ) {
				var promises = files.map( function ( file ) {
					var input = path.join( command.input, file );
					var output = path.join( command.output || command.input, file );

					return sorcery.load( input ).then( function ( chain ) {
						return chain.write( output, {
							inline: command.datauri,
							includeContent: !command.excludeContent
						});
					});
				});

				return sander.Promise.all( promises );
function convert ( options, method ) {
	if ( options.input ) {
		return sander.stat( options.input ).then( function ( stats ) {
			if ( stats.isDirectory() ) {
				if ( !options.output ) {
					handleError({ code: 'MISSING_OUTPUT_OPTION' });
				}

				// transpile all the things
				return sander.lsr( options.input )
					.then( filterOutNonJs )
					.then( function ( files ) {
						var promises = files.map( function ( file ) {
							var input = path.join( options.input, file );
							var output = path.join( options.output, file );

							var fileOptions = assign( {}, options, {
								input: input,
								output: output,
								sourceMapSource: input,
								sourceMapFile: output
							});

							return convert( fileOptions, method );
						});

						return Promise.all( promises );
					});
			}

			return sander.readFile( options.input )
				.then( String )
				.then( run );
		});
	} else {
		return readFromStdin().then( run );
	}

	function run ( source ) {
		var transpiled, promises;

		transpiled = esperanto[ method ]( source, {
			strict: options.strict,
			name: options.name,
			amdName: options.amdName,
			sourceMap: options.sourcemap,
			sourceMapSource: options.input,
			sourceMapFile: options.output
		});

		if ( options.output ) {
			promises = [ sander.writeFile( options.output, transpiled.code ) ];

			if ( options.sourcemap === true ) {
				promises.push( sander.writeFile( options.output + '.map', transpiled.map ) );
			}

			return Promise.all( promises );
		}

		process.stdout.write( transpiled.code );
	}
}