Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative config - Allow function input for task events #576

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/lib/package-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@ module.exports = (pathToProject, entry, baseEslintPath, options = {}) => {
'node_modules',
],
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: 'eslint-loader',
exclude: /node_modules/,
options: {
baseConfig: baseEslintConfig,
useEslintrc: true,
ignore: !options.skipEslintIgnore,

// pack the library regardless of the eslint result
failOnError: false,
failOnWarning: false,
},
},
],
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // Ignore all optional deps of moment.js
]
Expand Down
13 changes: 8 additions & 5 deletions src/lib/validate-declarative-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ const TaskSchema = joi.array().items(
'should define property "resolvedIf" as: function(contact, report) { ... }.'
)
),
events: joi.alternatives().conditional('events', {
is: joi.array().length(1),
then: joi.array().items(EventSchema('optional')).min(1).required(),
otherwise: joi.array().items(EventSchema('required')).unique('id').required(),
}),
events: joi.alternatives().try(
joi.function(),
joi.alternatives().conditional('events', {
is: joi.array().length(1),
then: joi.array().items(EventSchema('optional')).min(1),
otherwise: joi.array().items(EventSchema('required')).unique('id'),
})
).required(),
priority: joi.alternatives().try(
joi.object({
level: joi.string().valid('high', 'medium').optional(),
Expand Down
15 changes: 13 additions & 2 deletions src/nools/task-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,19 @@ function emitTasks(taskDefinition, Utils, Task, emit, c, r) {

function emitForEvents(scheduledTaskIdx) {
var i, dueDate = null, event, priority, task;
for (i = 0; i < taskDefinition.events.length; i++) {
event = taskDefinition.events[i];

var events;
if (typeof taskDefinition.events === 'function') {
events = taskDefinition.events(c, r);
if (!Array.isArray(events)) {
throw Error('events did not return an array');
}
} else {
events = taskDefinition.events;
}

for (i = 0; i < events.length; i++) {
event = events[i];

if (event.dueDate) {
dueDate = event.dueDate(event, c, r, scheduledTaskIdx);
Expand Down
7 changes: 7 additions & 0 deletions test/lib/validate-declarative-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe('validate-declarative-schema', () => {
]);
});

it('events can be function', () => {
const task = buildTaskWithAction('contact');
task.events = () => [];
const actual = validate([task], TaskSchema);
expect(actual).to.be.empty;
});

it('array.unique internal', () => {
const schema = joi.array().items(joi.object({
event: joi.array().items(joi.object()).unique('id'),
Expand Down
Loading