-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (129 loc) · 4.15 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
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
const core = require('@actions/core');
const puppeteer = require('puppeteer-core');
const { Cluster } = require('puppeteer-cluster');
const { AxePuppeteer } = require('axe-puppeteer');
const colors = require('colors');
const path = require('path');
const os = require("os");
const opts = ['--no-sandbox', '--disable-setuid-sandbox'];
const localhost = `file://${process.env.GITHUB_WORKSPACE}${core.getInput('directory')}`;
if (!localhost) {
core.warning('Directory was not set');
}
const getChromePath = () => {
let browserPath;
if (os.type() === "Windows_NT") {
// Chrome is usually installed as a 32-bit application, on 64-bit systems it will have a different installation path.
const programFiles = os.arch() === 'x64' ? process.env["PROGRAMFILES(X86)"] : process.env.PROGRAMFILES;
browserPath = path.join(programFiles, "Google/Chrome/Application/chrome.exe");
} else if (os.type() === "Linux") {
browserPath = "/usr/bin/google-chrome";
} else {
browserPath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
}
if (browserPath && browserPath.length > 0) {
return path.normalize(browserPath);
}
throw new TypeError(`Cannot run action. ${os.type} is not supported.`);
}
const unknownError = (e) => {
console.log(e);
const message = 'Something went wrong, please make sure storybook is running or is pointed to the right location.';
console.error(message.red);
core.setFailed(message);
process.exit(1);
}
const logger = (story, violation) => {
const name = `${story.kind}: ${story.name}`;
if (violation) {
const {description, helpUrl, nodes} = violation;
console.error(
`
${name}
`.cyan,
` ${violation.description}\n`.red,
` Please check:`.red, `${violation.helpUrl}\n`.red,
` ${violation.nodes[0].failureSummary}`.red
);
} else {
console.log(
`
${name}
`.cyan,
' All accessibility checks passed'.green
);
}
};
const getStorybook = async (browser, url) => {
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle2'
});
const evaluate = await page.evaluate('__STORYBOOK_CLIENT_API__.getStorybook()');
await page.close();
return evaluate;
};
const getStories = async (browser, components) => {
return components.map((component) => {
const kind = component.kind;
return component.stories.map((story) => {
const name = story.name;
return {
url: `${localhost}?selectedKind=${kind}&selectedStory=${name}`,
kind,
name
};
})
});
};
(async () => {
const browser = await puppeteer.launch({args: opts, executablePath: getChromePath()}).catch((e) => unknownError(e));
const components = await getStorybook(browser, localhost).catch((e) => unknownError(e));
const stories = await getStories(browser, components);
let errors = [];
await browser.close();
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 10,
puppeteerOptions: {
args: opts,
executablePath: getChromePath()
},
puppeteer
});
const allStories = stories.reduce((all, value) => {
return all.concat(value);
}, []);
await cluster.task(async ({ page, data }) => {
const {url} = data;
try {
await page.goto(url, {waitUntil: 'networkidle2'});
const results = await new AxePuppeteer(page).include('#root').analyze();
await page.close();
if (results.violations.length < 1) {
logger(data);
}
results.violations.forEach((violation) => {
errors.push(violation);
logger(data, violation);
});
} catch (err) {
console.log(err);
core.setFailed('Page failed to load');
throw err;
}
});
for (const storyObj of allStories) {
cluster.queue(storyObj);
}
await cluster.idle();
await cluster.close();
if (errors.length > 0) {
console.error(`\n${errors.length} accessibility tests failed`.underline.red);
core.setFailed(`${errors.length} accessibility tests failed`);
process.exit(1);
} else {
console.log(`\nAll accessibility tests passed`.underline.green);
process.exit(0);
}
})();