Skip to content

Commit a459c9b

Browse files
authored
Merge pull request #1 from sushobhit-lt/stage
add web and static config commands
2 parents 05c35e6 + 24f2e78 commit a459c9b

File tree

5 files changed

+100
-9
lines changed

5 files changed

+100
-9
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,17 @@ npm run capture
2727
```
2828
npm run capture-with-deafult-config
2929
```
30+
31+
### To Create Web config for multiple browser and viewports
32+
```
33+
npm run config:create-web
34+
or
35+
npm run config:create-web smartui-web.json
36+
```
37+
38+
### To Create Static config for multiple URLs
39+
```
40+
npm run config:web-static
41+
or
42+
npm run config:web-static urls.json
43+
```

packages/cli/src/config.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import fs from 'fs';
2+
import { ABNORMAL_EXIT } from './constant.js';
3+
import path from 'path';
4+
15
export const defaultScreenshotConfig = [
26
{
37
"name": "lambdatest-home-page",
4-
"url": "https://www.lambdatest.com"
8+
"url": "https://www.lambdatest.com",
9+
"waitForTimeout": 1000
510
},
611
{
712
"name": "example-page",
@@ -18,9 +23,59 @@ export const defaultSmartUIWebConfig = {
1823
'edge'
1924
],
2025
resolutions: [
21-
[1920, 1080]
26+
[1920, 1080],
27+
[1366, 768],
28+
[360, 640],
2229
],
2330
include: [],
2431
exclude: []
2532
}
2633
};
34+
35+
export function createWebConfig(filepath, log) {
36+
// default filepath
37+
filepath = filepath || 'smartui-web.json';
38+
let filetype = path.extname(filepath);
39+
if (filetype != '.json') {
40+
log.error(`Error: Web Config file must have .json extension`);
41+
process.exitCode = ABNORMAL_EXIT;
42+
return
43+
}
44+
45+
// verify the file does not already exist
46+
if (fs.existsSync(filepath)) {
47+
log.error(`Error: SmartUI Web Config already exists: ${filepath}`);
48+
log.error(`To create a new file, please specify the file name like: 'smartui config:create-web webConfig.json'`);
49+
process.exitCode = ABNORMAL_EXIT;
50+
return
51+
}
52+
53+
// write stringified default config options to the filepath
54+
fs.mkdirSync(path.dirname(filepath), { recursive: true });
55+
fs.writeFileSync(filepath, JSON.stringify(defaultSmartUIWebConfig, null, 2) + '\n');
56+
log.info(`Created SmartUI Web Config: ${filepath}`);
57+
};
58+
59+
export function createWebStaticConfig(filepath, log) {
60+
// default filepath
61+
filepath = filepath || 'url.json';
62+
let filetype = path.extname(filepath);
63+
if (filetype != '.json') {
64+
log.error(`Error: Config file must have .json extension`);
65+
process.exitCode = ABNORMAL_EXIT;
66+
return
67+
}
68+
69+
// verify the file does not already exist
70+
if (fs.existsSync(filepath)) {
71+
log.error(`Error: web-static config already exists: ${filepath}`);
72+
log.error(`To create a new file, please specify the file name like: 'smartui config:create-web links.json'`);
73+
process.exitCode = ABNORMAL_EXIT;
74+
return
75+
}
76+
77+
// write stringified default config options to the filepath
78+
fs.mkdirSync(path.dirname(filepath), { recursive: true });
79+
fs.writeFileSync(filepath, JSON.stringify(defaultScreenshotConfig, null, 2) + '\n');
80+
log.info(`Created web-static config: ${filepath}`);
81+
};

packages/cli/src/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { Command,Option } from 'commander';
2-
// import { version } from '../package.json';
1+
import { Command, Option } from 'commander';
32
import { capture } from './capture.js';
43
import { logger } from '@smartui/logger';
54
import { validateScreenshotConfig } from './validate.js';
65
import packageJson from '../package.json' assert { type: 'json' };
6+
import { createWebConfig, createWebStaticConfig } from './config.js';
77

88
const program = new Command();
99

1010
program
11-
.name('smartui')
12-
.description('CLI to help you to take screenshot SmartUI on LambdaTest platform')
13-
.addOption(new Option('--env <prod|stage>', 'Runtime environment option').choices(['prod', 'stage']));
11+
.name('smartui')
12+
.description('CLI to help you to take screenshot SmartUI on LambdaTest platform')
13+
.addOption(new Option('--env <prod|stage>', 'Runtime environment option').choices(['prod', 'stage']));
1414

1515
program
1616
.command('capture <file>')
@@ -26,4 +26,24 @@ program
2626
capture(file, options, log);
2727
});
2828

29+
program.command('config:create-web')
30+
.description('Create SmartUI Web config file')
31+
.argument('[filepath]', 'Optional config filepath')
32+
.action(async function (filepath, options) {
33+
const log = await logger();
34+
log.info('SmartUI Config CLI v' + packageJson.version);
35+
log.info('\n');
36+
createWebConfig(filepath, log);
37+
});
38+
39+
program.command('config:web-static')
40+
.description('Create Web Static config file')
41+
.argument('[filepath]', 'Optional config filepath')
42+
.action(async function (filepath, options) {
43+
const log = await logger();
44+
log.info('SmartUI Config CLI v' + packageJson.version);
45+
log.info('\n');
46+
createWebStaticConfig(filepath, log);
47+
});
48+
2949
program.parse();

packages/cli/src/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function validateScreenshotConfig(configFile, options, log) {
6666
// Parse JSON
6767
let webConfig;
6868
try {
69-
webConfig = JSON.parse(fs.readFileSync(webConfigFile)).web;
69+
webConfig = JSON.parse(fs.readFileSync(webConfigFile));
7070
} catch (error) {
7171
log.error('Error: ', error.message);
7272
process.exit(ABNORMAL_EXIT);

packages/core/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"capture": "NODE_NO_WARNINGS=1 node packages/cli/src/index.js capture url.json --config smartui-web.json --env stage",
10-
"capture-with-deafult-config": "NODE_NO_WARNINGS=1 node packages/cli/src/index.js capture url.json --env stage"
10+
"capture-with-deafult-config": "NODE_NO_WARNINGS=1 node packages/cli/src/index.js capture url.json --env stage",
11+
"config:create-web": "NODE_NO_WARNINGS=1 node packages/cli/src/index.js config:create-web",
12+
"config:web-static": "NODE_NO_WARNINGS=1 node packages/cli/src/index.js config:web-static"
1113
},
1214
"author": "",
1315
"license": "ISC",

0 commit comments

Comments
 (0)