Skip to content

Commit f812cca

Browse files
committed
refactor(command): construct full options earlier
1 parent 4ac1103 commit f812cca

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

bin/command.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,25 @@ program
104104
"for that browser, on a matching OS."
105105
)
106106
.option( "--run-id <id>", "A unique identifier for the run in BrowserStack." )
107-
.action( async( { configFile, ...options } ) => {
107+
.action( async( { configFile, ...argv } ) => {
108108
const config = await readYAML( configFile );
109-
const flag = [
109+
const options = {
110+
baseUrl: "/test/",
111+
...config,
112+
testUrl: config.testUrls,
113+
...argv
114+
};
115+
options.flag = [
110116
...parseFlags( config.flags ),
111-
...( config.flag ?? [] ),
112117
...( options.flag ?? [] )
113118
];
114-
const run = [
119+
options.run = [
115120
...parseRuns( config.runs ),
116-
...( config.run ?? [] ),
117121
...( options.run ?? [] )
118122
];
119-
const middleware = await parseMiddleware( config, options );
123+
options.middleware = await parseMiddleware( options );
120124

121-
return runTests( {
122-
...config,
123-
testUrl: config.testUrls,
124-
...options,
125-
flag,
126-
middleware,
127-
run
128-
} );
125+
return runTests( options );
129126
} );
130127

131128
// Define the serve command
@@ -156,22 +153,26 @@ program
156153
"passing the path to a module that exports a middleware factory function. " +
157154
"Pass multiple by repeating the option."
158155
)
159-
.action( async( { configFile, quiet, ...options } ) => {
156+
.action( async( { configFile, ...argv } ) => {
160157
console.log( "Starting server..." );
161158
const config = await readYAML( configFile );
162-
const middleware = await parseMiddleware( config, options );
163-
const baseUrl = config.baseUrl ?? options.baseUrl ?? "/test/";
159+
const options = {
160+
baseUrl: "/test/",
161+
port: DEFAULT_PORT,
162+
...config,
163+
...argv
164+
};
165+
options.middleware = await parseMiddleware( options );
164166

165167
/**
166168
* Run a simple server for loading tests in a browser.
167169
* Note: this server does not support middleware.
168170
* To add middleware, use createTestServer directly.
169171
*/
170-
const app = await createTestServer( { baseUrl, middleware, quiet } );
172+
const app = await createTestServer( options );
171173

172-
const port = options.port ?? config.port ?? DEFAULT_PORT;
173-
return app.listen( { port, host: "0.0.0.0" }, function() {
174-
console.log( `Open tests at http://localhost:${ port }${ baseUrl }` );
174+
return app.listen( { port: options.port, host: "0.0.0.0" }, function() {
175+
console.log( `Open tests at http://localhost:${ options.port }${ options.baseUrl }` );
175176
} );
176177
} );
177178

@@ -249,13 +250,15 @@ function parseRuns( runs ) {
249250
return results;
250251
}
251252

252-
async function parseMiddleware( config, options ) {
253+
async function parseMiddleware( options ) {
253254
const middleware = await Promise.all(
254-
[ ...( config.middleware ?? [] ), ...( options.middleware ?? [] ) ].map(
255+
( options.middleware ?? [] ).map(
255256
async( mw ) => {
256-
const module = await import(
257-
pathToFileURL( resolve( process.cwd(), mw ) ).toString()
258-
);
257+
const filepath = pathToFileURL( resolve( process.cwd(), mw ) ).toString();
258+
if ( options.verbose ) {
259+
console.log( `Loading middleware from ${ filepath }...` );
260+
}
261+
const module = await import( filepath );
259262
return module.default;
260263
}
261264
)

0 commit comments

Comments
 (0)