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

[FEATURE] projectPreprocessor: Add handling for task extensions #64

Merged
merged 1 commit into from
Nov 17, 2018
Merged
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
21 changes: 21 additions & 0 deletions lib/projectPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ class ProjectPreprocessor {
case "project-shim":
this.handleShim(extension);
break;
case "task":
this.handleTask(extension);
break;
default:
throw new Error(`Unknown extension type '${extension.type}' for ${extension.id}`);
}
Expand All @@ -349,6 +352,9 @@ class ProjectPreprocessor {
}

handleShim(extension) {
if (!extension.shims) {
throw new Error(`Project shim extension ${extension.id} is missing 'shim' configuration`);
}
const {configurations, dependencies, collections} = extension.shims;

if (configurations) {
Expand Down Expand Up @@ -465,6 +471,21 @@ class ProjectPreprocessor {
}
}
}

handleTask(extension) {
if (!extension.metadata && !extension.metadata.name) {
throw new Error(`Task extension ${extension.id} is missing 'metadata.name' configuration`);
}
if (!extension.task) {
throw new Error(`Task extension ${extension.id} is missing 'task' configuration`);
}
const taskRepository = require("@ui5/builder").tasks.taskRepository;

const taskPath = path.join(extension.path, extension.task.path);
const task = require(taskPath); // throws if not found

taskRepository.addTask(extension.metadata.name, task);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/application.a/task.a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function () {};
65 changes: 65 additions & 0 deletions test/lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,68 @@ test("Project with unknown extension dependency inline configuration", (t) => {
return t.throws(projectPreprocessor.processTree(tree),
"Unknown extension type 'phony-pony' for extension.a", "Rejected with error");
});

test("Project with task extension dependency", (t) => {
// "project-type" extension handling not yet implemented => test currently checks for error
const tree = {
id: "application.a",
path: applicationAPath,
dependencies: [{
id: "ext.task.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "0.1",
kind: "extension",
type: "task",
metadata: {
name: "task.a"
},
task: {
path: "task.a.js"
}
}],
version: "1.0.0",
specVersion: "0.1",
type: "application",
metadata: {
name: "xy"
}
};
return projectPreprocessor.processTree(tree).then((parsedTree) => {
t.deepEqual(parsedTree.dependencies.length, 0, "Application project has no dependencies");
const taskRepository = require("@ui5/builder").tasks.taskRepository;
t.truthy(taskRepository.getTask("task.a"), "task.a has been added to the task repository");
});
});

test("Project with task extension dependency - task module not found", async (t) => {
// "project-type" extension handling not yet implemented => test currently checks for error
const tree = {
id: "application.a",
path: applicationAPath,
dependencies: [{
id: "ext.task.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "0.1",
kind: "extension",
type: "task",
metadata: {
name: "task.a"
},
task: {
path: "task.not.existing.js"
}
}],
version: "1.0.0",
specVersion: "0.1",
type: "application",
metadata: {
name: "xy"
}
};
const error = await t.throws(projectPreprocessor.processTree(tree));
t.regex(error.message, /^Cannot find module.*/, "Rejected with error");
});