@zachleat/spider-pig
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -42,4 +42,4 @@ #!/usr/bin/env node | ||
let localUrl = urls[j]; | ||
let has = await sp.hasSelector(localUrl, selector); | ||
console.log( `${localUrl}`, has ? chalk.green("✅ Yes") : chalk.red("❌ No") ); | ||
let resultCount = await sp.selectorCount(localUrl, selector); | ||
console.log( `${localUrl}`, resultCount > 0 ? chalk.green(`✅ Yes (${resultCount} result${resultCount !== 1 ? "s" : ""})`) : chalk.red("❌ No") ); | ||
} | ||
@@ -46,0 +46,0 @@ } |
{ | ||
"name": "@zachleat/spider-pig", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Get a list of local URL links from a root URL.", | ||
@@ -5,0 +5,0 @@ "main": "SpiderPig.js", |
# spider-pig | ||
Get a list of local URL links from a root URL. Works with JavaScript generated content. | ||
Get a list of local URL links from a root URL. Works with JavaScript generated content. Can also act as a live-DOM CSS search across multiple files (find all the templates that are using the CSS selector I want to change). | ||
@@ -31,3 +31,3 @@ ``` | ||
http://zachleat.localhost/web/best-of/ | ||
… etc. | ||
… | ||
``` | ||
@@ -67,2 +67,3 @@ | ||
http://zachleat.localhost/web/best-of/ ✅ Yes | ||
… | ||
``` | ||
@@ -81,18 +82,24 @@ | ||
let sp = new SpiderPig(); | ||
await sp.start(); | ||
(async function() { | ||
let sp = new SpiderPig(); | ||
await sp.start(); | ||
let urls = await sp.fetchLocalUrls("http://localhost/myproject/"); | ||
let urls = await sp.fetchLocalUrls("http://localhost/myproject/"); | ||
// Optional, filter (case sensitive) | ||
urls = sp.filterUrls(urls, "views"); | ||
// Optional, filter (case sensitive) | ||
urls = sp.filterUrls(urls, "views"); | ||
// Optional | ||
for(let url of urls) { | ||
if( await sp.hasSelector(url, ".test-css-selector:nth-child(2)") ) { | ||
// has it | ||
} else { | ||
// doesn’t have it | ||
// Optional | ||
for(let url of urls) { | ||
if( await sp.hasSelector(url, ".test-css-selector:nth-child(2)") ) { | ||
// has it | ||
} else { | ||
// doesn’t have it | ||
} | ||
} | ||
} | ||
``` | ||
})(); | ||
``` | ||
## | ||
![Homer Simpson holding the Spider Pig](docs/spider-pig.jpg) |
@@ -68,6 +68,6 @@ const puppeteer = require("puppeteer"); | ||
async hasSelector(localUrl, sel) { | ||
async selectorCount(url, sel) { | ||
let browser = await puppeteer.launch(); | ||
let page = await browser.newPage(); | ||
await page.goto(localUrl, { | ||
await page.goto(url, { | ||
waitUntil: ["load", "networkidle0"] | ||
@@ -78,5 +78,10 @@ }); | ||
return ret.length > 0; | ||
return ret.length; | ||
} | ||
async hasSelector(url, sel) { | ||
let results = await this.selectorCount(url, sel); | ||
return results > 0; | ||
} | ||
filterUrls(hrefs, filter) { | ||
@@ -83,0 +88,0 @@ return hrefs.filter(function(href) { |
@@ -10,2 +10,3 @@ import test from "ava"; | ||
const urlRoot = "http://localhost:8080/"; | ||
const globalServer = openServer(); | ||
@@ -15,3 +16,3 @@ function openServer() { | ||
let server = connect().use(serveStatic(sitePath)).listen(8080, function() { | ||
console.log(`Web server started on 8080 for ${sitePath}.`); | ||
// console.log(`Web server started on 8080 for ${sitePath}.`); | ||
resolve(server); | ||
@@ -22,6 +23,11 @@ }); | ||
function closeServer(server) { | ||
test.before(async t => { | ||
await globalServer; | ||
}); | ||
test.after.always(async t => { | ||
let server = await globalServer; | ||
server.close(); | ||
console.log( "Web server closed." ); | ||
} | ||
// console.log( "Web server closed." ); | ||
}); | ||
@@ -57,9 +63,6 @@ test("cleanupHref", t => { | ||
test("files/root.html", async t => { | ||
let url = urlRoot + "files/root.html"; | ||
let sp = new SpiderPig(); | ||
await sp.start(); | ||
let server = await openServer(); | ||
let urls = await sp.fetchLocalUrls(url); | ||
let urls = await sp.fetchLocalUrls(urlRoot + "files/root.html"); | ||
let expected = [ | ||
@@ -81,4 +84,14 @@ "http://localhost:8080/test.html", | ||
await sp.finish(); | ||
}); | ||
closeServer(server); | ||
test(".selector()", async t => { | ||
let sp = new SpiderPig(); | ||
await sp.start(); | ||
t.is( await sp.selectorCount(urlRoot + "files/hasselector.html", ".myselector" ), 3 ); | ||
t.is( await sp.selectorCount(urlRoot + "files/hasselector.html", ".madeupselector" ), 0 ); | ||
t.is( await sp.selectorCount(urlRoot + "files/doesnothaveselector.html", ".myselector" ), 0 ); | ||
t.is( await sp.selectorCount(urlRoot + "files/doesnothaveselector.html", "div" ), 3 ); | ||
await sp.finish(); | ||
}); |
66939
10
217
102