-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathutils.ts
More file actions
158 lines (134 loc) · 3.89 KB
/
Copy pathutils.ts
File metadata and controls
158 lines (134 loc) · 3.89 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
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
import fs from 'fs';
import path from 'path';
import colors from 'colors';
import type { AxeResults, UnlabelledFrameSelector } from 'axe-core';
export const { CHROME_TEST_PATH, CHROMEDRIVER_TEST_PATH } = process.env;
export const saveOutcome = (
outcome: AxeResults | AxeResults[],
fileName?: string,
dir?: string
): string => {
if (typeof fileName !== 'string') {
fileName = 'axe-results-' + Date.now() + '.json';
}
/* istanbul ignore if */
if (typeof dir !== 'string') {
dir = process.cwd();
} else if (!path.isAbsolute(dir)) {
dir = path.join(process.cwd(), dir);
}
const filePath = path.join(dir, fileName);
/* istanbul ignore else */
if (!fs.existsSync(filePath)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(outcome, null, 2), 'utf-8');
return filePath;
};
export const parseUrl = (url: string): string => {
if (!/[a-z]+:\/\//.test(url)) {
return 'http://' + url;
}
return url;
};
/**
* Convert a user provided browser name into a normalized form
* for internal use. Conversion is case-insensitive. Any matching
* character set may also be provided if it is shorter than the
* expected strings. If no browser is provided, it defaults
* to 'chrome-headless'.
*
* @throws {Error} If the browser is not recognized.
*/
export const parseBrowser = (browser?: string): string => {
if (!browser) {
return 'chrome-headless';
}
const l = browser.length;
switch (browser.toLowerCase()) {
case 'ff':
case 'firefox'.substring(0, l):
case 'gecko'.substring(0, l):
case 'marionette'.substring(0, l):
return 'firefox';
case 'chrome'.substring(0, l):
return 'chrome';
case 'ie':
case 'explorer'.substring(0, l):
case 'internetexplorer'.substring(0, l):
case 'internet_explorer'.substring(0, l):
case 'internet-explorer'.substring(0, l):
return 'ie';
case 'safari'.substring(0, l):
return 'safari';
case 'edge'.substring(0, l):
case 'microsoftedge'.substring(0, l):
return 'MicrosoftEdge';
default:
throw new Error('Unknown browser ' + browser);
}
};
export const getAxeSource = (
axePath?: string,
dirname?: string
): string | void => {
// Abort if axePath should exist, and it isn't
if (axePath && !fs.existsSync(axePath)) {
return;
}
let cwd = dirname;
if (!cwd) {
cwd = process.cwd();
}
if (!dirname) {
dirname = __dirname;
}
// Look for axe in current working directory
if (!axePath) {
axePath = path.join(cwd, 'axe.js');
}
if (!fs.existsSync(axePath)) {
// Look for axe in CWD ./node_modules
axePath = path.join(cwd, 'node_modules', 'axe-core', 'axe.js');
}
if (!fs.existsSync(axePath)) {
// in local develop using npm workspaces axe-core is
// hoisted to the root, but when published axe-core
// will be in the node_modules for the cli
axePath = require.resolve('axe-core/axe.js');
}
return fs.readFileSync(axePath, 'utf-8');
};
export const getAxeVersion = (source: string): string => {
const match = source.match(/\.version\s*=\s'([^']+)'/);
return match ? match[1] : 'unknown version';
};
export const splitList = (val: string): string[] => {
return val.split(/[,;]/).map(str => str.trim());
};
export const selectorToString = (
selectors: UnlabelledFrameSelector,
separator?: string
): string => {
separator = separator || ' ';
return selectors
.reduce((prev, curr) => prev.concat(curr as never), [])
.join(separator);
};
export const reporter = (
noReporter: boolean,
silentMode: boolean
): (() => void) => {
if (!noReporter || silentMode) {
return () => {};
} else {
return (...args: string[]) => {
console.log(...args);
};
}
};
export const link = colors.underline.blue;
export const error = colors.red.bold;
export const bold = colors.bold;
export const green = colors.green;
export const italics = colors.italic;