Skip to content

Commit

Permalink
fix: better finish scan
Browse files Browse the repository at this point in the history
- more than 2 tries to open xlsx
  • Loading branch information
popstas committed Aug 22, 2020
1 parent e4cf923 commit eb25a69
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const color = require('./color');
let SKIP_IMAGES = true;
let SKIP_CSS = true;
let SKIP_JS = true;
const finishTries = 5;

// поля описаны в API по ссылке выше
const fields_presets = {
Expand Down Expand Up @@ -653,13 +654,9 @@ module.exports = async (baseUrl, options = {}) => {
// close lighthouse's chrome
await chromeLauncher.killAll();;

const finishScan = () => {
if(options.removeCsv) {
fs.unlinkSync(csvPath);
}

// Validate summary
const outValidationSummary = () => {
const sum = getValidationSum();
if(Object.entries(sum).length === 0) return;
console.log(`\n\n${color.white}Validation summary:${color.reset}`);
for(let colName in sum) {
console.log(`\n${color.white}${colName}:${color.reset}`);
Expand All @@ -668,34 +665,49 @@ module.exports = async (baseUrl, options = {}) => {
console.log(`${msgColor}${res.msg}${color.reset}\t${res.url}`);
}
}

console.log(`\n${color.yellow}Saved to ${xlsxPath}${color.reset}`);
console.log(`Finish: ${t} sec (${perPage} per page)`);
if(options.openFile) exec(`"${xlsxPath}"`);
};

let isSuccess = true;
try {
const finishScan = async () => {
saveAsXlsx(csvPath, xlsxPath);
console.log(`\n${color.yellow}Saved to ${xlsxPath}${color.reset}`);

if (options.json) await saveAsJson(csvPath, jsonPath, options.lang);
if (options.upload) webPath = await uploadJson(jsonPath, options);

if (options.web) await publishGoogleSheets(xlsxPath);

if (options.json) await startViewer(jsonPath, webPath);
} catch (e) {
if(e.code == 'EBUSY'){
isSuccess = false;
console.error(`${color.red}${xlsxPath} is busy, please close file in 10 seconds!`);
setTimeout(async () => {
saveAsXlsx(csvPath, xlsxPath);
if (options.json) await saveAsJson(csvPath, jsonPath, options.lang);
if (options.upload) webPath = await uploadJson(jsonPath, options);
if (options.web) await publishGoogleSheets(xlsxPath);
if (options.json) await startViewer(jsonPath, webPath);
finishScan();
}, 10000)

if(options.removeCsv) {
fs.unlinkSync(csvPath);
}
else console.error(e);
}

if(isSuccess) finishScan();
console.log(`Finish: ${t} sec (${perPage} per page)`);

if(options.openFile) exec(`"${xlsxPath}"`);
};

outValidationSummary();

const tryFinish = async (tries) => {
try {
await finishScan()
} catch (e) {
if (e.code == 'EBUSY') {
let msg = `${color.red}${xlsxPath} is busy`;
if(tries > 0) msg += ', please close file in 10 seconds!';
console.error(msg);

if (tries > 0) {
setTimeout(async () => {
await tryFinish(tries - 1);
}, 10000);
}
} else {
console.error(e);
}
}
};

await tryFinish(finishTries);
};

0 comments on commit eb25a69

Please sign in to comment.