Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Jan 30, 2020
0 parents commit a339b90
Show file tree
Hide file tree
Showing 11 changed files with 2,460 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12]
steps:
- uses: actions/checkout@v2
- run: yarn
- run: yarn test
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12.x'
registry-url: 'https://registry.npmjs.org'
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Perceptual Inc.

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# gatsby-plugin-percy

A Gatsby plugin for automatically taking Percy snapshots after a build.

### Dependencies

- [@percy/agent](https://github.com/percy/percy-agent)

## Install

```shell
$ npm install --save @percy/agent gatsby-plugin-percy
```

## Usage

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-percy`,
options: {
files: [`dir/*.html`],
ignore: [`ignore/*.html*`],
config: `config/.percy.yaml`,
},
},
],
}
```

## Options

Options are not required. Percy searches the current working directory and up
for a [matching config file](https://docs.percy.io/docs/sdk-configuration#section-configuration-files).
The `files` and `ignore` options can be specified within a
[`static-snapshots` section](https://docs.percy.io/docs/sdk-configuration#section-static-snapshots)
of your Percy config file as `snapshot-files` and `ignore-files` respectively.

- `files` - A single file glob or array of file globs to snapshot within the `public` directory
- `ignore` - A single file glob or array of file globs to ignore within the `public` directory
- `config` - Path to a [Percy config file](https://docs.percy.io/docs/sdk-configuration)
10 changes: 10 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Releasing

1. Create a new tag for the version `git tag vX.X.X`
2. Push the tag to GitHub `git push --tags`
3. Ensure tests have passed on that tag
4. [Create a new release](https://github.com/percy/gatsby-plugin-percy/releases) on GitHub
5. Wait for the publish workflow to complete (triggered via a new release)
6. [Visit npm](https://www.npmjs.com/package/gatsby-plugin-percy) and see the new version is live

* Announce the new release, making sure to say "thank you" to the contributors who helped shape this version!
22 changes: 22 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path')
const { execSync } = require('child_process')
const { isArray } = Array

exports.onPostBuild = ({ store }, { files, ignore, config }) => {
let { program: { useYarn, directory } } = store.getState()
let cmd = [useYarn ? 'yarn' : 'npm', 'percy', 'snapshot', path.resolve(directory, 'public')]

if (files) {
cmd = cmd.concat('--snapshot-files', [].concat(files).join(','))
}

if (ignore) {
cmd = cmd.concat('--ignore-files', [].concat(ignore).join(','))
}

if (config) {
cmd = cmd.concat('--config', config)
}

execSync(cmd.join(' '), { stdio: 'inherit' })
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// no-op
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "gatsby-plugin-percy",
"version": "0.1.0",
"description": "A Gatsby plugin for automatically taking Percy snapshots after a build.",
"repository": "github:percy/gatsby-plugin-percy",
"author": "Perceptual Inc.",
"license": "MIT",
"keywords": [
"gatsby",
"gatsby-plugin",
"percy",
"visual",
"screenshot"
],
"scripts": {
"test": "mocha test.js"
},
"dependencies": {
"unist-util-visit": "^1.4.0"
},
"peerDependencies": {
"@percy/agent": "^0.20.0"
},
"devDependencies": {
"expect": "^25.1.0",
"mocha": "^7.0.0",
"mock-require": "^3.0.0"
}
}
86 changes: 86 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const expect = require('expect')
const mock = require('mock-require')

function execSyncMock(...args) {
(execSyncMock.calls = execSyncMock.calls || []).push(args)
}

mock('child_process', { execSync: execSyncMock })
// we don't need to actually resolve paths for tests
mock('path', { resolve: (...p) => p.filter(Boolean).join('/') })
// required after mocking so we don't have to re-require
const { onPostBuild } = require('./gatsby-node')

describe('onPostBuild', () => {
let program, store = { getState: () => ({ program }) }

beforeEach(() => {
delete execSyncMock.calls
program = {}
})

it('calls `percy snapshot` with npm using the public path', () => {
onPostBuild({ store }, {})
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('npm percy snapshot public')
})

it('calls `percy snapshot` with yarn using the public path', () => {
program.useYarn = true
onPostBuild({ store }, {})
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('yarn percy snapshot public')
})

it('calls `percy snapshot` with the resolved public path', () => {
program.directory = 'some/project/path'
onPostBuild({ store }, {})
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('npm percy snapshot some/project/path/public')
})

it('calls `percy snapshot` with the `snapshot-files` option', () => {
onPostBuild({ store }, { files: '*.html' })
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('npm percy snapshot public --snapshot-files *.html')

onPostBuild({ store }, { files: ['*.html', '*.htm'] })
expect(execSyncMock.calls).toHaveLength(2)
expect(execSyncMock.calls[1][0]).toBe('npm percy snapshot public --snapshot-files *.html,*.htm')
})

it('calls `percy snapshot` with the `ignore-files` option', () => {
onPostBuild({ store }, { ignore: '*.html' })
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('npm percy snapshot public --ignore-files *.html')

onPostBuild({ store }, { ignore: ['*.html', '*.htm'] })
expect(execSyncMock.calls).toHaveLength(2)
expect(execSyncMock.calls[1][0]).toBe('npm percy snapshot public --ignore-files *.html,*.htm')
})

it('calls `percy snapshot` with the `config` option', () => {
onPostBuild({ store }, { config: 'path/to/.percy.yml' })
expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe('npm percy snapshot public --config path/to/.percy.yml')
})

it('calls `percy snapshot` with multiple options', () => {
program.directory = 'some/project/path'
program.useYarn = true

onPostBuild({ store }, {
files: ['*.html'],
ignore: ['*.htm'],
config: 'path/to/.percy.yml'
})

expect(execSyncMock.calls).toHaveLength(1)
expect(execSyncMock.calls[0][0]).toBe([
'yarn percy snapshot some/project/path/public',
'--snapshot-files *.html',
'--ignore-files *.htm',
'--config path/to/.percy.yml'
].join(' '))
})
})
Loading

0 comments on commit a339b90

Please sign in to comment.