-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiffy-screenshots.js
More file actions
120 lines (98 loc) · 3.82 KB
/
diffy-screenshots.js
File metadata and controls
120 lines (98 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Example to run
// node --env-file=.env diffy-screenshots.js --url=https://diffy.website
const process = require("process");
const logger = require('./lib/logger')
const { Jobs } = require('./lib/jobs')
const jobs = new Jobs(logger)
const { Api } = require('./lib/api.js')
const fs = require("fs/promises");
const Promise = require("bluebird");
const path = require("path");
const os = require("os");
const apiKey = process.env.DIFFY_API_KEY || ''
if (apiKey == '') {
console.log('Add Diffy API key to .env file. DIFFY_API_KEY=XXX')
return;
}
const projectId = process.env.DIFFY_PROJECT_ID || ''
if (projectId == '') {
console.log('Add Diffy API project ID .env file. DIFFY_PROJECT_ID=XXX')
return;
}
const maxWorkers = parseInt(process.env.DIFFY_MAX_WORKERS || '5');
console.log(`Running with ${maxWorkers} workers`);
const diffyUrl = 'https://app.diffy.website/api'
const diffyWebsiteUrl = 'https://app.diffy.website/#'
// Staging URLs
// const diffyUrl = 'https://stage.diffy.website/api'
// const diffyWebsiteUrl = 'https://stage.diffy.website/#'
const argv = require('minimist')(process.argv.slice(2));
async function end (code = 1) {
try {
// Remove tmp files.
// func.cleanTmpDir()
} catch (e) {
console.log('Failed to remove tmp files', e)
}
process.exit(code)
}
process.once('SIGTERM', end)
process.once('SIGINT', end)
process.on('uncaughtException', async (e) => {
console.log('UncaughtException', e)
await end()
});
process.on('unhandledRejection', async (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, reason)
await end()
});
(async () => {
if (argv.url === undefined) {
console.log('Provide --url parameter. Example --url="https://diffy.website"');
}
const screenshotName = argv['screenshot-name'] ? argv['screenshot-name'] : argv.url;
try {
const api = new Api(diffyUrl, apiKey, projectId, logger)
await api.login()
const project = await api.getProject()
const jobsList = jobs.prepareJobs(argv.url, project)
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const uploadItems = await Promise.map(jobsList, async (job, index) => {
const outputFilepath = path.join(os.tmpdir(), `screenshot-results-${index}.json`);
const inputFilepath = path.join(os.tmpdir(), `screenshot-input-${index}.json`);
let jsonJob = JSON.stringify(job);
try {
await fs.writeFile(inputFilepath, jsonJob);
} catch (err) {
console.log('Failed to write file', err);
}
console.log(`Starting screenshot ${(index + 1)} of ${jobsList.length}`);
const startTime = performance.now();
await exec('node ./index.js --env-file=.env --local=true --output-filepath=\'' + outputFilepath + '\' --file=\'' + inputFilepath + '\'', {stdio: 'inherit'});
const duration = ((performance.now() - startTime) / 1000).toFixed(2);
console.log(`Completed screenshot ${(index + 1)} of ${jobsList.length} in ${duration}s`);
const resultsContent = await fs.readFile(outputFilepath, 'utf8');
console.log('Output file content', resultsContent);
let result = JSON.parse(resultsContent);
// Clean up temp files
await fs.unlink(inputFilepath).catch(() => {});
await fs.unlink(outputFilepath).catch(() => {});
return {
status: true,
breakpoint: job.params.breakpoint,
uri: job.params.uri,
filename: result.screenshot,
htmlFilename: result.html,
jsConsoleFilename: result.jsConsole
};
}, { concurrency: maxWorkers });
// Send screenshots to Diffy.
screenshotId = await api.uploadScreenshots(screenshotName, uploadItems)
console.log(`Diffy screenshot url: ${diffyWebsiteUrl}/snapshots/${screenshotId}`)
await end(0)
} catch (e) {
console.log('Failed to run executor', e)
await end()
}
})()