Skip to content

Commit

Permalink
🐛 Fix config normalization and migration (#166)
Browse files Browse the repository at this point in the history
* 🐛 Fix config key normalization

The previous regex was only matching the first occurrence of a dash or underscore. This new regex
will match even mixed sytaxes

* 🐛 Register core config migration for the migrate command

Since this command doesn't include core directly, core's config schema and migration are never
registered. We were already registering the schema, but also need to register the migration

* 🚚 Move core config registration to cli-exec

Core config registration is needed when running config:migrate, however cli-exec already requires
@percy/core but relies on the package to register itself. It makes more sense to require cli-exec do
the registration rather than have cli-config require core only for registration.
  • Loading branch information
Wil Wilsman authored Feb 5, 2021
1 parent 8495d98 commit 37f2b0b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
4 changes: 0 additions & 4 deletions packages/cli-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"oclif": {
"bin": "percy",
"commands": "./dist/commands",
"hooks": {
"init": "./dist/hooks/init"
},
"topics": {
"config": {
"description": "manage Percy config files"
Expand All @@ -40,7 +37,6 @@
"@oclif/command": "^1.8.0",
"@oclif/config": "^1.17.0",
"@percy/config": "^1.0.0-beta.33",
"@percy/core": "^1.0.0-beta.33",
"@percy/logger": "^1.0.0-beta.33"
}
}
7 changes: 0 additions & 7 deletions packages/cli-config/src/hooks/init.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/cli-exec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"oclif": {
"bin": "percy",
"commands": "./dist/commands",
"hooks": {
"init": "./dist/hooks/init"
},
"topics": {
"exec": {
"description": "capture and upload snapshots"
Expand Down
8 changes: 8 additions & 0 deletions packages/cli-exec/src/hooks/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import PercyConfig from '@percy/config';
import { schema, migration } from '@percy/core/dist/config';

// ensures the core schema and migration is loaded
export default function() {
PercyConfig.addSchema(schema);
PercyConfig.addMigration(migration);
}
4 changes: 2 additions & 2 deletions packages/config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const CAMELIZE_MAP = {
};

// Converts kebab-cased and snake_cased strings to camelCase.
const KEBAB_SNAKE_REG = /(-|_)([^\1]+)/g;
const KEBAB_SNAKE_REG = /[-_]([^-_]+)/g;

function camelize(str) {
return str.replace(KEBAB_SNAKE_REG, (match, sep, word) => (
return str.replace(KEBAB_SNAKE_REG, (match, word) => (
CAMELIZE_MAP[word] || (word[0].toUpperCase() + word.slice(1))
));
}
Expand Down
25 changes: 25 additions & 0 deletions packages/config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,31 @@ describe('PercyConfig', () => {
});
});

describe('.normalize()', () => {
it('removes empty values', () => {
expect(PercyConfig.normalize({
foo: 'bar',
arr: [{}, {}],
obj: { val: undefined },
nested: { arr: [undefined] }
})).toEqual({
foo: 'bar'
});
});

it('converts keys to camelCase', () => {
expect(PercyConfig.normalize({
'foo-bar': 'baz',
foo: { bar_baz: 'qux' },
'foo_bar-baz': 'qux'
})).toEqual({
fooBar: 'baz',
foo: { barBaz: 'qux' },
fooBarBaz: 'qux'
});
});
});

describe('.stringify()', () => {
it('formats the default config', () => {
expect(PercyConfig.stringify('json')).toBe([
Expand Down

0 comments on commit 37f2b0b

Please sign in to comment.