Skip to content

Commit 1076b0a

Browse files
authored
Merge pull request #46 from LambdaTest/stage
Release v2.0.6
2 parents 2d92bdc + fa0c16a commit 1076b0a

File tree

16 files changed

+88
-41
lines changed

16 files changed

+88
-41
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/smartui-cli",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
55
"files": [
66
"dist/**/*"

src/commander/exec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ command
2020
let ctx: Context = ctxInit(command.optsWithGlobals());
2121

2222
if (!which.sync(execCommand[0], { nothrow: true })) {
23-
console.log(`Error: Command not found "${execCommand[0]}"`);
23+
ctx.log.error(`Error: Command not found "${execCommand[0]}"`);
2424
return
2525
}
2626
ctx.args.execCommand = execCommand
@@ -50,7 +50,7 @@ command
5050
try {
5151
await tasks.run(ctx);
5252
} catch (error) {
53-
console.log('\nRefer docs: https://www.lambdatest.com/support/docs/smart-visual-regression-testing/')
53+
ctx.log.info('\nRefer docs: https://www.lambdatest.com/support/docs/smart-visual-regression-testing/');
5454
} finally {
5555
await ctx.server?.close();
5656
await ctx.browser?.close();

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
#!/usr/bin/env node
22

33
import commander from './commander/commander.js'
4+
import getEnv from './lib/env.js'
5+
import httpClient from './lib/httpClient.js'
6+
import logger from './lib/logger.js'
7+
import chalk from 'chalk'
8+
import pkgJSON from './../package.json'
49

5-
commander.parse();
10+
(async function() {
11+
let client = new httpClient(getEnv());
12+
let log = logger;
13+
14+
try {
15+
log.info(`\nLambdaTest SmartUI CLI v${pkgJSON.version}`);
16+
let { data: { latestVersion, deprecated } } = await client.checkUpdate(log);
17+
if (deprecated) log.warn(`This version is deprecated. A new version ${latestVersion} is available!\n`);
18+
else if (pkgJSON.version !== latestVersion) log.info(chalk.gray(`A new version ${latestVersion} is available!\n`));
19+
else log.info(chalk.gray('https://www.npmjs.com/package/@lambdatest/smartui-cli\n'));
20+
} catch (error) {
21+
log.debug(error);
22+
log.info(chalk.gray('https://www.npmjs.com/package/@lambdatest/smartui-cli\n'));
23+
}
24+
25+
commander.parse();
26+
})();

src/lib/ctx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Context, Env, Config } from '../types.js'
22
import { DEFAULT_CONFIG } from './config.js'
33
import { version } from '../../package.json'
44
import { validateConfig } from './schemaValidation.js'
5-
import logger from '../lib/logger.js'
6-
import getEnv from '../lib/env.js'
5+
import logger from './logger.js'
6+
import getEnv from './env.js'
77
import httpClient from './httpClient.js'
88
import fs from 'fs'
99

src/lib/env.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ export default (): Env => {
44
const {
55
PROJECT_TOKEN = '',
66
SMARTUI_CLIENT_API_URL = 'https://api.lambdatest.com/visualui/1.0',
7-
SMARTUI_LOG_LEVEL,
8-
SMARTUI_DEBUG
7+
LT_SDK_LOG_LEVEL,
8+
LT_SDK_DEBUG
99
} = process.env
1010

1111
return {
1212
PROJECT_TOKEN,
1313
SMARTUI_CLIENT_API_URL,
14-
SMARTUI_LOG_LEVEL,
15-
SMARTUI_DEBUG
14+
LT_SDK_LOG_LEVEL,
15+
LT_SDK_DEBUG
1616
}
1717
}

src/lib/httpClient.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
44
import { Env, ProcessedSnapshot, Git, Build } from '../types.js';
55
import { delDir } from './utils.js';
66
import type { Logger } from 'winston'
7+
import pkgJSON from './../../package.json'
78

89
export default class httpClient {
910
axiosInstance: AxiosInstance;
@@ -29,11 +30,18 @@ export default class httpClient {
2930
})
3031
.catch(error => {
3132
if (error.response) {
33+
log.debug(`http response: ${JSON.stringify({
34+
status: error.response.status,
35+
headers: error.response.headers,
36+
body: error.response.data
37+
})}`);
3238
throw new Error(JSON.stringify(error.response.data));
3339
}
3440
if (error.request) {
41+
log.debug(`http request failed: ${error.toJSON()}`);
3542
throw new Error(error.toJSON().message);
3643
}
44+
log.debug(`http request failed: ${error.message}`);
3745
throw new Error(error.message);
3846
})
3947
}
@@ -128,4 +136,16 @@ export default class httpClient {
128136
throw new Error(error.message);
129137
})
130138
}
139+
140+
checkUpdate(log: Logger) {
141+
return this.request({
142+
url: `/packageinfo`,
143+
method: 'GET',
144+
headers: { 'Content-Type': 'application/json' },
145+
params: {
146+
packageName: pkgJSON.name,
147+
packageVersion: pkgJSON.version
148+
}
149+
}, log)
150+
}
131151
}

src/lib/logger.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ interface LogContext {
77
task?: string;
88
}
99

10-
let logContext: LogContext = {};
10+
let logContext: LogContext = { task: 'smartui-cli' };
1111

1212
// Function to update context
1313
export function updateLogContext(newContext: LogContext) {
14-
logContext = { ...logContext, ...newContext };
14+
logContext = { ...logContext, ...newContext };
1515
}
1616

1717
const logLevel = (): string => {
1818
let env: Env = getEnv();
19-
let debug: any = (env.SMARTUI_DEBUG === 'true') ? 'debug' : undefined;
20-
return debug || env.SMARTUI_LOG_LEVEL || 'info'
19+
let debug: any = (env.LT_SDK_DEBUG === 'true') ? 'debug' : undefined;
20+
return debug || env.LT_SDK_LOG_LEVEL || 'info'
2121
}
2222

2323
// Create a Winston logger
@@ -26,17 +26,23 @@ const logger = createLogger({
2626
format: format.combine(
2727
format.timestamp(),
2828
format.printf(info => {
29-
let contextString: string;
30-
if (logContext && Object.keys(logContext).length) {
31-
contextString = Object.values(logContext).join(' | ');
32-
}
33-
let message = `[${contextString}:${info.level}] `;
34-
message += (info.message === 'object') ? JSON.stringify(info.message) : info.message;
35-
return message;
29+
let contextString = Object.values(logContext).join(' | ');
30+
let message = (typeof info.message === 'object') ? JSON.stringify(info.message) : info.message;
31+
switch (info.level) {
32+
case 'debug':
33+
message = chalk.blue(message);
34+
break;
35+
case 'warn':
36+
message = chalk.yellow(message);
37+
break;
38+
case 'error':
39+
message = chalk.red(message);
40+
break;
41+
}
42+
return (info.level === 'info') ? message : `[${contextString}:${info.level}] ` + message;
3643
})
3744
),
38-
transports: [new transports.File({ filename: 'smartui.log' })]
45+
transports: [new transports.Console()]
3946
});
4047

4148
export default logger
42-

src/lib/processSnapshot.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const MIN_VIEWPORT_HEIGHT = 1080;
66
export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string, any>> => {
77
// Process snapshot options
88
let options = snapshot.options;
9-
let warnings: Array<string> = [];
9+
let optionWarnings: Set<string> = new Set();
1010
let processedOptions: Record<string, any> = {};
1111
if (options && Object.keys(options).length !== 0) {
1212
ctx.log.debug(`Processing options: ${JSON.stringify(options)}`);
@@ -56,7 +56,7 @@ export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string,
5656
for (const selector of selectors) {
5757
let l = await page.locator(selector).all()
5858
if (l.length === 0) {
59-
warnings.push(`For snapshot ${snapshot.name}, no element found for selector ${selector}`);
59+
optionWarnings.add(`For snapshot ${snapshot.name}, no element found for selector ${selector}`);
6060
continue;
6161
}
6262
locators.push(...l);
@@ -77,14 +77,13 @@ export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string,
7777
}
7878
}
7979

80-
warnings.push(...snapshot.dom.warnings);
8180
return {
8281
processedSnapshot: {
8382
name: snapshot.name,
8483
url: snapshot.url,
8584
dom: Buffer.from(snapshot.dom.html).toString('base64'),
8685
options: processedOptions
8786
},
88-
warnings
87+
warnings: [...optionWarnings, ...snapshot.dom.warnings]
8988
}
9089
}

src/lib/schemaValidation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const SnapshotSchema: JSONSchemaType<Snapshot> = {
186186
additionalProperties: false
187187
}
188188
},
189-
required: ["name", "url", "dom", "options"],
189+
required: ["name", "url", "dom"],
190190
additionalProperties: false,
191191
errorMessage: "Invalid snapshot"
192192
}

src/lib/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
3838
});
3939

4040

41-
await server.listen({ port: 8080 })
41+
await server.listen();
4242
// store server's address for SDK
43-
let { port } = server.addresses()[0]
44-
process.env.SMARTUI_SERVER_ADDRESS = `http://localhost:${port}`
43+
let { port } = server.addresses()[0];
44+
process.env.SMARTUI_SERVER_ADDRESS = `http://localhost:${port}`;
4545

4646
return server;
4747
}

0 commit comments

Comments
 (0)