-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (88 loc) · 3.31 KB
/
index.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
#!/usr/bin/env node
import program from 'commander';
import jsonfile from 'jsonfile';
import Sitemapper from 'sitemapper';
import { other, scenario, viewports } from './defaults.js';
const sitemap = new Sitemapper();
const combineContents = (name, scenarios = [], paths) => {
return {
...{
id: name,
viewports: viewports,
scenarios: scenarios,
paths: paths,
},
...other
}
}
const getSitemap = async (domain, www) => {
return await sitemap.fetch(`https://${www ? 'www.' : ''}${domain}/sitemap.xml`)
}
const createConfig = async (domain) => {
const scenarios = []
let res = await getSitemap(domain)
if (!res.sites.length) {
console.log(`Sitemap not found, trying www.${domain}`)
res = await getSitemap(domain, true)
}
if (res.sites.length) {
console.log(`Sitemap ${domain} ok, found ${res.sites.length} pages. Creating config`, domain)
for (const key in res.sites) {
if (res.sites.hasOwnProperty(key)) {
const url = res.sites[key];
scenarios.push({
...{
label: url.replace(domain, '').replace(/https:|www.|http:|\/\//g, ''), // Strip url except path to get page name ;)
url: url
},
...scenario
})
}
}
const id = domain.replace(/\//g, "-")
const paths = {
"bitmaps_reference": id + "/bitmaps_reference",
"bitmaps_test": id + "/bitmaps_test",
"engine_scripts": id + "/engine_scripts",
"html_report": id + "/html_report",
"ci_report": id + "/ci_report"
}
jsonfile.writeFile(`./${id}.json`,
combineContents(id, scenarios, paths),
{ spaces: 2 },
function (err) {
if (err) console.error(err)
})
console.log(`Succesfully generated config for ${res.sites.length} pages on`, domain)
console.log("run: npm run setup", id)
console.log("to generate a reference report")
} else {
console.log(`ERROR: Sitemap not found or empty, make sure ${res.url} exist`)
}
}
program
.version('0.0.1')
.description("An CLI used to generate backstop configs from sitemaps")
.option('-s, --site <url>', 'Set url')
.option('-d, --domains <lang,lang>', 'Set of domains extentions to check ex; de,com')
program.parse(process.argv)
if (!program.args[0]) {
console.log("ERROR: No url was given\n")
program.help()
} else if (program.args[0]) {
program.site = program.args[0].replace(/www./, '').trim()
}
if (program.domains) {
console.log("Creating config for", program.args[0])
program.domains = program.domains.split(",")
createConfig(program.site)
for (const key in program.domains) {
if (program.domains.hasOwnProperty(key)) {
const extension = program.domains[key].indexOf('.') ? ('.' + program.domains[key]) : program.domains[key]; // if input is .de,.com instead of de,com,es for example; add a dot
const domain = program.site.substring(0, program.site.indexOf('.')) // Strip domain extention
createConfig(domain + extension)
}
}
} else if (program.site) {
createConfig(program.site)
}