Skip to content

Commit

Permalink
feat: added ability to load metadata from file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinries committed Sep 27, 2023
1 parent fd6378c commit f329df0
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 10 deletions.
3 changes: 3 additions & 0 deletions misc/development/cli/factory/external/external-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.makeSomething = () => {
return 'something made__';
};
13 changes: 13 additions & 0 deletions misc/development/cli/factory/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions misc/development/cli/factory/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ../../../../packages/core/cli/lib/index.js start --config --repl --watch --factory ./services/main.js"
},
"author": "",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions misc/development/cli/factory/services/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (broker) => {
broker.createService(require('./user/index.js'));
broker.createService(require('./task/index.js'));
};
3 changes: 3 additions & 0 deletions misc/development/cli/factory/services/main_invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
name: 'invalid'
};
7 changes: 7 additions & 0 deletions misc/development/cli/factory/services/task/add.action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
addTask: {
handler (context) {
return 'hello from new user';
}
}
};
7 changes: 7 additions & 0 deletions misc/development/cli/factory/services/task/delete.action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
deleteTask: {
handler (context) {
return 'hello from delete task';
}
}
};
11 changes: 11 additions & 0 deletions misc/development/cli/factory/services/task/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

const addTask = require('./add.action');
const deleteTask = require('./delete.action');

module.exports = {
name: 'tast',
actions: {
...addTask,
...deleteTask
}
};
7 changes: 7 additions & 0 deletions misc/development/cli/factory/services/user/add.action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
addUser: {
handler (context) {
return 'hello from new user';
}
}
};
9 changes: 9 additions & 0 deletions misc/development/cli/factory/services/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

const addUser = require('./add.action');

module.exports = {
name: 'user',
actions: {
...addUser
}
};
3 changes: 3 additions & 0 deletions misc/development/cli/factory/testlib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.testFunc = () => {
return 'test';
};
3 changes: 3 additions & 0 deletions misc/development/cli/factory/user.addUser.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "kevin"
}
7 changes: 7 additions & 0 deletions misc/development/cli/factory/user.addUser.meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"user": {
"id": 1,
"name": "John Doe",
"email": "bla@test.de"
}
}
7 changes: 7 additions & 0 deletions misc/development/cli/factory/weave.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module.exports = {
nodeId: 'weave-js-cli',
logger: {
level: 'debug'
}
};
3 changes: 3 additions & 0 deletions misc/development/cli/simple/external/external-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.makeSomething = () => {
return 'something made__';
};
2 changes: 1 addition & 1 deletion misc/development/cli/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node ../../../../packages/core/cli/lib/index.js start --services ./services --config"
"start": "node ../../../../packages/core/cli/lib/index.js start --services ./services --config --repl --watch"
},
"author": "",
"license": "ISC"
Expand Down
22 changes: 22 additions & 0 deletions misc/development/cli/simple/services/private.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { testFunc } = require('../testlib');
const { makeSomething } = require('../external/external-test');
exports.name = 'test-private';

exports.settings = {
$private: true
};

exports.started = function () {
this.timer = setInterval(() => {
}, 2000);
};

exports.actions = {
hello () {
return makeSomething();
}
};

exports.started = async function () {
testFunc();
};
3 changes: 2 additions & 1 deletion packages/core/repl/lib/commands/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module.exports = ({ vorpal, broker }) => {
vorpal
.command('call <actionName> [jsonParams]', 'Call an action.')
.alias('c')
.option('-f, --file [filename]', 'Load params from file')
.option('-d, --data [filename]', 'Load params from file')
.option('-m, --metadata [filename]', 'Load metadata from file')
.option('--stream [filename]', 'Send a file as stream')
.option('-s, --save [filename]', 'Save response to file')
.autocomplete({
Expand Down
7 changes: 4 additions & 3 deletions packages/core/repl/lib/commands/dcall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const invokeAction = require('../helper/invoke-action');
module.exports = ({ vorpal, broker }) => {
vorpal
.command('dcall <nodeId> <actionName> [jsonParams]', 'Direct call of an action using its node ID.')
.option('--l [filename]', 'Load params from file')
.option('--f [filename]', 'Send a file as stream')
.option('--s [filename]', 'Save response to file')
.option('-d, --data [filename]', 'Load params from file')
.option('-m, --metadata [filename]', 'Load metadata from file')
.option('--stream [filename]', 'Send a file as stream')
.option('-s, --save [filename]', 'Save response to file')
.autocomplete({
data () {
return [...new Set(broker.runtime.registry.actionCollection.list({}).map(item => item.name))];
Expand Down
39 changes: 34 additions & 5 deletions packages/core/repl/lib/helper/invoke-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ function preparePayloadArguments (args, payload, done) {
function preparePayloadFromFile (args) {
let filePath;

console.log(args.options);
if (typeof args.options.file === 'string') {
filePath = path.resolve(args.options.file);
if (typeof args.options.data === 'string') {
filePath = path.resolve(args.options.data);
} else {
filePath = path.resolve(`${args.actionName}.data.json`);
}

if (fs.existsSync(filePath)) {
try {
console.log(cliUI.infoText(`Read payload from ${filePath}`));
console.log(cliUI.infoText(`Load data from ${filePath}`));
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
} catch (error) {
console.log(cliUI.errorText('Can\'t parse parameter file'), error);
Expand All @@ -125,6 +124,31 @@ function preparePayloadFromFile (args) {
}
}

function prepareMetadataFromFile (args, metadata) {
let filePath;

if (typeof args.options.loadMeta === 'string') {
filePath = path.resolve(args.options.loadMeta);
} else {
filePath = path.resolve(`${args.actionName}.meta.json`);
}

if (fs.existsSync(filePath)) {
try {
console.log(cliUI.infoText(`Load metadata from ${filePath}`));
const loadedMetadata = JSON.parse(fs.readFileSync(filePath, 'utf8'));
return {
...loadedMetadata,
...metadata
};
} catch (error) {
console.log(cliUI.errorText('Can\'t parse parameter file'), error);
}
} else {
console.log(cliUI.errorText(`File not found: ${filePath}`));
}
}

function preparePayloadStream (args) {
let filePath;

Expand All @@ -149,10 +173,15 @@ module.exports = (broker) =>
let payload = preparePayloadArguments(args, {}, done);

// Send parameters from file
if (args.options.file) {
if (args.options.data) {
payload = preparePayloadFromFile(args) || payload;
}

if (args.options.metadata) {
delete payload.metadata;
callOptions.meta = prepareMetadataFromFile(args, callOptions.meta);
}

// Prepare send file stream
if (args.options.stream) {
callOptions.stream = preparePayloadStream(args, payload) || payload;
Expand Down

0 comments on commit f329df0

Please sign in to comment.