Skip to content

Commit

Permalink
Incorporate feedback and add check for base-url slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
maprules1000 committed Apr 16, 2019
1 parent cd99901 commit 8181cf7
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/commands/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Constants from '../services/constants'
import {StaticSnapshotOptions} from '../services/static-snapshot-options'
import StaticSnapshotService from '../services/static-snapshot-service'
import PercyCommand from './percy-command'
import logger from '../utils/logger';

export default class Snapshot extends PercyCommand {
static description = 'Snapshot a directory of webpages'
Expand All @@ -16,7 +17,7 @@ export default class Snapshot extends PercyCommand {

static examples = [
'$ percy snapshot _site/',
'$ percy snapshot _site/ --base-url "blog/"',
'$ percy snapshot _site/ --base-url "/blog/"',
'$ percy snapshot _site/ --ignore-files "\.(blog|docs)$"',
]

Expand All @@ -33,8 +34,7 @@ export default class Snapshot extends PercyCommand {
}),
'base-url': flags.string({
char: 'b',
description: 'The path that the site will be deployed to on a production server. \
Use this if your site will be hosted at a non-root url.',
description: 'If your static files will be hosted in a subdirectory, instead of the webserver\'s root path, set that subdirectory with this flag.',
default: '/',
}),
// from exec command. needed to start the agent service.
Expand All @@ -55,6 +55,8 @@ export default class Snapshot extends PercyCommand {

const {args, flags} = this.parse(Snapshot)

const isWindows = process.platform.includes("win")

const snapshotDirectory = args.snapshotDirectory as string
const port = flags.port as number
const staticServerPort = port + 1
Expand All @@ -66,6 +68,14 @@ export default class Snapshot extends PercyCommand {
// exit gracefully if percy will not run
if (!this.percyWillRun()) { this.exit(0) }

// check that base url starts with a slash and exit if it is missing
if (isWindows) {
this.checkForWrappingSlashes(baseUrl, '\\')
} else {
this.checkForWrappingSlashes(baseUrl, '/')
}


// start the agent service
await this.agentService.start({port, networkIdleTimeout})
this.logStart()
Expand All @@ -90,4 +100,11 @@ export default class Snapshot extends PercyCommand {
await staticSnapshotService.stop()
await this.agentService.stop()
}

private checkForWrappingSlashes(url: string, slash: string) {
if (url[0] != slash || url[url.length - 1] != slash) {
logger.warn('The base-url flag must begin and end with a slash.')
this.exit(1)
}
}
}

0 comments on commit 8181cf7

Please sign in to comment.