-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
ยท264 lines (236 loc) ยท 7.38 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env node
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import {
LOG_FILE_NAME,
DEFAULT_SITEMAP_URL,
DEFAULT_SELECTOR,
DEFAULT_OUTPUT_FILE,
DEFAULT_IS_SPA,
DEFAULT_LIMIT,
DEFAULT_TAKE_SCREENSHOTS,
DEFAULT_SHOW_ELEMENT_DETAILS,
DEFAULT_SHOW_HTML,
DEFAULT_CRAWL,
} from './src/constants.js';
import SelectorFinder from './src/selector-finder.js';
import SiteCrawler from './src/site-crawler.js';
import Outputter from './src/outputter.js';
import Log from './src/logger.js';
import CSSReader from './src/css-reader.js';
const log = new Log(LOG_FILE_NAME);
/*
CLI arguments
node index.js --sitemap=https://wherever.com/xml --limit=20 --selector=".yourthing"
node index.js -u https://wherever.com/xml -l 20 -s ".yourthing"
*/
const { argv } = yargs(hideBin(process.argv))
.option('sitemap', {
alias: 'u',
description: 'url for the sitemap',
type: 'string',
default: DEFAULT_SITEMAP_URL,
})
.option('crawl', {
alias: 'r',
description: 'treat the url as an html page and crawl from there',
type: 'boolean',
default: DEFAULT_CRAWL,
})
.option('dontUseExportedSitemap', {
alias: 'X',
description: 'Force the Site Crawler to refetch links and ignore an existing sitemap',
type: 'boolean',
default: false,
})
.option('limit', {
alias: 'l',
description: 'how many pages to crawl',
type: 'number',
default: DEFAULT_LIMIT,
})
.option('selector', {
alias: 's',
description: 'css selector',
type: 'string',
default: DEFAULT_SELECTOR,
})
.option('takeScreenshots', {
alias: 'c',
description: 'Take a screenshot',
type: 'boolean',
default: DEFAULT_TAKE_SCREENSHOTS,
})
.option('isSpa', {
alias: 'd',
description: 'Is a Single Page Application',
type: 'boolean',
default: DEFAULT_IS_SPA,
})
.option('outputFileName', {
alias: 'o',
description: 'name of output file',
type: 'string',
default: DEFAULT_OUTPUT_FILE,
})
.option('cssFile', {
alias: 'f',
description: 'path to a CSS File',
type: 'string',
})
.option('showElementDetails', {
alias: 'e',
description: 'Show details like tagname, attributes, innerText for elements',
type: 'boolean',
default: DEFAULT_SHOW_ELEMENT_DETAILS,
})
.option('showHtml', {
alias: 'm',
description: 'Shows the HTML for the element',
type: 'boolean',
default: DEFAULT_SHOW_HTML,
})
.help()
.alias('help', 'h');
const {
sitemap,
crawl,
limit,
dontUseExportedSitemap,
selector,
outputFileName,
takeScreenshots,
isSpa,
cssFile,
showElementDetails,
showHtml,
} = argv;
const selectorFinderConfig = {
sitemap,
crawl,
limit,
useExportedSitemap: !dontUseExportedSitemap,
selector,
outputFileName,
takeScreenshots,
isSpa,
cssFile,
showElementDetails,
showHtml,
};
async function setCSSFileSelectors(config) {
if (config.cssFile) {
try {
const cssReader = new CSSReader(config.cssFile);
await cssReader.readFileAsync();
// eslint-disable-next-line no-param-reassign
config.selector = cssReader.selectors;
} catch (cssFileReadError) {
await log.errorToFileAsync(cssFileReadError);
}
}
return config;
}
function getFormattedResult(result, hasElementDetails, hasElementHtml) {
const formattedResult = { ...result };
const { pagesWithSelector } = result;
const editedPages = pagesWithSelector.map((pageWithSelector) => {
const editedPageWithSelector = { ...pageWithSelector };
if (!hasElementDetails) {
const editedElements = editedPageWithSelector.elements.map((element) => {
const { html } = element;
return { selector: element.selector, html };
});
editedPageWithSelector.elements = editedElements;
}
if (!hasElementHtml) {
const editedElements = editedPageWithSelector.elements.map((element) => {
const newEl = { ...element };
delete newEl.html;
return newEl;
});
editedPageWithSelector.elements = editedElements;
}
return editedPageWithSelector;
});
formattedResult.pagesWithSelector = editedPages;
return formattedResult;
}
async function main(config) {
const outputter = new Outputter(DEFAULT_OUTPUT_FILE, log);
let mainConfig = { ...config };
if (!mainConfig.sitemap) {
await log.toConsole('No sitemap provided. Exiting.');
await log.errorToFileAsync('No sitemap provided. Exiting.');
return;
}
try {
const startMessage = `
๐๐ SelectorHound is looking...
๐ Sitemap: ${mainConfig.sitemap}
๐ limit: ${limit === 0 ? 'None' : limit}
${mainConfig.cssFile ? `๐ cssFile: ${cssFile}` : ''}
${mainConfig.selector && !mainConfig.cssFile ? `๐ฏ CSS Selector: ${mainConfig.selector}` : ''}
${mainConfig.showElementDetails ? '๐ก Show full details for matching elements' : ''}
${mainConfig.isSpa ? '๐ก Handle as Single Page Application' : ''}
${mainConfig.takeScreenshots ? '๐ท Take Screenshots' : ''}
${mainConfig.useExportedSitemap ? '' : '๐ก Ignore any existing .sitemap.json file and make a new one'}
`;
await log
.toConsole(startMessage)
.startTimer()
.infoToFileAsync();
if (mainConfig.cssFile) {
mainConfig = await setCSSFileSelectors(mainConfig);
}
// Set up the Crawler
const siteCrawler = new SiteCrawler(
{
startPage: mainConfig.sitemap,
shouldCrawl: mainConfig.crawl,
useExportedSitemap: mainConfig.useExportedSitemap,
},
);
log.toConsole(`
๐ ${mainConfig.crawl ? 'Crawling site' : 'Fetching sitemap'} ${mainConfig.crawl ? 'starting on ๐' : 'from ๐ฆด'} ${siteCrawler.config.startPage}
`);
await siteCrawler.produceSiteLinks();
const numberOfSiteLinks = siteCrawler.linkSet.size;
const isNotExported = !mainConfig.useExportedSitemap;
const siteLinksMessage = `๐ ${numberOfSiteLinks} URLs ${isNotExported ? 'exported to' : 'read from'} ๐พ ${siteCrawler.exportFileName}.sitemap.json`;
log.toConsole(siteLinksMessage);
if (siteCrawler.linkSet.size === 0) {
const noLinksMessage = `
๐ซ๐ No links found. Nothing To search.`;
await log
.toConsole(noLinksMessage)
.infoToFileAsync(noLinksMessage);
return;
}
mainConfig.siteCrawler = siteCrawler;
const selectorFinder = new SelectorFinder(mainConfig);
const result = await selectorFinder.findSelectorAsync();
const { totalPagesSearched, pagesWithSelector, totalMatches } = result;
const formattedResult = getFormattedResult(
result,
mainConfig.showElementDetails,
mainConfig.showHtml,
);
await outputter.writeDataAsync(formattedResult, outputFileName);
log.endTimer();
const { elapsedTime } = log;
const friendlyTime = elapsedTime > 300 ? `${(elapsedTime / 60).toFixed(2)}m` : `${elapsedTime}s`;
const endMessage = `
๐ถ SelectorHound is finished!
โฑ Time lapsed: ${friendlyTime}
๐ Pages Scanned: ${totalPagesSearched}
๐ฏ Pages with a Match: ${pagesWithSelector.length}
๐งฎ Total Results: ${totalMatches} ${totalMatches > pagesWithSelector.length ? '(multiple matches on a page)' : ''}
๐พ Results File: ${outputFileName}${outputFileName !== 'pages.json' ? '.pages.json' : ''}
`;
await log.toConsole(endMessage, true).infoToFileAsync();
} catch (mainFunctionError) {
await log.errorToFileAsync(mainFunctionError);
}
}
main(selectorFinderConfig);