Skip to content

Commit

Permalink
✨ Alias default exports with named exports (#667)
Browse files Browse the repository at this point in the history
* ♻ Add named aliases for default exports

* ✨ Re-export client request util from core

Packages that rely on `@percy/core` frequently need to perform some sort of request and utlize the
request util from `@percy/client` to do so. When linting for extraneous dependencies, these packages
need to specifically declare a direct dependency on `@percy/client`, which can cause unnecessary
dependency updates. This small inconvinience can be alleviated by re-exporting the request util from
`@percy/client` via `@percy/core` utils.

* ♻ Rename imported @percy/env class

* 💚 Increment CI cache-key for Windows
  • Loading branch information
Wil Wilsman authored Dec 13, 2021
1 parent 8db2346 commit 369ab32
Show file tree
Hide file tree
Showing 61 changed files with 205 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .github/.cache-key
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
; ;
/ \
_____________/_ __ \_____________
Times we have broken CI: 2
Times we have broken CI: 3
4 changes: 3 additions & 1 deletion packages/cli-command/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import logger from '@percy/logger';
// The PercyCommand class that all Percy CLI commands should extend
// from. Provides common #init() and #catch() methods and provides other methods
// for loading configuration and checking if Percy is enabled.
export default class PercyCommand extends Command {
export class PercyCommand extends Command {
log = logger('cli:command');

// Initialize flags, args, the loglevel, and attach process handlers to allow
Expand Down Expand Up @@ -86,3 +86,5 @@ export default class PercyCommand extends Command {
});
}
}

export default PercyCommand;
2 changes: 1 addition & 1 deletion packages/cli-command/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default } from './command';
export { default, PercyCommand } from './command';
export { default as flags } from './flags';
2 changes: 1 addition & 1 deletion packages/cli-config/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ beforeEach(() => {
});

export { logger };
export { default as mockConfig, getMockConfig } from '@percy/config/test/helpers';
export { mockConfig, getMockConfig } from '@percy/config/test/helpers';
2 changes: 1 addition & 1 deletion packages/cli-exec/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ afterEach(() => {
});

export { logger, mockAPI };
export { default as createTestServer } from '@percy/core/test/helpers/server';
export { createTestServer } from '@percy/core/test/helpers/server';
4 changes: 3 additions & 1 deletion packages/cli-upload/src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function createImageResource(url, content, mimetype) {
// Returns root resource and image resource objects based on an image's
// filename, contents, and dimensions. The root resource is a generated DOM
// designed to display an image at it's native size without margins or padding.
export default function createImageResources(filename, content, width, height) {
export function createImageResources(filename, content, width, height) {
let { dir, name, ext } = path.parse(filename);
let rootUrl = `/${encodeURIComponent(path.join(dir, name))}`;
let imageUrl = `/${encodeURIComponent(filename)}`;
Expand All @@ -52,3 +52,5 @@ export default function createImageResources(filename, content, width, height) {
createImageResource(imageUrl, content, mimetype)
];
}

export default createImageResources;
8 changes: 5 additions & 3 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PercyEnvironment from '@percy/env';
import PercyEnv from '@percy/env';
import { git } from '@percy/env/dist/utils';
import logger from '@percy/logger';
import pkg from '../package.json';
Expand Down Expand Up @@ -28,9 +28,9 @@ function validateProjectPath(path) {
// PercyClient is used to communicate with the Percy API to create and finalize
// builds and snapshot. Uses @percy/env to collect environment information used
// during build creation.
export default class PercyClient {
export class PercyClient {
log = logger('client');
env = new PercyEnvironment(process.env);
env = new PercyEnv(process.env);
clientInfo = new Set();
environmentInfo = new Set();

Expand Down Expand Up @@ -349,3 +349,5 @@ export default class PercyClient {
return snapshot;
}
}

export default PercyClient;
2 changes: 1 addition & 1 deletion packages/client/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './client';
export { default, PercyClient } from './client';
4 changes: 3 additions & 1 deletion packages/client/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function proxyAgentFor(url, options) {
// default, and 404 errors may also be optionally retried. If a callback is provided, it is called
// with the parsed response body and response details. If the callback returns a value, that value
// will be returned in the final resolved promise instead of the response body.
export default function request(url, options = {}, callback) {
export function request(url, options = {}, callback) {
// accept `request(url, callback)`
if (typeof options === 'function') [options, callback] = [{}, options];
let { body, retries, retryNotFound, interval, noProxy, ...requestOptions } = options;
Expand Down Expand Up @@ -255,3 +255,5 @@ export default function request(url, options = {}, callback) {
req.end(body);
}, { retries, interval });
}

export default request;
4 changes: 3 additions & 1 deletion packages/config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ function getDefaultsFromSchema(schema) {
}
}

export default function getDefaults(overrides = {}) {
export function getDefaults(overrides = {}) {
return merge([getDefaultsFromSchema(), overrides], (path, prev, next) => {
// override default array instead of merging
return isArray(next) && [path, next];
});
}

export default getDefaults;
18 changes: 17 additions & 1 deletion packages/config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ import getDefaults from './defaults';
import normalize from './normalize';
import stringify from './stringify';

// Export a single object that can be imported as PercyConfig
export {
load,
search,
cache,
explorer,
validate,
addSchema,
resetSchema,
migrate,
addMigration,
clearMigrations,
getDefaults,
normalize,
stringify
};

// mirror the namespace as the default export
export default {
load,
search,
Expand Down
4 changes: 3 additions & 1 deletion packages/config/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function search(path) {
// unless `bail` is true. Supports kebab-case and camelCase config options and
// always returns camelCase options. Will automatically convert older config
// versions to the latest version while printing a warning.
export default function load({
export function load({
path,
overrides = {},
reload = false,
Expand Down Expand Up @@ -107,3 +107,5 @@ export default function load({
// merge with defaults
return getDefaults(config);
}

export default load;
4 changes: 3 additions & 1 deletion packages/config/src/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function deprecate(config, log, path, options) {

// Calls each registered migration function with a normalize provided config
// and util functions for working with the config object
export default function migrate(config, schema = '/config') {
export function migrate(config, schema = '/config') {
config = normalize(config, { schema }) ?? {};

if (migrations.has(schema)) {
Expand All @@ -77,3 +77,5 @@ export default function migrate(config, schema = '/config') {

return config;
}

export default migrate;
8 changes: 5 additions & 3 deletions packages/config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CAMELCASE_MAP = new Map([
// Converts kebab-cased and snake_cased strings to camelCase.
const KEBAB_SNAKE_REG = /[-_]([^-_]+)/g;

function camelcase(str) {
export function camelcase(str) {
if (typeof str !== 'string') return str;

return str.replace(KEBAB_SNAKE_REG, (match, word) => (
Expand All @@ -21,7 +21,7 @@ function camelcase(str) {
// Coverts camelCased and snake_cased strings to kebab-case.
const CAMEL_SNAKE_REG = /([a-z])([A-Z]+)|_([^_]+)/g;

function kebabcase(str) {
export function kebabcase(str) {
if (typeof str !== 'string') return str;

return Array.from(CAMELCASE_MAP)
Expand All @@ -36,7 +36,7 @@ function kebabcase(str) {
// Removes undefined empty values and renames kebab-case properties to camelCase. Optionally
// allows deep merging with options.overrides, converting keys to kebab-case with options.kebab,
// and normalizing against a schema with options.schema.
export default function normalize(object, options) {
export function normalize(object, options) {
let keycase = options?.kebab ? kebabcase : camelcase;

return merge([object, options?.overrides], path => {
Expand All @@ -52,3 +52,5 @@ export default function normalize(object, options) {
return [path];
});
}

export default normalize;
4 changes: 3 additions & 1 deletion packages/config/src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function inspect(config) {

// Converts a config to a yaml, json, or js string. When no config is provided,
// falls back to schema defaults.
export default function stringify(format, config = getDefaults()) {
export function stringify(format, config = getDefaults()) {
switch (format) {
case 'yml':
case 'yaml':
Expand All @@ -22,3 +22,5 @@ export default function stringify(format, config = getDefaults()) {
throw new Error(`Unsupported format: ${format}`);
}
}

export default stringify;
4 changes: 3 additions & 1 deletion packages/config/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function shouldHideError({ parentSchema, keyword, schemaPath }) {
}

// Validates data according to the associated schema and returns a list of errors, if any.
export default function validate(data, key = '/config') {
export function validate(data, key = '/config') {
if (!ajv.validate(key, data)) {
let errors = new Map();

Expand Down Expand Up @@ -214,3 +214,5 @@ export default function validate(data, key = '/config') {
return Array.from(errors.values());
}
}

export default validate;
4 changes: 3 additions & 1 deletion packages/config/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mock from 'mock-require';

export const configs = new Map();

export default function mockConfig(f, c) {
export function mockConfig(f, c) {
configs.set(f, () => typeof c === 'function' ? c() : c);
}

Expand Down Expand Up @@ -45,3 +45,5 @@ afterAll(() => {
afterEach(() => {
configs.clear();
});

export default mockConfig;
4 changes: 3 additions & 1 deletion packages/core/src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import install from './install';
import Session from './session';
import Page from './page';

export default class Browser extends EventEmitter {
export class Browser extends EventEmitter {
log = logger('core:browser');
sessions = new Map();
readyState = null;
Expand Down Expand Up @@ -315,3 +315,5 @@ export default class Browser extends EventEmitter {
}
}
}

export default Browser;
15 changes: 8 additions & 7 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export const snapshotDOMSchema = {
}
};

// Grouped schemas for easier registration
export const schemas = [
configSchema,
snapshotSchema,
snapshotDOMSchema
];

// Config migrate function
export function configMigration(config, util) {
/* eslint-disable curly */
Expand Down Expand Up @@ -248,13 +255,7 @@ export function snapshotMigration(config, util) {
util.deprecate('snapshots', { map: 'additionalSnapshots', ...notice });
}

// Convinient references for schema registrations
export const schemas = [
configSchema,
snapshotSchema,
snapshotDOMSchema
];

// Grouped migrations for easier registration
export const migrations = [
['/config', configMigration],
['/snapshot', snapshotMigration]
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Register core config options
const { default: PercyConfig } = require('@percy/config');
const PercyConfig = require('@percy/config');
const CoreConfig = require('./config');
const { Percy } = require('./percy');

PercyConfig.addSchema(CoreConfig.schemas);
PercyConfig.addMigration(CoreConfig.migrations);

// Export the Percy class with commonjs compatibility
module.exports = require('./percy').default;
// export the Percy class with commonjs compatibility
module.exports = Percy;
module.exports.Percy = Percy;
4 changes: 3 additions & 1 deletion packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

// The Interceptor class creates common handlers for dealing with intercepting asset requests
// for a given page using various devtools protocol events and commands.
export default class Network {
export class Network {
static TIMEOUT = 30000;

log = logger('core:network');
Expand Down Expand Up @@ -261,3 +261,5 @@ export default class Network {
this._forgetRequest(request);
}
}

export default Network;
4 changes: 3 additions & 1 deletion packages/core/src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
waitFor
} from './utils';

export default class Page {
export class Page {
static TIMEOUT = 30000;

log = logger('core:page');
Expand Down Expand Up @@ -265,3 +265,5 @@ export default class Page {
this.contextId = null;
}
}

export default Page;
4 changes: 3 additions & 1 deletion packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
// creation, asset discovery, and resource uploads, and will finalize the build
// when stopped. Snapshots are processed concurrently and the build is not
// finalized until all snapshots have been handled.
export default class Percy {
export class Percy {
log = logger('core');
readyState = null;

Expand Down Expand Up @@ -375,3 +375,5 @@ export default class Percy {
});
}
}

export default Percy;
4 changes: 3 additions & 1 deletion packages/core/src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
waitFor
} from './utils';

export default class Queue {
export class Queue {
running = true;
closed = false;

Expand Down Expand Up @@ -148,3 +148,5 @@ export default class Queue {
}
}
}

export default Queue;
4 changes: 3 additions & 1 deletion packages/core/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function createServer(routes) {
return context;
}

export default function createPercyServer(percy) {
export function createPercyServer(percy) {
let log = logger('core:server');

let context = createServer({
Expand Down Expand Up @@ -173,3 +173,5 @@ export default function createPercyServer(percy) {

return context;
}

export default createPercyServer;
4 changes: 3 additions & 1 deletion packages/core/src/session.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventEmitter from 'events';
import logger from '@percy/logger';

export default class Session extends EventEmitter {
export class Session extends EventEmitter {
#callbacks = new Map();

log = logger('core:session');
Expand Down Expand Up @@ -92,3 +92,5 @@ export default class Session extends EventEmitter {
}
}
}

export default Session;
1 change: 1 addition & 0 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sha256hash, hostnameMatches } from '@percy/client/dist/utils';
export { request } from '@percy/client/dist/request';
export { hostnameMatches };

// Returns the hostname portion of a URL.
Expand Down
Loading

0 comments on commit 369ab32

Please sign in to comment.