Skip to content

Commit

Permalink
Merge pull request #21 from percy/json-output
Browse files Browse the repository at this point in the history
Add output_format arg, that provides for JSON output of build status
  • Loading branch information
timhaines authored Dec 13, 2017
2 parents 4b87073 + 7838d1b commit dffc03e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getMinimumHeight from '../getMinimumHeight';

it('returns number when passed a number ', () => {
it('returns number when passed a number', () => {
expect(getMinimumHeight(100)).toEqual(100);
});

Expand Down
18 changes: 18 additions & 0 deletions packages/percy-storybook/src/__tests__/getOutputFormat-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getOutputFormat from '../getOutputFormat';

it('returns text when passed text', () => {
expect(getOutputFormat('TEXT')).toEqual('text');
expect(getOutputFormat('text')).toEqual('text');
});

it('returns json when passed JSON', () => {
expect(getOutputFormat('JSON')).toEqual('json');
expect(getOutputFormat('json')).toEqual('json');
});

it('raises an error if called with an invalid value', () => {
expect(() => getOutputFormat()).toThrow();
expect(() => getOutputFormat('xml')).toThrow();
expect(() => getOutputFormat(7)).toThrow();
expect(() => getOutputFormat('')).toThrow();
});
4 changes: 4 additions & 0 deletions packages/percy-storybook/src/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const options = {
description: 'Directory to load the static storybook built by build-storybook from',
requiresArg: true,
},
output_format: {
description: 'Specify JSON to log the build parameters in JSON. Note: --debug outputs non-JSON',
requiresArg: true,
},
rtl: {
description: 'Runs stories a second time with a direction parameter set to rtl',
requiresArg: false,
Expand Down
36 changes: 31 additions & 5 deletions packages/percy-storybook/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getStories from './getStories';
import getStaticAssets from './getStaticAssets';
import getWidths from './getWidths';
import getMinimumHeight from './getMinimumHeight';
import getOutputFormat from './getOutputFormat';
import getRtlRegex from './getRtlRegex';
import selectStories from './selectStories';
import uploadStorybook from './uploadStorybook';
Expand All @@ -27,6 +28,7 @@ export async function run(argv) {
.options(args.options)
.epilogue(args.docs)
.default('build_dir', 'storybook-static')
.default('output_format', 'text')
.default('minimum_height', '800').argv;

if (argv.help) {
Expand All @@ -44,13 +46,23 @@ export async function run(argv) {
const rtlRegex = getRtlRegex(argv.rtl, argv.rtl_regex);

const options = {
debug: argv.debug,
// Configure debug logging if flag specified, or if it was already enabled via DEBUG env var
debug: argv.debug || debug.enabled,
buildDir: argv.build_dir,
outputFormat: getOutputFormat(argv.output_format),
};

// Enable debug logging based on options.
debug.enabled = options.debug;

if (process.env.PERCY_ENABLE === '0') {
// eslint-disable-next-line no-console
console.log('The PERCY_ENABLE environment variable is set to 0. Exiting.');
if (options.outputFormat == 'text') {
// eslint-disable-next-line no-console
console.log('The PERCY_ENABLE environment variable is set to 0. Exiting.');
} else if (options.outputFormat == 'json') {
// eslint-disable-next-line no-console
console.log(`{'exitReason':'The PERCY_ENABLE environment variable is set to 0.'}`);
}
return;
}

Expand All @@ -72,7 +84,13 @@ export async function run(argv) {
debug('selectedStories %o', selectedStories);

if (selectedStories.length === 0) {
console.log('WARNING: No stories were found.'); // eslint-disable-line no-console
if (options.outputFormat == 'text') {
// eslint-disable-next-line no-console
console.log('No stories were found.');
} else if (options.outputFormat == 'json') {
// eslint-disable-next-line no-console
console.log(`{'exitReason':'No stories were found.'}`);
}
return;
}

Expand All @@ -83,5 +101,13 @@ export async function run(argv) {
`storybook/${storybookVersion()} react/${reactVersion()}`,
);

return uploadStorybook(client, selectedStories, widths, minimumHeight, storyHtml, assets);
return uploadStorybook(
client,
selectedStories,
widths,
minimumHeight,
storyHtml,
assets,
options.outputFormat,
);
}
11 changes: 11 additions & 0 deletions packages/percy-storybook/src/getOutputFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function getOutputFormat(outputFormatString) {
const normalizedOutputFormat = outputFormatString.toLowerCase();

if (normalizedOutputFormat != 'text' && normalizedOutputFormat != 'json') {
throw new Error(
`Output format must be either 'text' or 'json'. Received: ${outputFormatString}`,
);
}

return normalizedOutputFormat;
}
7 changes: 6 additions & 1 deletion packages/percy-storybook/src/getStaticAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ function gatherBuildResources(buildDir) {
resourceUrl = resourceUrl.replace(/\\/g, '/');
}

resourceUrl = resourceUrl.replace(buildDir, '');
// Strip the buildDir from the start of the resourceUrl
if (buildDir.startsWith('./')) {
resourceUrl = resourceUrl.replace(buildDir.substr(2), '');
} else {
resourceUrl = resourceUrl.replace(buildDir, '');
}

// Remove the leading /
if (resourceUrl.charAt(0) === '/') {
Expand Down
23 changes: 16 additions & 7 deletions packages/percy-storybook/src/uploadStorybook.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ export default async function uploadStorybook(
minimumHeight,
storyHtml,
assets,
outputFormat,
) {
const snapshotPluralization = selectedStories.length === 1 ? 'snapshot' : 'snapshots';
// eslint-disable-next-line no-console
console.log('\nUploading', selectedStories.length, snapshotPluralization, 'to Percy.');

if (outputFormat == 'text') {
// eslint-disable-next-line no-console
console.log('\nUploading', selectedStories.length, snapshotPluralization, 'to Percy.');
}

const resources = client.makeResources(assets);
const build = await client.createBuild(resources);
Expand All @@ -19,9 +23,14 @@ export default async function uploadStorybook(
await uploadStories(client, build, selectedStories, widths, minimumHeight, assets, storyHtml);
await client.finalizeBuild(build);

// eslint-disable-next-line no-console
console.log(
'Percy snapshots uploaded. Visual diffs are now processing:',
build.attributes['web-url'],
);
if (outputFormat == 'json') {
// eslint-disable-next-line no-console
console.log(JSON.stringify(build.attributes));
} else if (outputFormat == 'text') {
// eslint-disable-next-line no-console
console.log(
'Percy snapshots uploaded. Visual diffs are now processing:',
build.attributes['web-url'],
);
}
}

0 comments on commit dffc03e

Please sign in to comment.