Skip to content

Commit d8c7da6

Browse files
authored
Revert "fix validation errors and added logger"
1 parent 68d037f commit d8c7da6

File tree

9 files changed

+76
-173
lines changed

9 files changed

+76
-173
lines changed

commands/capture.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const { fetchDOM, upload } = require('./utils/dom')
22
const { cleanup } = require('./utils/cleanup')
33

44

5-
async function capture(screenshots, options, logger) {
6-
await fetchDOM(screenshots,options, logger);
7-
await upload(screenshots, options, logger);
8-
cleanup(logger);
5+
async function capture(screenshots, options) {
6+
await fetchDOM(screenshots,options);
7+
await upload(screenshots, options);
8+
cleanup();
99
}
1010

1111

commands/config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,50 @@ const fs = require('fs');
33
const { defaultSmartUIConfig, defaultScreenshotConfig } = require('./utils/config');
44
var { constants } = require('./utils/constants');
55

6-
function createConfig(filepath, logger) {
6+
function createConfig(filepath) {
77
// default filepath
88
filepath = filepath || '.smartui.json';
99
let filetype = path.extname(filepath);
1010
if (filetype != '.json') {
11-
logger.error(`[smartui] Error: Config file must have .json extension`);
11+
console.log(`[smartui] Error: Config file must have .json extension`);
1212
process.exitCode = constants.ERROR_CATCHALL;
1313
return
1414
}
1515

1616
// verify the file does not already exist
1717
if (fs.existsSync(filepath)) {
18-
logger.error(`[smartui] Error: LambdaTest SmartUI config already exists: ${filepath}`);
18+
console.log(`[smartui] Error: LambdaTest SmartUI config already exists: ${filepath}`);
1919
process.exitCode = constants.ERROR_CATCHALL;
2020
return
2121
}
2222

2323
// write stringified default config options to the filepath
2424
fs.mkdirSync(path.dirname(filepath), { recursive: true });
2525
fs.writeFileSync(filepath, JSON.stringify(defaultSmartUIConfig, null, 2) + '\n');
26-
logger.info(`[smartui] Created LambdaTest SmartUI config: ${filepath}`);
26+
console.log(`[smartui] Created LambdaTest SmartUI config: ${filepath}`);
2727
};
2828

29-
function createScreenshotConfig(filepath, logger) {
29+
function createScreenshotConfig(filepath) {
3030
// default filepath
3131
filepath = filepath || 'screenshot.json';
3232
let filetype = path.extname(filepath);
3333
if (filetype != '.json') {
34-
logger.error(`[smartui] Error: Config file must have .json extension`);
34+
console.log(`[smartui] Error: Config file must have .json extension`);
3535
process.exitCode = constants.ERROR_CATCHALL;
3636
return
3737
}
3838

3939
// verify the file does not already exist
4040
if (fs.existsSync(filepath)) {
41-
logger.error(`[smartui] Error: Screenshot config already exists: ${filepath}`);
41+
console.log(`[smartui] Error: Screenshot config already exists: ${filepath}`);
4242
process.exitCode = constants.ERROR_CATCHALL;
4343
return
4444
}
4545

4646
// write stringified default config options to the filepath
4747
fs.mkdirSync(path.dirname(filepath), { recursive: true });
4848
fs.writeFileSync(filepath, JSON.stringify(defaultScreenshotConfig, null, 2) + '\n');
49-
logger.info(`[smartui] Created Screenshot config: ${filepath}`);
49+
console.log(`[smartui] Created Screenshot config: ${filepath}`);
5050
};
5151

5252
module.exports = { createConfig, createScreenshotConfig };

commands/utils/cleanup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const fs = require('fs');
22

3-
function cleanup(logger){
3+
function cleanup(){
44
fs.rm('doms', { recursive: true }, (err) => {
55
if (err) {
6-
return logger.error(err);
6+
return console.error(err);
77
}
88
});
99
return true

commands/utils/dom.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,25 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
9898
});
9999
};
100100

101-
async function fetchDOM(screenshots, options, logger) {
102-
logger.debug("fetchDOM started")
101+
async function fetchDOM(screenshots, options) {
102+
console.log("fetchDOM started")
103103
const createBrowser = require('browserless')
104104
const browser = createBrowser()
105105

106106
if (!fs.existsSync('doms')) {
107107
fs.mkdir('doms', (err) => {
108108
if (err) {
109-
logger.error(err);
109+
console.error(err);
110110
process.exit(constants.ERROR_CATCHALL);
111111
}
112112
});
113113
}
114114

115115
//TODO: Make this async
116116
for (const screenshot of screenshots) {
117-
logger.debug(screenshot)
117+
console.log(screenshot)
118118
let id = generateId(screenshot.name)
119-
logger.debug(id)
119+
console.log(id)
120120
screenshot.id = id
121121
const browserless = await browser.createContext()
122122

@@ -135,16 +135,16 @@ async function fetchDOM(screenshots, options, logger) {
135135
let image = new URL(element.getAttribute('src'), `https://${host}`).href;
136136
element.setAttribute('src', image);
137137
} catch (e) {
138-
logger.error(e);
138+
console.log(e);
139139
}
140140
}
141141

142142
try {
143-
logger.debug("Creating CSS DOM")
143+
console.log("Creating CSS DOM")
144144
await serializeCSSOM(dom, clone);
145145
fs.writeFileSync(`doms/${id}.html`, clone.serialize());
146146
} catch (err) {
147-
logger.error(err);
147+
console.error(err);
148148
}
149149
await browserless.destroyContext();
150150
//Async upload
@@ -191,12 +191,12 @@ function generateId(str) {
191191
return noSpacesStr;
192192
}
193193

194-
async function upload(screenshots, options,logger) {
194+
async function upload(screenshots, options) {
195195
// Create form
196196
let commit = await getLastCommit();
197197
const form = new formData();
198-
logger.info("Upload screenshot started")
199-
logger.debug(screenshots)
198+
console.log("Upload Started with ", screenshots)
199+
200200
for (const screenshot of screenshots) {
201201
const file = fs.readFileSync(`doms/${screenshot.id}.html`);
202202
form.append('files', file, `${screenshot.name}.html`);
@@ -222,14 +222,14 @@ async function upload(screenshots, options,logger) {
222222
...form.getHeaders()
223223
}
224224
}).then(async function (response) {
225-
logger.info('[smartui] Build URL: '+ response.data.buildURL);
226-
logger.info('[smartui] Build in progress...');
225+
console.log('[smartui] Build URL: ', response.data.buildURL);
226+
console.log('[smartui] Build in progress...');
227227
await shortPolling(response.data.buildId, 0, options);
228228
}).catch(function (error) {
229229
if (error.response) {
230-
logger.error('[smartui] Build failed: Error: '+ error.response.data.message);
230+
console.log('[smartui] Build failed: Error: ', error.response.data.message);
231231
} else {
232-
logger.error('[smartui] Build failed: Error: '+ error.message);
232+
console.log('[smartui] Build failed: Error: ', error.message);
233233
}
234234
});
235235
}

commands/utils/package.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ const axios = require('axios');
22
var { constants } = require('./constants');
33

44
// Check for package updates
5-
function checkUpdate(version, options, logger) {
5+
function checkUpdate(version, options) {
66
return axios.get(new URL(constants[options.env].CHECK_UPDATE_PATH, constants[options.env].BASE_URL).href, {
77
params: {
88
packageName: 'smartui-storybook',
99
packageVersion: version
1010
}})
1111
.then(function (response) {
1212
if (response.data.data.deprecated) {
13-
logger.warn('v' + version + ' is deprecated. Please update to v' + response.data.data.latestVersion);
13+
console.log('v' + version + ' is deprecated. Please update to v' + response.data.data.latestVersion);
1414
} else if (response.data.data.latestVersion != version) {
15-
logger.warn('A newer version v' + response.data.data.latestVersion + ' is available.');
15+
console.log('A newer version v' + response.data.data.latestVersion + ' is available.');
1616
}
1717
})
1818
.catch(function (error) {
1919
if (error.response && error.response.data && error.response.data.error) {
20-
logger.error('Cannot check for updates. Error: ' + error.response.data.error.message);
20+
console.log('Cannot check for updates. Error: ' + error.response.data.error.message);
2121
} else {
22-
logger.error('Cannot check for updates. Error: ' + error.message);
22+
console.log('Cannot check for updates. Error: ' + error.message);
2323
}
2424
});
2525
};

commands/utils/validate.js

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,29 @@ class ValidationError extends Error {
1717
}
1818
}
1919

20-
function validateProjectToken(options, logger) {
21-
logger.debug("options", options);
22-
if (process.env.PROJECT_TOKEN) {
20+
function validateProjectToken(options) {
21+
console.log("options", options);
22+
if (process.env.PROJECT_TOKEN) {
2323
return axios.get(constants[options.env].AUTH_URL, {
2424
headers: {
2525
projectToken: process.env.PROJECT_TOKEN
26-
}
27-
})
26+
}})
2827
.then(function (response) {
29-
logger.info('[smartui] Project Token Validated');
28+
console.log('[smartui] Project Token Validated');
3029
})
3130
.catch(function (error) {
3231
if (error.response) {
33-
logger.error('[smartui] Error: Invalid Project Token');
32+
console.log('[smartui] Error: Invalid Project Token');
3433
} else if (error.request) {
35-
logger.error('[smartui] Project Token not validated. Error: ', error.message);
34+
console.log('[smartui] Project Token not validated. Error: ', error.message);
3635
} else {
37-
logger.error('[smartui] Project Token not validated. Error: ', error.message);
36+
console.log('[smartui] Project Token not validated. Error: ', error.message);
3837
}
3938
process.exit(constants.ERROR_CATCHALL);
40-
});
39+
});
4140
}
42-
else {
43-
logger.error('[smartui] Error: please set PROJECT_TOKEN key, refer to https://smartui.lambdatest.com');
41+
else {
42+
console.log('[smartui] Error: please set PROJECT_TOKEN key, refer to https://smartui.lambdatest.com');
4443
process.exit(constants.ERROR_CATCHALL);
4544
}
4645
};
@@ -95,8 +94,7 @@ async function validateLatestBuild(options) {
9594
params: {
9695
branch: commit.branch,
9796
commitId: commit.shortHash
98-
}
99-
})
97+
}})
10098
.then(function (response) {
10199
if (response.data.status === 'Failure') {
102100
console.log(`[smartui] Build with commit '${commit.shortHash}' on branch '${commit.branch}' already exists.`);
@@ -200,56 +198,12 @@ function parse(file) {
200198
return JSON.parse(data);
201199
}
202200

203-
// Verify Screenshot config
204-
function validateScreenshotConfig(configFile, logger) {
205-
// Check for JSON extension
206-
if (!isJSONFile(configFile)) {
207-
logger.error('capture command only supports json file');
208-
process.exit(constants.ERROR_CATCHALL);
209-
}
210-
211-
// Check if file exists
212-
if (configFile) {
213-
try {
214-
fs.accessSync(configFile, fs.constants.F_OK);
215-
} catch (error) {
216-
logger.error('Error: File does not exist ' + configFile);
217-
process.exit(constants.ERROR_CATCHALL);
218-
}
219-
220-
}
221-
222-
223-
let screenshots = {};
224-
// Check JSON Parse Error
225-
if (configFile) {
226-
try {
227-
screenshots = parse(configFile)
228-
} catch (error) {
229-
logger.error('Error: Invalid json file');
230-
process.exit(constants.ERROR_CATCHALL);
231-
}
232-
}
233-
234-
logger.debug(screenshots)
235-
236-
//Check for URLs should not be empty
237-
for (const screenshot of screenshots) {
238-
if(!screenshot.url || screenshot.url == ''){
239-
logger.error('Error: Missing required URL for screenshot');
240-
process.exit(constants.ERROR_CATCHALL);
241-
}
242-
//Check for URLs should valid (like abcd in URL)
243-
try {
244-
new URL(screenshot.url);
245-
} catch (error) {
246-
logger.error('Error: Invalid screenshot URL: '+screenshot.url);
247-
process.exit(constants.ERROR_CATCHALL);
248-
}
249-
}
201+
// Verify Screenshot config
202+
function validateScreenshotConfig(configFile) {
203+
// Verify config file exists
250204
}
251205

252-
module.exports = {
206+
module.exports = {
253207
ValidationError,
254208
validateProjectToken,
255209
validateStorybookUrl,

0 commit comments

Comments
 (0)