-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapomatic.js
executable file
·185 lines (169 loc) · 5.99 KB
/
snapomatic.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
const { spawn } = require('child_process'),
{ Chromeless } = require('chromeless'),
untildify = require('untildify'),
program = require('commander'),
path = require('path'),
fs = require('fs'),
chromeLauncher = require('chrome-launcher'),
ora = require('ora')
colors = require('colors'),
spinner = ora(),
csv = require('csvtojson'),
snapomatic = require('./package.json'),
mkdirp = require('mkdirp')
let workflow,
div = colors.magenta("============================================================"),
config = {
"size": {
"height": 800,
"width": 1280,
"scale": 1
},
"users": {},
"domain": null,
"input": null,
"output": null,
"screenshots": [],
"log": false
}
program
.version(snapomatic.version)
.allowUnknownOption()
.option('-l, --log', 'enable console logging')
.option('-i, --input <file>','path to CSV input file')
.option('-o, --output <path>', 'path to output folder')
.option('-d, --domain <url>', 'domain to use with input file')
.option('-u, --users <users>', 'JSON users object')
.option('-w, --workflow <workflow>', 'path to external workflow file')
.parse(process.argv);
if(typeof program.users !== 'undefined') {
if(program.users.charAt(0) == "{") {
config.users = JSON.parse(program.users)
} else {
config.users = JSON.parse(fs.readFileSync(program.users, 'utf8'));
}
}
if(typeof program.workflow === 'undefined') {
workflow = require('./workflow.js')
} else {
workflow = require(program.workflow)
}
if(typeof program.input === 'undefined') {
console.error(colors.red("✖ Error: No input CSV file provided. Specify input file with `-i <file>`"))
process.exit(1)
} else {
config.input = untildify(program.input)
}
if(typeof program.output === 'undefined') {
console.error(colors.red("✖ Error: No output folder provided. Specify output folder with `-i <file>`"))
process.exit(1)
} else {
config.output = untildify(program.output)
if (!fs.existsSync(config.output)){
if(typeof program.users !== 'undefined') {
for(let i=0; i < Object.keys(config.users).length; i++) {
mkdirp(config.output + '/' + Object.keys(config.users)[i])
}
} else {
mkdirp(config.output);
}
} else {
if(typeof program.users !== 'undefined') {
for(let i=0; i < Object.keys(config.users).length; i++) {
mkdirp(config.output + '/' + Object.keys(config.users)[i])
}
}
}
}
if(typeof program.domain === 'undefined') {
console.error(colors.red("✖ Error: No URL provided. Specify domain with `-d <url>`"))
process.exit(1)
} else {
if(program.domain.startsWith('http://')) {
if(typeof program.users === 'undefined') {
console.warn(colors.yellow("⚠ Warning: Running over http\n"))
config.domain = program.domain
} else {
console.error(colors.red("✖ Error: Cannot process users over http.\n Please specify https or remove users object."))
process.exit(1)
}
} else if(program.domain.startsWith('https://')){
config.domain = program.domain
} else {
config.domain = 'https://' + program.domain
}
}
if(program.log) {
config.log = true
} else {
spinner.start(colors.yellow('Loading ' + config.domain))
}
csv()
.fromFile(config.input)
.then(jsonObj=>{
config.screenshots = jsonObj;
}).then(jsonObj=>{
chromeLauncher.launch({
port: 9222,
chromeFlags: ['--disable-gpu', '--crash-dumps-dir=/tmp', '--hide-scrollbars', '--headless']
}).then(chrome => {
if(config.log) {
console.log('\n' + colors.magenta("Command")+ '\n' + div)
console.log(colors.blue("Node path: ") + process.argv[0])
console.log(colors.blue("Script path: ") + process.argv[1])
console.log(colors.blue("Arguments: ") + process.argv.slice(2,process.argv.length).toString() + '\n\n')
console.log(colors.magenta("Configuration") + '\n' + div)
console.log("config: " + JSON.stringify(config, null, 2))
console.log('\n' + colors.magenta("Chrome")+ '\n' + div)
console.log(chrome)
}
if(!config.log) spinner.succeed(colors.green("Loaded " + config.domain))
run(chrome).catch(function(error){
if(!config.log) spinner.stop()
console.error(colors.red("✖ Error: Unable to process workflow. Debug log:"))
console.error(error)
process.exit(1)
})
})
})
async function run(chrome) {
const chromeless = new Chromeless({
launchChrome: false,
"debug": config.log,
"implicitWait": true,
"waitTimeout": 30000,
"viewport": {
"width": config.size.width,
"height": config.size.height,
"scale": config.size.scale
},
"cdp": {
"host": "127.0.0.1",
"port": chrome.port,
"secure": false,
"closeTab": true
}
})
for(let i = 0; i < config.screenshots.length; i++) {
let url = config.domain + config.screenshots[i]["URL"],
description = config.screenshots[i]["Description"],
role = config.screenshots[i]["Role"],
title = config.screenshots[i]["Title"],
indicator = (role == '') ? role : " (" + role + ")",
file = path.join(config.output + '/' + role, title + indicator + ".png")
if(!config.log) spinner.start(colors.yellow("Creating screenshot " + (i+1) + ": " + title + indicator))
if(config.log) {
console.log('\n\n' + colors.magenta(title) + '\n' + colors.magenta(description) + '\n' + div)
console.log(colors.blue("Current role: ") + role)
console.log(colors.blue("Current URL: ") + url)
console.log(colors.blue("Output file: ") + file + '\n')
}
await workflow.process(description, role, title, {filePath:file}, url, chromeless, config)
}
await chromeless.end()
chrome.kill()
if(config.log) console.log('\n')
spinner.succeed(colors.green(config.screenshots.length + " screenshots created"))
if(!config.log) spinner.stop()
}