-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ability to snapshot a directory of webpages. Usage: `percy snap…
…shot directory/` (#137) * Add wip static snpashot service * Clean trailing slashes off passed in asset dir and baseUrl * Install walk package * Remove errant semicolon * WIP static snapshot service * Add dummy static site app for testing * Refactor static snapshot service * Add tests for static snapshot service * Clean up misses from rebase * Code cleanup * Make the ignore regex required for the static snapshot service * Add baseURL to snapshot url walking and clean up regex logic * Add test static site * Add test command for snapshot multiplatform integration testing * Refactor snapshot file capture and ignore logic * Comment cleanup * Fix typos * Add debugger logging * Incorporate PR feedback * Update testing static site folder names * Build the local static site url in a seperate function * Test the actual urls instead of just a count of urls * Uninstall walk and install globby * Update snapshot command to new glob syntax and naming * Update static snapshot options interface * Update static snapshot service to use globby instead of walk * Add nested file for unit testing * Update unit tests * Remove walk types * Code cleanup * Implement ignore glob * Test ignore glob * Incorporate PR feedback
- Loading branch information
1 parent
bbf6bae
commit 20daabb
Showing
34 changed files
with
3,151 additions
and
2,863 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,93 @@ | ||
import * as bodyParser from 'body-parser' | ||
import * as cors from 'cors' | ||
import * as express from 'express' | ||
import * as globby from 'globby' | ||
import {Server} from 'http' | ||
import * as puppeteer from 'puppeteer' | ||
import logger from '../utils/logger' | ||
import {agentJsFilename} from '../utils/sdk-utils' | ||
import {StaticSnapshotOptions} from './static-snapshot-options' | ||
|
||
// Use this instead of importing PercyAgent - we only want the compiled version | ||
declare var PercyAgent: any | ||
|
||
export default class StaticSnapshotService { | ||
readonly options: StaticSnapshotOptions | ||
private readonly app: express.Application | ||
private server: Server | null = null | ||
|
||
constructor(options: StaticSnapshotOptions) { | ||
// logger.info('calling constructor...') | ||
this.app = express() | ||
this.options = options | ||
|
||
this.app.use(cors()) | ||
this.app.use(bodyParser.urlencoded({extended: true})) | ||
this.app.use(bodyParser.json({limit: '50mb'})) | ||
|
||
this.app.use(options.baseUrl, express.static(options.snapshotDirectory)) | ||
} | ||
|
||
async start() { | ||
// logger.info('starting static snapshot service...') | ||
logger.info(`serving static site at ${this._buildLocalUrl()}`) | ||
this.server = await this.app.listen(this.options.port) | ||
} | ||
|
||
async snapshotAll() { | ||
// logger.info('taking snapshots of the static site...') | ||
logger.debug('taking snapshots of static site') | ||
|
||
const browser = await puppeteer.launch({ | ||
args: ['--no-sandbox'], | ||
handleSIGINT : false, | ||
}) | ||
|
||
const percyAgentClientFilename = agentJsFilename() | ||
const page = await browser.newPage() | ||
|
||
const pageUrls = await this._buildPageUrls() | ||
|
||
for (const url of pageUrls) { | ||
logger.debug(`visiting ${url}`) | ||
|
||
await page.goto(url) | ||
|
||
await page.addScriptTag({ | ||
path: percyAgentClientFilename, | ||
}) | ||
|
||
await page.evaluate((name) => { | ||
const percyAgentClient = new PercyAgent() | ||
return percyAgentClient.snapshot(name) | ||
}, url) | ||
} | ||
|
||
browser.close() | ||
} | ||
|
||
async stop() { | ||
// logger.info('stopping static snapshot service...') | ||
if (this.server) { await this.server.close() } | ||
|
||
logger.info(`shutting down static site at ${this._buildLocalUrl()}`) | ||
} | ||
|
||
_buildLocalUrl() { | ||
return `http://localhost:${this.options.port}${this.options.baseUrl}` | ||
} | ||
|
||
async _buildPageUrls() { | ||
const baseUrl = this._buildLocalUrl() | ||
const pageUrls = [] as any | ||
|
||
const globOptions = { | ||
cwd: this.options.snapshotDirectory, | ||
ignore: this.options.ignoreGlobs, | ||
} | ||
|
||
const paths = await globby(this.options.snapshotGlobs, globOptions) | ||
|
||
for (const path of paths) { | ||
pageUrls.push(baseUrl + path) | ||
} | ||
|
||
return pageUrls | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/fixtures/services/static-snapshot-service/_dummy-testing-app/about-us.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is another dummy page. |
1 change: 1 addition & 0 deletions
1
test/fixtures/services/static-snapshot-service/_dummy-testing-app/blog/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a nested file for unit testing. |
1 change: 1 addition & 0 deletions
1
test/fixtures/services/static-snapshot-service/_dummy-testing-app/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a test file. |
1 change: 1 addition & 0 deletions
1
test/fixtures/services/static-snapshot-service/_dummy-testing-app/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// this is a dummy file that should not be picked up in testing |
1 change: 1 addition & 0 deletions
1
test/fixtures/services/static-snapshot-service/_dummy-testing-app/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* this is a dummy file that should not be picked up in testing */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.icon { | ||
width: 20px; | ||
height: 20px; | ||
} | ||
.icon.lg { | ||
width: 26px; | ||
height: 26px; | ||
} |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions
15
test/integration/test-static-site/families/greyjoy/dead.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Greyjoy Deceased List</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Deceased Greyjoys</strong></p> | ||
<ul> | ||
<li>Balon Greyjoy</li> | ||
</ul> | ||
<p><img src="/dummy-base-url/assets/images/red-x.png" class="icon"> <strong>percy-agent should not snapshot this page.</strong></p> | ||
</body> | ||
</html> |
17 changes: 17 additions & 0 deletions
17
test/integration/test-static-site/families/greyjoy/members.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Greyjoy Alive List</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Living Greyjoys</strong></p> | ||
<ul> | ||
<li>Theon Greyjoy</li> | ||
<li>Euron Greyjoy</li> | ||
<li>Yara Greyjoy - Unconfirmed</li> | ||
</ul> | ||
<p><img src="/dummy-base-url/assets/images/check.png" class="icon"> <strong>percy-agent should snapshot this page.</strong></p> | ||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
test/integration/test-static-site/families/greyjoy/pyke.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Pyke - Greyjoy Castle</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Pyke - Greyjoy Castle (via <a href="https://www.deviantart.com/feliche">feliche</a>)</strong></p> | ||
<p> | ||
<img src="/dummy-base-url/assets/images/pyke.jpg" alt=""> | ||
</p> | ||
<p><img src="/dummy-base-url/assets/images/check.png" class="icon"> <strong>percy-agent should snapshot this page.</strong></p> | ||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
test/integration/test-static-site/families/lannister/casterly-rock.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Casterly Rock - Lannister Castle</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Casterly Rock - Lannister Castle (via <a href="https://www.deviantart.com/feliche">feliche</a>)</strong></p> | ||
<p> | ||
<img src="/dummy-base-url/assets/images/casterly-rock.jpg" alt=""> | ||
</p> | ||
<p><img src="/dummy-base-url/assets/images/check.png" class="icon"> <strong>percy-agent should snapshot this page.</strong></p> | ||
</body> | ||
</html> |
18 changes: 18 additions & 0 deletions
18
test/integration/test-static-site/families/lannister/dead-members/list.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Lannister Deceased List</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Deceased Lannisters</strong></p> | ||
<p>Half of the Lannisters are dead. They're all pretty close anyway.</p> | ||
<ul> | ||
<li>Tywin Lannister</li> | ||
<li>Kevan Lannister</li> | ||
<li>Lancel Lannister</li> | ||
</ul> | ||
<p><img src="/dummy-base-url/assets/images/red-x.png" class="icon"> <strong>percy-agent should not snapshot this page.</strong></p> | ||
</body> | ||
</html> |
17 changes: 17 additions & 0 deletions
17
test/integration/test-static-site/families/lannister/members.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Lannister Alive List</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Living Lannisters</strong></p> | ||
<ul> | ||
<li>Cersei Lannister</li> | ||
<li>Jamie Lannister</li> | ||
<li>Tyrion Lannister</li> | ||
</ul> | ||
<p><img src="/dummy-base-url/assets/images/check.png" class="icon"> <strong>percy-agent should snapshot this page.</strong></p> | ||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
test/integration/test-static-site/families/stark/dead-members/list.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Stark Deceased List</title> | ||
<meta charset="utf-8"/> | ||
<link rel="stylesheet" href="/dummy-base-url/assets/css/app.css"> | ||
</head> | ||
<body> | ||
<p><strong>Deceased Starks</strong></p> | ||
<p>Half of the Starks are dead. The rest are pretty close.</p> | ||
<ul> | ||
<li>Ned Stark</li> | ||
<li>Catelyn Stark</li> | ||
<li>Robb Stark</li> | ||
<li>Rickon Stark</li> | ||
</ul> | ||
<p><img src="/dummy-base-url/assets/images/red-x.png" class="icon"> <strong>percy-agent should not snapshot this page.</strong></p> | ||
</body> | ||
</html> |
Oops, something went wrong.