@@ -2,32 +2,16 @@ import * as child_process from "child_process";
22import * as fs from "fs" ;
33import * as url from "url" ;
44
5- import * as opn from "opn"
5+ import * as opn from "opn" ;
66
77import * as util from "./util" ;
88
99import { config , IConfig } from "./visreg.config" ;
1010const screenshotsDirectory = "visreg/screenshots" ;
11+ const repoUrl = url . parse ( `https://${ config . githubHostname } /${ config . githubName } /${ config . repo } ` ) ;
1112
12- async function visreg (
13- currentBranch : string ,
14- targetBranch ?: string ,
15- ) : Promise < void > {
16- if ( config . githubHostname === "github.rackspace.com" ) {
17- process . stdout . write ( "Checking connection to VPN..." ) ;
18- try {
19- child_process . execSync ( "ping -t 3 -c 1 rax.io" ) . toString ( ) . match ( / 1 p a c k e t s t r a n s m i t t e d , 1 p a c k e t s r e c e i v e d / ) ;
20- } catch ( e ) {
21- console . log ( " ✘" ) ;
22- console . log ( "Check your VPN connection and try again." ) ;
23- throw new Error ( e ) ;
24- }
25- console . log ( " ✔" ) ;
26- }
27-
28- const branch = await util . getBranchName ( targetBranch ) ;
29- const token = await util . validateToken ( await util . getGithubToken ( ) ) ;
30- const repoUrl = url . parse ( `https://${ config . githubHostname } /${ config . githubName } /${ config . repo } ` ) ;
13+ async function prepareRepository ( token : string ) {
14+ util . validateVPN ( config . githubHostname ) ;
3115
3216 util . resetRepository ( screenshotsDirectory ) ;
3317 if ( ! util . repositoryExists ( token , repoUrl ) ) {
@@ -38,6 +22,15 @@ async function visreg(
3822 if ( ! fs . existsSync ( `${ screenshotsDirectory } /.git` ) ) {
3923 util . cloneRepo ( token , screenshotsDirectory , repoUrl ) ;
4024 }
25+ }
26+
27+ async function visreg (
28+ currentBranch : string ,
29+ targetBranch ?: string ,
30+ ) : Promise < void > {
31+ const token = await util . validateToken ( await util . getGithubToken ( ) ) ;
32+ await prepareRepository ( token ) ;
33+ const branch = await util . getBranchName ( targetBranch ) ;
4134
4235 const anonymousBranch = `anon-${ new Date ( ) . valueOf ( ) } ` ;
4336 console . log ( "Creating a new baseline..." ) ;
@@ -50,15 +43,46 @@ async function visreg(
5043 opn ( remoteUrl ) ;
5144}
5245
46+ /*
47+ * This exists as a way of generating a one-off baseline. Why?
48+ * So you can share the current state of a given branch easily.
49+ * Run this with `yarn run visreg share`. It'll commit just the current screenshots,
50+ * and push them up as a singular commit reference and open the commit in your browser.
51+ */
52+ async function visregNoDiff ( currentBranch : string ) {
53+ const token = await util . validateToken ( await util . getGithubToken ( ) ) ;
54+ await prepareRepository ( token ) ;
55+
56+ const anonymousBranch = `anon-${ new Date ( ) . valueOf ( ) } ` ;
57+ console . log ( "Creating a new baseline..." ) ;
58+ const baseCommit = util . createBaseline ( token , anonymousBranch , screenshotsDirectory , currentBranch ) ;
59+
60+ util . pushBranch ( token , repoUrl , anonymousBranch , screenshotsDirectory ) ;
61+ const remoteUrl = `${ repoUrl . href } /commit/${ baseCommit } ` ;
62+ console . log ( `Opening remote screenshot diff located at: ${ remoteUrl } ` ) ;
63+ opn ( remoteUrl ) ;
64+ }
65+
5366if ( require . main === module ) {
54- const branch = Array . prototype . slice . call ( process . argv , 2 ) [ 0 ] ;
67+ const arg = Array . prototype . slice . call ( process . argv , 2 ) [ 0 ] ;
5568 const currentBranch = child_process . execSync ( "git rev-parse --abbrev-ref HEAD" ) . toString ( ) . trim ( ) ;
69+ let share = "share" . indexOf ( arg ) === 0 ;
70+
71+ if ( share ) {
72+ visregNoDiff ( currentBranch ) . then ( ( ) => { process . exit ( 0 ) ; } )
73+ . catch ( err => {
74+ console . log ( err . message ) ;
75+ process . exit ( 1 ) ;
76+ } ) ;
77+ } else {
78+ const branch = arg ;
5679
57- visreg ( currentBranch , branch )
58- . then ( ( ) => { process . exit ( 0 ) ; } )
59- . catch ( err => {
60- child_process . execSync ( `git checkout ${ currentBranch } > /dev/null 2>&1` ) ;
61- console . log ( err . message ) ;
62- process . exit ( 1 ) ;
63- } ) ;
80+ visreg ( currentBranch , branch )
81+ . then ( ( ) => { process . exit ( 0 ) ; } )
82+ . catch ( err => {
83+ child_process . execSync ( `git checkout ${ currentBranch } > /dev/null 2>&1` ) ;
84+ console . log ( err . message ) ;
85+ process . exit ( 1 ) ;
86+ } ) ;
87+ }
6488} ;
0 commit comments