-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·211 lines (181 loc) · 5.22 KB
/
cli.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
'use strict';
const inquirer = require('inquirer');
const chalk = require('chalk');
const meow = require('meow');
const { URL } = require('url');
const { getHostame, getSitemap, readConfigs } = require('./lib/utils');
const run = require('.');
// Initialize cli
const cli = meow(
`
Usage
$ visual-tester <cmd>
Cmd
test Compare with reference images
approve Approve test
reference Generate reference images
Options
--query Add query params to page request
--approve Approve test (same as approve cmd)
--reference Generate reference images (same as reference cmd)
--config Manually specify a <project>.visualtest.config.js file
--config-dir Specify custom directory to search for project configs
--output-dir Specify custom output directory
--uid Manually set uid to make references from different domains (live - staging)
Examples
$ visual-tester test --query 'optimize-css=0'
`,
{
flags: {
query: {
type: 'string',
},
config: {
type: 'string',
},
configDir: {
type: 'string',
},
outputDir: {
type: 'string',
},
uid: {
type: 'string',
},
},
}
);
/**
* Get project from configs
* @param {*} configs Config data
*/
const getProject = async (configs) => {
if (configs.length === 0) {
return null;
}
// Return the first project if only one exists
if (configs.length === 1) {
return configs[0];
}
// Ask the user if there are more than one projects available
const choices = [...new Set(configs.filter((config) => config.name).map((config) => config.name))];
const { project } = await inquirer.prompt({
type: 'list',
name: 'project',
message: 'Choose project',
choices,
});
return configs.find((config) => config.name === project);
};
/**
* Build volatile environment from sitemap
* @param {String} sitemap Url to sitemap
*/
const getEnvironmentFromSitemap = async (sitemap) => {
const url = new URL(sitemap);
const environment = {
host: url.origin,
user: url.username,
pass: url.password,
sitemap: url.pathname,
};
return {
...environment,
uid: url.hostname,
urls: await getSitemap(environment),
};
};
/**
* Build environment from project
* @param {Object} project Project config
*/
const getEnvironmentFromProject = async (project, reference) => {
const { environments: environmentsRaw, urls, uid, ...projectData } = project;
if (!environmentsRaw && !urls) {
console.log(`${chalk.red('Error: Missing urls or environments config')}`);
process.exit(1);
}
if (environmentsRaw && !Array.isArray(environmentsRaw) && typeof environmentsRaw !== 'function') {
console.log(`${chalk.red('Error: environments need to be an array or a function returning an array')}`);
process.exit(1);
}
// Handle async function or static config
const environments = environmentsRaw
? Array.isArray(environmentsRaw)
? environmentsRaw
: await environmentsRaw()
: [];
let [environment] = environments;
const choices = environments.map((env) => env.name || env.host);
if (environments.length > 1) {
const { environmentName } = await inquirer.prompt({
type: 'list',
name: 'environmentName',
message: 'Choose environment',
choices,
});
environment = environments.find((env) => (env.name || env.host) === environmentName);
}
const envId = environment ? getHostame(environment) : '';
const sitemapUrls = await getSitemap(environment);
const result = {
...projectData,
...(environment || {}),
uid: [uid, envId].filter((v) => v).join('/'),
urls: sitemapUrls,
};
if (environments.length > 1 && reference) {
const { name: selected } = environment;
const { environmentName } = await inquirer.prompt({
type: 'list',
name: 'environmentName',
message: 'Choose reference environment',
choices: choices,
default: selected,
});
if (environmentName !== selected) {
result.referenceEnvironment = environments.find((env) => (env.name || env.host) === environmentName);
}
}
if (Array.isArray(urls)) {
return {
...result,
urls: [...new Set([...urls, ...sitemapUrls])],
};
}
// Handle async function passed as url prop in the config
if (typeof urls === 'function') {
return {
...result,
urls: await urls(result),
};
}
return result;
};
// Run
(async () => {
let [cmd = 'test', sitemap = ''] = cli.input;
const { reference, approve, compare } = cli.flags;
if (!sitemap && /\:\/\//.test(cmd)) {
sitemap = cmd;
cmd = 'test';
}
if (reference) {
cmd = 'reference';
} else if (approve) {
cmd = 'approve';
}
if (sitemap) {
const environment = await getEnvironmentFromSitemap(sitemap);
return run({ ...environment, ...cli.flags }, cmd);
}
const configs = await readConfigs(cli.flags);
if (configs.length === 0) {
console.log(`${chalk.red('Error: No config file found')}`);
process.exit(1);
}
const project = await getProject(configs);
const environment = await getEnvironmentFromProject(project, cmd === 'reference');
return run({ ...environment, ...cli.flags }, cmd);
})();