From 8181cf7b066309acb10acc3480891d5ccff39b84 Mon Sep 17 00:00:00 2001 From: Michael Padon Date: Tue, 16 Apr 2019 10:52:16 -0500 Subject: [PATCH] Incorporate feedback and add check for base-url slashes --- src/commands/snapshot.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/commands/snapshot.ts b/src/commands/snapshot.ts index 213a4ed9..65dce847 100644 --- a/src/commands/snapshot.ts +++ b/src/commands/snapshot.ts @@ -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' @@ -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)$"', ] @@ -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. @@ -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 @@ -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() @@ -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) + } + } }