Skip to content

Commit

Permalink
feat: add support for fields CLI option (#963)
Browse files Browse the repository at this point in the history
* add fields cli

* Update src/options.ts

Co-authored-by: Emilio Alvarez Piñeiro <95703246+emilioalvap@users.noreply.github.com>

* Update src/options.ts

* Update src/options.ts

* wip

* added some unit test

---------

Co-authored-by: Emilio Alvarez Piñeiro <95703246+emilioalvap@users.noreply.github.com>
  • Loading branch information
shahzad31 and emilioalvap authored Oct 2, 2024
1 parent c730775 commit 10be171
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe('CLI', () => {
}
});

it('run expect assetions with type check', async () => {
it('run expect assertions with type check', async () => {
// flag turns on type checking
process.env['TS_NODE_TYPE_CHECK'] = 'true';
const cli = new CLIMock()
Expand Down
3 changes: 3 additions & 0 deletions __tests__/fixtures/synthetics.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module.exports = env => {
enabled: false,
},
},
fields: {
area: 'website',
},
},
};
if (env !== 'development' && config.params) {
Expand Down
10 changes: 10 additions & 0 deletions __tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ describe('options', () => {
enabled: false,
},
},
fields: {
area: 'website',
},
});

expect(
Expand All @@ -101,6 +104,9 @@ describe('options', () => {
screenshots: 'only-on-failure',
locations: ['australia_east'],
privateLocations: ['test'],
fields: {
env: 'dev',
},
},
'push'
)
Expand All @@ -118,6 +124,10 @@ describe('options', () => {
enabled: false,
},
},
fields: {
env: 'dev',
area: 'website',
},
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const {
configOpt,
tags,
match,
fields,
} = getCommonCommandOpts();

program
Expand Down Expand Up @@ -194,6 +195,7 @@ program

.addOption(pattern)
.addOption(tags)
.addOption(fields)
.addOption(match)
.addOption(params)
.addOption(playwrightOpts)
Expand Down
1 change: 1 addition & 0 deletions src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ type BaseArgs = {
schedule?: MonitorConfig['schedule'];
locations?: MonitorConfig['locations'];
privateLocations?: MonitorConfig['privateLocations'];
fields?: MonitorConfig['fields'];
};

export type CliArgs = BaseArgs & {
Expand Down
22 changes: 22 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export async function normalizeOptions(
: {};

options.params = Object.freeze(merge(config.params, cliArgs.params || {}));
options.fields = Object.freeze(
merge(config.monitor?.fields ?? {}, cliArgs?.fields || {})
);

/**
* Merge playwright options from CLI and Synthetics config
Expand Down Expand Up @@ -233,6 +236,24 @@ export function getCommonCommandOpts() {
'--match <name>',
'run/push tests with a name or tags that matches a pattern'
);
const fields = createOption(
'--fields <jsonstring>',
'add fields to the monitor(s) in the format { "key": "value"}'
).argParser((fieldsStr: string) => {
const fields = JSON.parse(fieldsStr);
if (typeof fields !== 'object') {
throw new Error('Invalid fields format');
}
// make sure all values are strings
return Object.entries(fields).reduce((acc, [key, value]) => {
if (typeof value !== 'string') {
acc[key] = JSON.stringify(value);
} else {
acc[key] = value;
}
return acc;
}, {});
});

return {
auth,
Expand All @@ -243,6 +264,7 @@ export function getCommonCommandOpts() {
configOpt,
tags,
match,
fields,
};
}

Expand Down

0 comments on commit 10be171

Please sign in to comment.